register($userInfo, $referee_id); // 生成token (session3rd) $this->token = $this->token($userInfo['openid']); // 记录缓存, 7天 Cache::set($this->token, $user_id, 86400 * 7); return $user_id; } /** * 获取token */ public function getToken() { return $this->token; } /** * 生成用户认证的token */ private function token($openid) { return md5($openid . 'token_salt'); } /** * 自动注册用户 */ private function register($userInfo, $referee_id = 0) { $data = []; //通过unionid查询用户是否存在 $user = null; if (isset($userInfo['unionid']) && !empty($userInfo['unionid'])) { $data['union_id'] = $userInfo['unionid']; $user = self::detailByUnionid($userInfo['unionid']); } // 查询用户是否已存在 if (!$user) { $user = self::detail(['appopen_id' => $userInfo['openid']]); } if ($user) { $model = $user; // 只修改union_id if (isset($data['union_id'])) { $data = [ 'union_id' => $data['union_id'], ]; }else{ return $user['user_id']; } } else { $model = $this; $data['referee_id'] = $referee_id; $data['reg_source'] = 'app'; //默认等级 $data['grade_id'] = GradeModel::getDefaultGradeId(); } $data['appopen_id'] = $userInfo['openid']; // 用户昵称 if (!$user) { $data['nickName'] = preg_replace('/[\xf0-\xf7].{3}/', '', $userInfo['nickname']); } $data['avatarUrl'] = $userInfo['headimgurl']; $data['gender'] = $userInfo['sex']; $data['province'] = $userInfo['province']; $data['country'] = $userInfo['country']; $data['city'] = $userInfo['city']; $data['reg_source'] = 'app'; try { $this->startTrans(); // 保存/更新用户记录 if (!$model->save(array_merge($data, [ 'app_id' => self::$app_id ])) ) { throw new BaseException(['msg' => '用户注册失败']); } if (!$user && $referee_id > 0) { // 记录推荐人关系, RefereeModel::createRelation($model['user_id'], $referee_id); //更新用户邀请数量 (new UserModel())->where('user_id', '=', $referee_id)->inc('total_invite')->update(); } $this->commit(); } catch (\Exception $e) { $this->rollback(); throw new BaseException(['msg' => $e->getMessage()]); } return $model['user_id']; } /** * 手机号密码用户登录 */ public function phoneLogin($data) { $where = [ ['user_no','=',$data['mobile']], ['is_delete','=',0], ]; $user = $this->where($where)->order('user_id desc')->find(); if (empty($user)) { $this->error = '登录账号错误'; return false; } if ($user['password'] != md5($data['password'])) { $this->error = '登陆密码错误'; return false; } if (!empty($data['openid'])) { $weixin = [ // 'nickName' => authcode($data['nickName'], 'DECODE'), // 'avatarUrl' => authcode($data['avatarUrl'], 'DECODE'), 'mpopen_id' => authcode($data['openid'], 'DECODE'), ]; // $mpopen_id = authcode($data['openid'], 'DECODE'), if ($weixin['mpopen_id'] != $user['mpopen_id']) { if ($this->where('user_id',$user['user_id'])->update(['mpopen_id'=>$weixin['mpopen_id']])) { $ceshi = [ 'test' => json_encode($weixin,JSON_UNESCAPED_UNICODE), 'name' => '正确', 'time' => time(), ]; Db::name('user_ceshi')->insertGetId($ceshi); // 生成token (session3rd) $this->token = $this->token($user['user_no']); // 记录缓存, 30天 Cache::tag('cache')->set($this->token, $user['user_id'], 86400 * 30); return $user['user_id']; }else{ $ceshi = [ 'test' => json_encode($weixin,JSON_UNESCAPED_UNICODE), 'name' => '微信错误日志', 'time' => time(), ]; Db::name('user_ceshi')->insertGetId($ceshi); $this->error = '会员绑定微信信息失败'; return false; } }else{ // 生成token (session3rd) $this->token = $this->token($user['user_no']); // 记录缓存, 30天 Cache::tag('cache')->set($this->token, $user['user_id'], 86400 * 30); return $user['user_id']; } }else{ // 生成token (session3rd) $this->token = $this->token($user['user_no']); // 记录缓存, 30天 Cache::tag('cache')->set($this->token, $user['user_id'], 86400 * 30); return $user['user_id']; } } /** * 手机号验证码登录 */ public function smslogin($data) { $where = [ [ 'user_no','=',$data['mobile']], ['is_delete','=',0], ]; $user = $this->where($where)->order('user_id desc')->find(); if (empty($user)) { $this->error = '登录账号错误'; return false; } if (empty($user['mobile'])) { $this->error = '您的账号未绑定手机号'; return false; } $vall = [ 'mobile' => $user['mobile'], 'code' => $data['code'], ]; if (!$this->check($vall)) { return false; } if (!empty($data['openid'])) { $weixin = [ // 'nickName' => authcode($data['nickName'], 'DECODE'), // 'avatarUrl' => authcode($data['avatarUrl'], 'DECODE'), 'mpopen_id' => authcode($data['openid'], 'DECODE'), ]; if ($weixin['mpopen_id'] != $user['mpopen_id']) { if ($this->where('user_id',$user['user_id'])->update(['mpopen_id'=> $weixin['mpopen_id']])) { $ceshi = [ 'test' => json_encode($weixin,JSON_UNESCAPED_UNICODE), 'name' => '正确', 'time' => time(), ]; Db::name('user_ceshi')->insertGetId($ceshi); // 生成token (session3rd) $this->token = $this->token($user['user_no']); // 记录缓存, 30天 Cache::tag('cache')->set($this->token, $user['user_id'], 86400 * 30); return $user['user_id']; }else{ $ceshi = [ 'test' => json_encode($weixin,JSON_UNESCAPED_UNICODE), 'name' => '微信错误日志', 'time' => time(), ]; Db::name('user_ceshi')->insertGetId($ceshi); $this->error = '会员绑定微信信息失败'; return false; } }else{ // 生成token (session3rd) $this->token = $this->token($user['user_no']); // 记录缓存, 30天 Cache::tag('cache')->set($this->token, $user['user_id'], 86400 * 30); return $user['user_id']; } }else{ // 生成token (session3rd) $this->token = $this->token($user['user_no']); // 记录缓存, 30天 Cache::tag('cache')->set($this->token, $user['user_id'], 86400 * 30); return $user['user_id']; } } /* *重置密码 */ public function resetpassword($data) { $where = [[ 'user_no','=',$data['mobile']]]; $user = $this->where($where)->order('user_id desc')->find(); if (empty($user)) { $this->error = '登录账号错误'; return false; } if ($user['is_delete'] == 1) { $this->error = '手机号被禁止或删除,请联系客服'; return false; } if (empty($user['mobile'])) { $this->error = '您的账号未绑定手机号'; return false; } $vall = [ 'mobile' => $user['mobile'], 'code' => $data['code'], ]; if (!$this->check($vall)) { return false; } if (md5($data['password']) == $user['password']) { $this->error = '登陆密码与原密码相同'; return false; } return $this->where('user_id', $user['user_id'])->update([ 'password' => md5($data['password']) ]); } /* *手机号注册 */ public function phoneRegister($data) { if (!$this->check($data)) { return false; } $user = $this->where('user_no',$data['user_no'])->find(); if (empty($user)) { $vall = [ 'mobile' => $data['mobile'], 'user_no' => $data['user_no']??'', 'reg_source' => isset($data['reg_source'])?$data['reg_source']:'app', 'grade_id' => GradeModel::getDefaultGradeId(), 'app_id' => self::$app_id, 'password' => md5($data['password']) ]; if (empty($data['nickName'])) { $vall['nickName'] = $data['user_no']??''; } if (!empty($data['openid'])) { // $vall['avatarUrl'] = authcode($data['avatarUrl'], 'DECODE'); // $vall['nickName'] = authcode($data['nickName'], 'DECODE'); $vall['mpopen_id'] = authcode($data['openid'], 'DECODE'); } $this->save($vall); if ($data['referee_id'] > 0) { // 记录推荐人关系 RefereeModel::createRelation($this['user_id'], $data['referee_id']); //更新用户邀请数量 (new UserModel())->setIncInvite($data['referee_id']); //邀请好友送好礼 $data['invitation_id'] > 0 && (new Partake())->addPartake($data['invitation_id'], $data['referee_id'], $this['user_id']); } return true; } else { $this->error = '会员号已存在'; return false; } } /** * 验证 */ private function check($data) { //判断验证码是否过期、是否正确 $sms_model = new SmsModel(); $sms_record_list = $sms_model ->where('mobile', '=', $data['mobile']) ->order(['create_time' => 'desc']) ->limit(1)->select(); if (count($sms_record_list) == 0) { $this->error = '未查到短信发送记录'; return false; } $sms_model = $sms_record_list[0]; if ((time() - strtotime($sms_model['create_time'])) / 60 > 30) { $this->error = '短信验证码超时'; return false; } if ($sms_model['code'] != $data['code']) { $this->error = '验证码不正确'; return false; } return true; } /** * 手机号验证码登录 */ public function smsloginWx($data) { // 判断会员号如果是手机号,则查询是否有有此会员号 $pattern = '/^1\d{10}$/'; if (!preg_match($pattern, $data['mobile'])) { $this->error = '登录账号错误'; return false; } $user = $this->where('user_no', $data['mobile'])->find(); if (empty($user)) { $vall = [ 'mobile' => $data['mobile'], 'user_no' => $data['mobile'], 'reg_source' => 'wx', 'grade_id' => GradeModel::getDefaultGradeId(), 'app_id' => self::$app_id, 'password' => md5('123456'), 'nickName' => $data['mobile'], 'openid' => $data['openid'], // authcode($data['openid'], 'DECODE'), 'avatarUrl' => 'http://cni-ekshop.cni.com.cn/image/agent/user_head.png', ]; if (!$this->save($vall)) { $this->error = '登录失败'; return false; } $user = $this->where('user_no', $data['mobile'])->find(); } if (!$this->check($data)) { return false; } $info = [ 'open_id' => $data['openid'], 'reg_source' => 'wx', ]; $this->where('user_id', $user['user_id'])->update($info); // 生成token (session3rd) $this->token = $this->token($user['user_no']); // 记录缓存, 30天 Cache::tag('cache')->set($this->token, $user['user_id'], 86400 * 30); return $user['user_id']; } }