Browse Source

微信预注册和苹果预注册的用户,实注册

lizhen_gitee 1 year ago
parent
commit
6b73f21ad1
1 changed files with 101 additions and 83 deletions
  1. 101 83
      application/api/controller/User.php

+ 101 - 83
application/api/controller/User.php

@@ -23,7 +23,7 @@ use app\common\library\Wechat;
  */
 class User extends Api
 {
-    protected $noNeedLogin = ['login', 'mobilelogin','wechatlogin','applelogin', 'register', 'resetpwd', 'changemobile', 'onlogin','getUserOpenid_gzh','jssdkBuildConfig'];
+    protected $noNeedLogin = ['login', 'mobilelogin','wechatlogin','applelogin','bindmobile','applebindmobile', 'register', 'resetpwd', 'changemobile', 'onlogin','getUserOpenid_gzh','jssdkBuildConfig'];
     protected $noNeedRight = '*';
 
     public function _initialize()
@@ -564,52 +564,62 @@ class User extends Api
      */
     public function applebindmobile()
     {
-        Db::startTrans();
-        try {
-            $iosUserId = $this->request->param('ios_user_id','');
-            $mobile    = $this->request->param('mobile');
-            $captcha   = $this->request->param('captcha');
+        $mobile    = $this->request->param('mobile');
+        $captcha   = $this->request->param('captcha');
+        $iosUserId = $this->request->param('ios_user_id','');
 
-            if (!$mobile || !$captcha || !$iosUserId) {
-                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) {
-                throw new Exception(__('Captcha is incorrect'));
-            }
+        if (!$mobile || !$captcha || !$iosUserId) {
+            $this->error(__('Invalid parameters'));
+        }
+        /*if (!Validate::regex($mobile, "^1\d{10}$")) {
+            throw new Exception(__('Mobile is incorrect'));
+        }*/
+        $result = Sms::check($mobile, $captcha, 'changemobile');
+        if (!$result) {
+            $this->error(__('Captcha is incorrect'));
+        }
 
 
-            $where['mobile'] = $mobile;
-            $userData = model('User')->where($where)->find();//老用户
-            if (!empty($userData)) {
-                if (empty($userData['ios_user_id'])) {
-                    model('User')->update(['ios_user_id' => $iosUserId],$where);//老用户更新openid
-                } else {
-                    if ($userData['ios_user_id'] != $iosUserId) {
-                        throw new Exception('该手机号已被其他用户绑定');
-                    }
-                }
-                $ret = $this->auth->direct($userData['id']);
-            } else {
-                $extend = [
-                    'ios_user_id' => $iosUserId,
-                ];
-                $ret = $this->auth->register('', '','', $mobile, $extend);
+        //检查ios_user_id绑定的用户
+        $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
+        if ($user) {
+            if ($user['status'] == -1) {
+                $this->error('账户已经注销');
             }
-            if (!$ret) {
-                throw new Exception($this->auth->getError());
+            if ($user['status'] != 1) {
+                $this->error(__('Account is locked'));
             }
+            //如果已经有账号则直接登录
 
-            Sms::flush($mobile, 'changemobile');
-            Db::commit();
+            $ret = $this->auth->direct($user['id']);
             $this->success('success',$this->userInfo('return'));
-        } catch (Exception $e) {
-            Db::rollback();
-            $this->error($e->getMessage());
         }
+
+        //新的ios用户
+        $where['mobile'] = $mobile;
+        $userData = model('User')->where($where)->find();//老用户
+        if (!empty($userData)) {
+            if (empty($userData['ios_user_id'])) {
+                model('User')->update(['ios_user_id' => $iosUserId],$where);//老用户更新ios_user_id
+            } else {
+                if ($userData['ios_user_id'] != $iosUserId) {
+                    $this->error('该手机号已被其他用户绑定');
+                }
+            }
+            $ret = $this->auth->direct($userData['id']);
+        } else {
+            $extend = [
+                'ios_user_id' => $iosUserId,
+            ];
+            $ret = $this->auth->register('', '','', $mobile, $extend);
+        }
+        if (!$ret) {
+            $this->error($this->auth->getError());
+        }
+
+
+        $this->success('success',$this->userInfo('return'));
+
     }
     /**
      * 微信注册来的,绑定手机号
@@ -620,57 +630,65 @@ class User extends Api
      */
     public function bindmobile()
     {
-        Db::startTrans();
-        try {
-            $code = $this->request->param('code');
-            $mobile = $this->request->param('mobile');
-            $captcha = $this->request->param('captcha');
+        $mobile = $this->request->param('mobile');
+        $captcha = $this->request->param('captcha');
+        $code = $this->request->param('code');
 
-            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) {
-                throw new Exception(__('Captcha is incorrect'));
-            }
+        if (!$mobile || !$captcha || !$code) {
+            $this->error(__('Invalid parameters'));
+        }
+        /*if (!Validate::regex($mobile, "^1\d{10}$")) {
+            throw new Exception(__('Mobile is incorrect'));
+        }*/
+        $result = Sms::check($mobile, $captcha, 'changemobile');
+        if (!$result) {
+            $this->error(__('Captcha is incorrect'));
+        }
 
-            $wechatCodeWhere['code'] = $code;
-            $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
-            if (empty($wechatCode)) {
-                throw new Exception('请先微信登录');
-            }
+        $wechatCodeWhere['code'] = $code;
+        $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
+        if (empty($wechatCode)) {
+            $this->error('请先微信登录');
+        }
 
-            $where['mobile'] = $mobile;
-            $userData = model('User')->where($where)->find();//老用户
-            if (!empty($userData)) {
-                if (empty($userData['openid'])) {
-                    model('User')->update(['wechat_openid' => $wechatCode['openid']],$where);//老用户更新openid
-                } else {
-                    if ($userData['wechat_openid'] != $wechatCode['openid']) {
-                        throw new Exception('该手机号已被其他用户绑定');
-                    }
-                }
-                $ret = $this->auth->direct($userData['id']);
-            } else {
-                $extend = [
-                    'wechat_openid' => $wechatCode['openid'],
-                ];
-                $ret = $this->auth->register('', '','', $mobile, $extend);
+        //检查appid绑定的用户
+        $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
+        if ($user) {
+            if ($user['status'] == -1) {
+                $this->error('账户已注销');
             }
-            if (!$ret) {
-                throw new Exception($this->auth->getError());
+            if ($user['status'] != 1) {
+                $this->error(__('Account is locked'));
             }
-
-            Sms::flush($mobile, 'changemobile');
-            Db::commit();
+            //如果已经有账号则直接登录
+            $ret = $this->auth->direct($user['id']);
             $this->success('success',$this->userInfo('return'));
-        } catch (Exception $e) {
-            Db::rollback();
-            $this->error($e->getMessage());
         }
+
+        //新的openid用户
+        $where['mobile'] = $mobile;
+        $userData = model('User')->where($where)->find();//老用户
+        if (!empty($userData)) {
+            if (empty($userData['wechat_openid'])) {
+                model('User')->update(['wechat_openid' => $wechatCode['openid']],$where);//老用户更新openid
+            } else {
+                if ($userData['wechat_openid'] != $wechatCode['openid']) {
+                    throw new Exception('该手机号已被其他用户绑定');
+                }
+            }
+            $ret = $this->auth->direct($userData['id']);
+        } else {
+            $extend = [
+                'wechat_openid' => $wechatCode['openid'],
+            ];
+            $ret = $this->auth->register('', '','', $mobile, $extend);
+        }
+        if (!$ret) {
+            throw new Exception($this->auth->getError());
+        }
+
+        $this->success('success',$this->userInfo('return'));
+
     }
 
     /**