|  | @@ -20,7 +20,7 @@ use think\Db;
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  |  class User extends Api
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -    protected $noNeedLogin = ['login', 'onLogin', 'mobilelogin', 'register', 'resetpwd', 'changemobile', 'third', 'getUserOpenid', 'wxMiniProgramLogin','getNickName','wechatlogin'];
 | 
	
		
			
				|  |  | +    protected $noNeedLogin = ['login', 'onLogin', 'mobilelogin', 'register', 'resetpwd', 'changemobile', 'third', 'getUserOpenid', 'wxMiniProgramLogin','getNickName','wechatlogin','bindmobile'];
 | 
	
		
			
				|  |  |      protected $noNeedRight = '*';
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      public function _initialize()
 | 
	
	
		
			
				|  | @@ -737,20 +737,28 @@ class User extends Api
 | 
	
		
			
				|  |  |              //如果已经有账号则直接登录
 | 
	
		
			
				|  |  |              $ret = $this->auth->direct($user['id']);
 | 
	
		
			
				|  |  |              $is_register = 0;
 | 
	
		
			
				|  |  | +            $userInfo = $this->auth->getUserinfo();
 | 
	
		
			
				|  |  |          } else {
 | 
	
		
			
				|  |  | -            $extend = [
 | 
	
		
			
				|  |  | +            //记录code和openid,绑定手机号的时候更新openid
 | 
	
		
			
				|  |  | +            $wechatCodeData = [
 | 
	
		
			
				|  |  | +                'code' => $code,
 | 
	
		
			
				|  |  |                  'openid' => $openid,
 | 
	
		
			
				|  |  | -                'nickname' => $wxuserinfo['nickname'],
 | 
	
		
			
				|  |  | -                'sex' => $wxuserinfo['sex'],
 | 
	
		
			
				|  |  | -                'status' => 'new',
 | 
	
		
			
				|  |  | +                'createtime' => time(),
 | 
	
		
			
				|  |  |              ];
 | 
	
		
			
				|  |  | -            $mobile = '';
 | 
	
		
			
				|  |  | -            $ret = $this->auth->register($mobile, Random::alnum(), $mobile, $extend);
 | 
	
		
			
				|  |  | +            $wechatCode = Db::name('wechat_code')->where(['code'=>$code])->find();
 | 
	
		
			
				|  |  | +            if (empty($wechatCode)) {
 | 
	
		
			
				|  |  | +                Db::name('wechat_code')->insertGetId($wechatCodeData);
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +                Db::name('wechat_code')->where(['code'=>$code])->update($wechatCodeData);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            $ret = true;
 | 
	
		
			
				|  |  |              $is_register = 1;
 | 
	
		
			
				|  |  | -            //$ret = $this->auth->openid_register($openid,$extend);
 | 
	
		
			
				|  |  | +            $userInfo = [];
 | 
	
		
			
				|  |  | +            $data = ['code'=>$code,'is_register' => $is_register, 'userinfo' => $userInfo];
 | 
	
		
			
				|  |  | +            $this->success('获取信息成功', $data, 2);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          if ($ret) {
 | 
	
		
			
				|  |  | -            $data = ['is_register' => $is_register, 'userinfo' => $this->auth->getUserinfo()];
 | 
	
		
			
				|  |  | +            $data = ['code'=>$code,'is_register' => $is_register, 'userinfo' => $userInfo];
 | 
	
		
			
				|  |  |              $this->success(__('Logged in successful'), $data);
 | 
	
		
			
				|  |  |          } else {
 | 
	
		
			
				|  |  |              $this->error($this->auth->getError());
 | 
	
	
		
			
				|  | @@ -1126,62 +1134,47 @@ class User extends Api
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  |          Db::startTrans();
 | 
	
		
			
				|  |  |          try {
 | 
	
		
			
				|  |  | -            $user = model('User')->find($this->auth->id);
 | 
	
		
			
				|  |  | +            $code = $this->request->param('code');
 | 
	
		
			
				|  |  |              $mobile = $this->request->param('mobile');
 | 
	
		
			
				|  |  |              $captcha = $this->request->param('captcha');
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            if(!empty($this->auth->mobile) && $mobile != $this->auth->mobile){
 | 
	
		
			
				|  |  | -                throw new Exception('已经绑定了手机号');
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -            if (!$mobile || !$captcha) {
 | 
	
		
			
				|  |  | +            if (!$mobile || !$captcha || !$code) {
 | 
	
		
			
				|  |  |                  throw new Exception(__('Invalid parameters'));
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              if (!Validate::regex($mobile, "^1\d{10}$")) {
 | 
	
		
			
				|  |  |                  throw new Exception(__('Mobile is incorrect'));
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              $result = Sms::check($mobile, $captcha, 'changemobile');
 | 
	
		
			
				|  |  | -            if (!$result && $captcha != 1212 ) {
 | 
	
		
			
				|  |  | +            if (!$result) {
 | 
	
		
			
				|  |  |                  throw new Exception(__('Captcha is incorrect'));
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              $where['mobile'] = $mobile;
 | 
	
		
			
				|  |  | -            $where['id'] = ['neq',$this->auth->id];
 | 
	
		
			
				|  |  | -            $where['status'] = ['neq','new'];
 | 
	
		
			
				|  |  |              $userData = model('User')->where($where)->find();//老用户
 | 
	
		
			
				|  |  | +            $wechatCodeWhere['code'] = $code;
 | 
	
		
			
				|  |  | +            $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
 | 
	
		
			
				|  |  | +            if (empty($wechatCode)) {
 | 
	
		
			
				|  |  | +                throw new Exception('请先微信登录');
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |              if (!empty($userData)) {
 | 
	
		
			
				|  |  |                  if (empty($userData['openid'])) {
 | 
	
		
			
				|  |  | -                    model('User')->update(['openid'=>$user['openid']],$where);//老用户更新openid
 | 
	
		
			
				|  |  | +                    model('User')->update(['openid' => $wechatCode['openid']],$where);//老用户更新openid
 | 
	
		
			
				|  |  |                  } else {
 | 
	
		
			
				|  |  | -                    throw new Exception('该手机号已被其他用户绑定');
 | 
	
		
			
				|  |  | +                    if ($userData['openid'] != $wechatCode['openid']) {
 | 
	
		
			
				|  |  | +                        throw new Exception('该手机号已被其他用户绑定');
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  | +                $ret = $this->auth->direct($userData['id']);
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +                $extend = [
 | 
	
		
			
				|  |  | +                    'openid' => $wechatCode['openid'],
 | 
	
		
			
				|  |  | +                ];
 | 
	
		
			
				|  |  | +                $ret = $this->auth->register($mobile, Random::alnum(), $mobile, $extend);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (!$ret) {
 | 
	
		
			
				|  |  | +                throw new Exception($this->auth->getError());
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | -            //清除微信登录没绑定手机号的数据
 | 
	
		
			
				|  |  | -            $whereDel['mobile'] = '';
 | 
	
		
			
				|  |  | -            $whereDel['status'] = 'new';
 | 
	
		
			
				|  |  | -            $userDel = model('User')->where($whereDel)->find();
 | 
	
		
			
				|  |  | -            $userPowerWhere['user_id'] = $userDel['id'];
 | 
	
		
			
				|  |  | -            model('UserPower')->where($userPowerWhere)->delete();
 | 
	
		
			
				|  |  | -            model('User')->where($whereDel)->delete();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              Sms::flush($mobile, 'changemobile');
 | 
	
		
			
				|  |  | -            $this->auth->direct($userData['id']);
 | 
	
		
			
				|  |  | -            //更新token
 | 
	
		
			
				|  |  | -            $userTokenWhere['user_id'] = $userDel['id'];
 | 
	
		
			
				|  |  | -            $userToken = Db::name('user_token')->where($userTokenWhere)->find();
 | 
	
		
			
				|  |  | -            if (!empty($userToken)) {
 | 
	
		
			
				|  |  | -                $userTokenStr = $userToken['token'];
 | 
	
		
			
				|  |  | -                $userTokenDelRes = Db::name('user_token')->where($userTokenWhere)->delete();
 | 
	
		
			
				|  |  | -                if (!$userTokenDelRes) {
 | 
	
		
			
				|  |  | -                    throw new Exception('绑定失败');
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -                $userOldTokenWhere['user_id'] = $userData['id'];
 | 
	
		
			
				|  |  | -                $userOldTOken = Db::name('user_token')->where($userOldTokenWhere)->find();
 | 
	
		
			
				|  |  | -                if (!empty($userOldTOken)) {
 | 
	
		
			
				|  |  | -                    $userOldTOkenRes = Db::name('user_token')->where($userOldTokenWhere)->update(['token'=>$userTokenStr]);
 | 
	
		
			
				|  |  | -                    if (!$userOldTOkenRes) {
 | 
	
		
			
				|  |  | -                       throw new Exception('绑定登录失败');
 | 
	
		
			
				|  |  | -                    }
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  |              Db::commit();
 | 
	
		
			
				|  |  |              $this->success('success',$this->userInfo('return'));
 | 
	
		
			
				|  |  |          } catch (Exception $e) {
 | 
	
	
		
			
				|  | @@ -1216,25 +1209,12 @@ class User extends Api
 | 
	
		
			
				|  |  |              if(!empty($this->auth->openid) && $openid != $this->auth->openid){
 | 
	
		
			
				|  |  |                  throw new Exception('已经绑定了微信号');
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | -            $where['openid'] = $openid;
 | 
	
		
			
				|  |  | -            $where['id'] = ['neq',$this->auth->id];
 | 
	
		
			
				|  |  | -            $where['status'] = ['neq','new'];
 | 
	
		
			
				|  |  | -            $userData = model('User')->where($where)->find();
 | 
	
		
			
				|  |  | -            if (!empty($userData)) {
 | 
	
		
			
				|  |  | -                throw new Exception('该微信号已被其他用户绑定');
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              $user->openid = $openid;
 | 
	
		
			
				|  |  |              $userRes = $user->save();
 | 
	
		
			
				|  |  | -            if ($userRes) {
 | 
	
		
			
				|  |  | -                $whereDel['openid'] = '';
 | 
	
		
			
				|  |  | -                $whereDel['status'] = 'new';
 | 
	
		
			
				|  |  | -                $userDel = model('User')->where($whereDel)->find();
 | 
	
		
			
				|  |  | -                $userPowerWhere['user_id'] = $userDel['id'];
 | 
	
		
			
				|  |  | -                model('UserPower')->where($userPowerWhere)->delete();
 | 
	
		
			
				|  |  | -                model('User')->where($whereDel)->delete();
 | 
	
		
			
				|  |  | +            if (!$userRes) {
 | 
	
		
			
				|  |  | +                throw new Exception('绑定微信失败');
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |              Db::commit();
 | 
	
		
			
				|  |  |              $this->success('success',$this->userInfo('return'));
 | 
	
		
			
				|  |  |          } catch (Exception $e) {
 |