ClientuserService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Elton
  5. * Date: 2019/4/15
  6. * Time: 14:37
  7. */
  8. namespace app\common\service;
  9. use app\admin\controller\auth\User;
  10. use app\main\constants\PayConstants;
  11. use app\main\service\FinancialService;
  12. use app\main\service\UserService;
  13. use think\Config;
  14. use think\Log;
  15. class ClientuserService
  16. {
  17. protected static $self = null;
  18. public static function instance()
  19. {
  20. if (self::$self == null) {
  21. self::$self = new self();
  22. }
  23. return self::$self;
  24. }
  25. public function getUserPhoneModel()
  26. {
  27. return model('UserPhone');
  28. }
  29. public function getUserModel()
  30. {
  31. return model('User');
  32. }
  33. /**
  34. * 给用户绑定手机号
  35. * @param $uid
  36. * @param $phone
  37. * @return bool|false|int
  38. */
  39. public function bindUserByPhone($uid, $phone)
  40. {
  41. try {
  42. $data = $this->getUserPhoneModel()->setConnect($phone)->getInfoByPhone($phone);
  43. if (!$data) {
  44. // 维护关系表
  45. $this->getUserPhoneModel()->setConnect($phone)->save(['uid' => $uid, 'phone' => $phone]);
  46. //给USER表维护冗余字段mobile
  47. $userInfo = $this->getUserModel()->getUserInfo($uid);
  48. if (empty($userInfo['mobile'])) {
  49. UserService::instance()->update(['mobile' => $phone, 'bindtime' => time()], ['id' => $uid]);
  50. }
  51. //手机号绑定成功,赠送书币
  52. $kandian = empty(Config::get('site.phone_bind_credit')) ? 0 : intval(Config::get('site.phone_bind_credit'));
  53. UserService::instance()->addFreeKanDian($uid, $kandian, time(), 'APP用户绑定手机号赠送书币', PayConstants::BUSINESS_APP);
  54. return $userInfo;
  55. } else {
  56. return false;
  57. }
  58. } catch (\Exception $exception) {
  59. Log::error($exception->getMessage());
  60. return false;
  61. }
  62. }
  63. /**
  64. * @param $uid
  65. * @param $oldphone
  66. * @param $newphone
  67. * @return bool
  68. */
  69. public function modifyPhone($uid, $oldphone, $newphone)
  70. {
  71. try {
  72. // 修改User 表中的冗余字段mobile
  73. UserService::instance()->update(['mobile' => $newphone], ['id' => $uid]);
  74. // 修改关联表user_phone 中的phone{1.删除记录;2.重新插入一条记录[原因:以手机号作为分库标准]}
  75. $result = $this->getUserPhoneModel()->modifyPhone($uid, $oldphone, $newphone);
  76. return $result;
  77. } catch (\Exception $e) {
  78. Log::error('[ ClientUserService ]' . $e->getMessage());
  79. }
  80. }
  81. /**
  82. *
  83. * @param $uid
  84. * @param $gender
  85. */
  86. public function modifyGender($uid, $gender)
  87. {
  88. try {
  89. // 修改User 表中的sex 字段
  90. UserService::instance()->update(['sex' => $gender], ['id' => $uid]);
  91. return true;
  92. } catch (\Exception $e) {
  93. Log::error('[ ClientUserService ]' . $e->getMessage());
  94. return false;
  95. }
  96. }
  97. /**
  98. * 通过手机号注册用户
  99. * @param $phone
  100. * @return bool|false|int
  101. */
  102. public function createUserByPhone($phone)
  103. {
  104. $channel_id = Config::get('site.fake_channel_id') ?? 0;
  105. // 伪造OpenID
  106. $open_id = self::getRandOpenid();
  107. Log::info('[ USERPHONE ] Open_id:' . $open_id);
  108. try {
  109. $user_id = UserService::instance()->fakeUserInfo($channel_id, $open_id, $phone, time(), PayConstants::BUSINESS_APP);
  110. Log::info('[ USERPHONE ] $uid:' . $user_id);
  111. Log::info('[ USERPHONE ] getUserDBIndex:' . UserService::instance()->getUserDBIndex($user_id));
  112. $this->getUserPhoneModel()->setConnect($phone)->save(['uid' => $user_id, 'phone' => $phone]);
  113. return $user_id;
  114. } catch (\Exception $exception) {
  115. Log::error($exception->getMessage());
  116. return false;
  117. }
  118. }
  119. /**
  120. * 获取用户信息 通过手UID
  121. * @param $uid
  122. * @return mixed
  123. */
  124. public function getUserInfoByUid($uid)
  125. {
  126. $userInfo = $this->getUserModel()->getUserInfo($uid);
  127. return $userInfo;
  128. }
  129. /**
  130. * 获取用户信息 通过手机号
  131. * @param $phone
  132. * @return array
  133. * @throws \think\Exception
  134. */
  135. public function getUserByPhone($phone)
  136. {
  137. $json_str = $this->getUserPhoneModel()->setConnect($phone)->getInfoByPhone($phone);
  138. if($json_str){
  139. return json_decode($json_str);
  140. }else{
  141. return false;
  142. }
  143. }
  144. //获取一个随机字符串28位
  145. public static function getRandOpenid()
  146. {
  147. // 字符串长度
  148. $len = 28;
  149. $string = '';
  150. for ($i = 1; $i < $len; $i++) {
  151. $randstr = chr(rand(65, 90)); //指定为字母
  152. $string .= $randstr;
  153. }
  154. return substr(md5($string), 0, 28);
  155. }
  156. /**
  157. * 封装成接口需要的数据
  158. * @param $userinfo
  159. * @param $type
  160. * @return false|string
  161. */
  162. public function packageUserInfo($userinfo, $type = '')
  163. {
  164. $obj = [];
  165. unset($userinfo['isvip']);
  166. $obj = $userinfo;
  167. $obj['uid'] = $userinfo['id'] ?? '';
  168. $obj['token'] = $userinfo['openid'] ?? '';
  169. $obj['is_vip'] = $userinfo['vip_endtime'] > time() ? TRUE : FALSE;
  170. $obj['balance'] = FinancialService::instance()->getTotalKandianAndFreeKandian($userinfo['id'])->data;
  171. $signTodayData = model('Sign')->setConnect($obj['uid'])->userSignToday();
  172. if($signTodayData){
  173. $obj['is_today_signed'] = 1;
  174. }else{
  175. $obj['is_today_signed'] = 0;
  176. }
  177. if ($type == 'json') {
  178. return json_encode($obj);
  179. } else {
  180. return $obj;
  181. }
  182. }
  183. }