|  | @@ -740,6 +740,10 @@ class User extends Api
 | 
	
		
			
				|  |  |          } else {
 | 
	
		
			
				|  |  |              $extend = [
 | 
	
		
			
				|  |  |                  'openid' => $openid,
 | 
	
		
			
				|  |  | +                'nickname' => $wxuserinfo['nickname'],
 | 
	
		
			
				|  |  | +                'sex' => $wxuserinfo['sex'],
 | 
	
		
			
				|  |  | +                'avatar' => $wxuserinfo['headimgurl'],
 | 
	
		
			
				|  |  | +                'status' => 'new',
 | 
	
		
			
				|  |  |              ];
 | 
	
		
			
				|  |  |              $mobile = '';
 | 
	
		
			
				|  |  |              $ret = $this->auth->register($mobile, Random::alnum(), $mobile, $extend);
 | 
	
	
		
			
				|  | @@ -1121,38 +1125,49 @@ class User extends Api
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      public function bindmobile()
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | -        //$user = $this->auth->getUser();
 | 
	
		
			
				|  |  | -        $user = model('User')->find($this->auth->id);
 | 
	
		
			
				|  |  | -        $mobile = $this->request->param('mobile');
 | 
	
		
			
				|  |  | -        $captcha = $this->request->param('captcha');
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            $user = model('User')->find($this->auth->id);
 | 
	
		
			
				|  |  | +            $mobile = $this->request->param('mobile');
 | 
	
		
			
				|  |  | +            $captcha = $this->request->param('captcha');
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        if(!empty($this->auth->mobile)){
 | 
	
		
			
				|  |  | -            $this->error('已经绑定了手机号');
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        if (!$mobile || !$captcha) {
 | 
	
		
			
				|  |  | -            $this->error(__('Invalid parameters'));
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        if (!Validate::regex($mobile, "^1\d{10}$")) {
 | 
	
		
			
				|  |  | -            $this->error(__('Mobile is incorrect'));
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        $userData = model('User')->where('mobile', $mobile)->find();
 | 
	
		
			
				|  |  | -        if (!empty($userData)) {
 | 
	
		
			
				|  |  | -            if ($userData['id'] != $this->auth->id) {
 | 
	
		
			
				|  |  | +            if(!empty($this->auth->mobile) && $mobile != $this->auth->mobile){
 | 
	
		
			
				|  |  | +                throw new Exception('已经绑定了手机号');
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (!$mobile || !$captcha) {
 | 
	
		
			
				|  |  | +                throw new Exception(__('Invalid parameters'));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (!Validate::regex($mobile, "^1\d{10}$")) {
 | 
	
		
			
				|  |  | +                throw new Exception(__('Mobile is incorrect'));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            $where['mobile'] = $mobile;
 | 
	
		
			
				|  |  | +            $where['id'] = ['!=',$this->auth->id];
 | 
	
		
			
				|  |  | +            $where['status'] = ['!=','new'];
 | 
	
		
			
				|  |  | +            $userData = model('User')->where($where)->find();
 | 
	
		
			
				|  |  | +            if (!empty($userData)) {
 | 
	
		
			
				|  |  |                  $this->error('该手机号已被其他用户绑定');
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        $result = Sms::check($mobile, $captcha, 'changemobile');
 | 
	
		
			
				|  |  | -        if (!$result && $captcha != 1212 ) {
 | 
	
		
			
				|  |  | -            $this->error(__('Captcha is incorrect'));
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        $verification = $user->verification;
 | 
	
		
			
				|  |  | -        $verification->mobile = 1;
 | 
	
		
			
				|  |  | -        $user->verification = $verification;
 | 
	
		
			
				|  |  | -        $user->mobile = $mobile;
 | 
	
		
			
				|  |  | -        $user->save();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        Sms::flush($mobile, 'changemobile');
 | 
	
		
			
				|  |  | -        $this->success('success',$this->userInfo('return'));
 | 
	
		
			
				|  |  | +            $result = Sms::check($mobile, $captcha, 'changemobile');
 | 
	
		
			
				|  |  | +            if (!$result && $captcha != 1212 ) {
 | 
	
		
			
				|  |  | +                throw new Exception(__('Captcha is incorrect'));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            $verification = $user->verification;
 | 
	
		
			
				|  |  | +            $verification->mobile = 1;
 | 
	
		
			
				|  |  | +            $user->verification = $verification;
 | 
	
		
			
				|  |  | +            $user->mobile = $mobile;
 | 
	
		
			
				|  |  | +            $userRes = $user->save();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if ($userRes) {
 | 
	
		
			
				|  |  | +                $where['mobile'] = $mobile;
 | 
	
		
			
				|  |  | +                $where['status'] = 'new';
 | 
	
		
			
				|  |  | +                model('User')->where($where)->delete();
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            Sms::flush($mobile, 'changemobile');
 | 
	
		
			
				|  |  | +            $this->success('success',$this->userInfo('return'));
 | 
	
		
			
				|  |  | +        } catch (Exception $e) {
 | 
	
		
			
				|  |  | +            $this->error($e->getMessage());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
	
		
			
				|  | @@ -1163,31 +1178,45 @@ class User extends Api
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      public function bindopenid()
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | -        $code =  $this->request->param('code','');
 | 
	
		
			
				|  |  | -        if(!$code){
 | 
	
		
			
				|  |  | -            $this->error(__('Invalid parameters'));
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        //微信
 | 
	
		
			
				|  |  | -        $wechat = new Wechat();
 | 
	
		
			
				|  |  | -        $openid = $wechat->getOpenid($code);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        if(!$openid){
 | 
	
		
			
				|  |  | -            $this->error('openid获取失败');
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | +        Db::startTrans();
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            $code =  $this->request->param('code','');
 | 
	
		
			
				|  |  | +            if(!$code){
 | 
	
		
			
				|  |  | +                throw new Exception(__('Invalid parameters'));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            //微信
 | 
	
		
			
				|  |  | +            $wechat = new Wechat();
 | 
	
		
			
				|  |  | +            $openid = $wechat->getOpenid($code);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        $user = model('User')->find($this->auth->id);
 | 
	
		
			
				|  |  | -        if(!empty($this->auth->openid)){
 | 
	
		
			
				|  |  | -            $this->error('已经绑定了微信号');
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | +            if(!$openid){
 | 
	
		
			
				|  |  | +                throw new Exception('openid获取失败');
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        if (\app\common\model\User::where('openid', $openid)->find()) {
 | 
	
		
			
				|  |  | -            $this->error('该微信号已被其他用户绑定');
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | +            $user = model('User')->find($this->auth->id);
 | 
	
		
			
				|  |  | +            if(!empty($this->auth->openid) && $openid != $this->auth->openid){
 | 
	
		
			
				|  |  | +                throw new Exception('已经绑定了微信号');
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            $where['openid'] = $openid;
 | 
	
		
			
				|  |  | +            $where['id'] = ['!=',$this->auth->id];
 | 
	
		
			
				|  |  | +            $where['status'] = ['!=','new'];
 | 
	
		
			
				|  |  | +            $userData = model('User')->where($where)->find();
 | 
	
		
			
				|  |  | +            if (!empty($userData)) {
 | 
	
		
			
				|  |  | +                throw new Exception('该微信号已被其他用户绑定');
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        $user->openid = $openid;
 | 
	
		
			
				|  |  | -        $user->save();
 | 
	
		
			
				|  |  | +            $user->openid = $openid;
 | 
	
		
			
				|  |  | +            $userRes = $user->save();
 | 
	
		
			
				|  |  | +            if ($userRes) {
 | 
	
		
			
				|  |  | +                $where['status'] = 'new';
 | 
	
		
			
				|  |  | +                model('User')->where($where)->delete();
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        $this->success('success',$this->userInfo('return'));
 | 
	
		
			
				|  |  | +            Db::commit();
 | 
	
		
			
				|  |  | +            $this->success('success',$this->userInfo('return'));
 | 
	
		
			
				|  |  | +        } catch (Exception $e) {
 | 
	
		
			
				|  |  | +            Db::rollback();
 | 
	
		
			
				|  |  | +            $this->error($e->getMessage());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      //用户详细资料
 |