getUserPhoneModel()->setConnect($phone)->getInfoByPhone($phone); if (!$data) { // 维护关系表 $this->getUserPhoneModel()->setConnect($phone)->save(['uid' => $uid, 'phone' => $phone]); //给USER表维护冗余字段mobile $userInfo = $this->getUserModel()->getUserInfo($uid); if (empty($userInfo['mobile'])) { UserService::instance()->update(['mobile' => $phone, 'bindtime' => time()], ['id' => $uid]); } //手机号绑定成功,赠送书币 $kandian = empty(Config::get('site.phone_bind_credit')) ? 0 : intval(Config::get('site.phone_bind_credit')); UserService::instance()->addFreeKanDian($uid, $kandian, time(), 'APP用户绑定手机号赠送书币', PayConstants::BUSINESS_APP); return $userInfo; } else { return false; } } catch (\Exception $exception) { Log::error($exception->getMessage()); return false; } } /** * @param $uid * @param $oldphone * @param $newphone * @return bool */ public function modifyPhone($uid, $oldphone, $newphone) { try { // 修改User 表中的冗余字段mobile UserService::instance()->update(['mobile' => $newphone], ['id' => $uid]); // 修改关联表user_phone 中的phone{1.删除记录;2.重新插入一条记录[原因:以手机号作为分库标准]} $result = $this->getUserPhoneModel()->modifyPhone($uid, $oldphone, $newphone); return $result; } catch (\Exception $e) { Log::error('[ ClientUserService ]' . $e->getMessage()); } } /** * * @param $uid * @param $gender */ public function modifyGender($uid, $gender) { try { // 修改User 表中的sex 字段 UserService::instance()->update(['sex' => $gender], ['id' => $uid]); return true; } catch (\Exception $e) { Log::error('[ ClientUserService ]' . $e->getMessage()); return false; } } /** * 通过手机号注册用户 * @param $phone * @return bool|false|int */ public function createUserByPhone($phone) { $channel_id = Config::get('site.fake_channel_id') ?? 0; // 伪造OpenID $open_id = self::getRandOpenid(); Log::info('[ USERPHONE ] Open_id:' . $open_id); try { $user_id = UserService::instance()->fakeUserInfo($channel_id, $open_id, $phone, time(), PayConstants::BUSINESS_APP); Log::info('[ USERPHONE ] $uid:' . $user_id); Log::info('[ USERPHONE ] getUserDBIndex:' . UserService::instance()->getUserDBIndex($user_id)); $this->getUserPhoneModel()->setConnect($phone)->save(['uid' => $user_id, 'phone' => $phone]); return $user_id; } catch (\Exception $exception) { Log::error($exception->getMessage()); return false; } } /** * 获取用户信息 通过手UID * @param $uid * @return mixed */ public function getUserInfoByUid($uid) { $userInfo = $this->getUserModel()->getUserInfo($uid); return $userInfo; } /** * 获取用户信息 通过手机号 * @param $phone * @return array * @throws \think\Exception */ public function getUserByPhone($phone) { $json_str = $this->getUserPhoneModel()->setConnect($phone)->getInfoByPhone($phone); if($json_str){ return json_decode($json_str); }else{ return false; } } //获取一个随机字符串28位 public static function getRandOpenid() { // 字符串长度 $len = 28; $string = ''; for ($i = 1; $i < $len; $i++) { $randstr = chr(rand(65, 90)); //指定为字母 $string .= $randstr; } return substr(md5($string), 0, 28); } /** * 封装成接口需要的数据 * @param $userinfo * @param $type * @return false|string */ public function packageUserInfo($userinfo, $type = '') { $obj = []; unset($userinfo['isvip']); $obj = $userinfo; $obj['uid'] = $userinfo['id'] ?? ''; $obj['token'] = $userinfo['openid'] ?? ''; $obj['is_vip'] = $userinfo['vip_endtime'] > time() ? TRUE : FALSE; $obj['balance'] = FinancialService::instance()->getTotalKandianAndFreeKandian($userinfo['id'])->data; $signTodayData = model('Sign')->setConnect($obj['uid'])->userSignToday(); if($signTodayData){ $obj['is_today_signed'] = 1; }else{ $obj['is_today_signed'] = 0; } if ($type == 'json') { return json_encode($obj); } else { return $obj; } } }