Browse Source

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

lizhen_gitee 1 năm trước cách đây
mục cha
commit
47b142169c
1 tập tin đã thay đổi với 38 bổ sung33 xóa
  1. 38 33
      application/api/controller/User.php

+ 38 - 33
application/api/controller/User.php

@@ -155,21 +155,19 @@ class User extends Api
 
     //微信登录,预先假注册
     public function wechatlogin(){
-
         $code = $this->request->param('code','');
         if(!$code){
             $this->error(__('Invalid parameters'));
         }
         //微信
         $wechat = new Wechat();
-
         $wxuserinfo = $wechat->getwxuserinfo($code);
-
         if(!$wxuserinfo){
             $this->error('openid获取失败');
         }
         $openid = $wxuserinfo['openid'];
 
+        //检查用户
         $user = Db::name('user')->where('wechat_openid',$openid)->find();
         if ($user) {
             if ($user['status'] == -1) {
@@ -180,8 +178,16 @@ class User extends Api
             }
             //如果已经有账号则直接登录
             $ret = $this->auth->direct($user['id']);
-            $is_register = 0;
-            $userInfo = $this->auth->getUserinfo();
+
+            if ($ret) {
+                $userInfo = $this->auth->getUserinfo();
+                $userInfo['is_register'] = 0;
+                $userInfo['code'] = $code;
+                $this->success(__('Logged in successful'), $userInfo);
+            } else {
+                $this->error($this->auth->getError());
+            }
+
         } else {
             //记录code和openid,绑定手机号的时候更新openid
             $wechatCodeData = [
@@ -195,18 +201,14 @@ class User extends Api
             } else {
                 Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
             }
-            $ret = true;
-            $is_register = 1;
+
+            //直接返回
             $userInfo = [];
-            $data = ['code'=>$code,'is_register' => $is_register, 'userinfo' => $userInfo];
-            $this->success('获取信息成功', $data, 2);
-        }
-        if ($ret) {
-            $data = ['code'=>$code,'is_register' => $is_register, 'userinfo' => $userInfo];
-            $this->success(__('Logged in successful'), $data);
-        } else {
-            $this->error($this->auth->getError());
+            $userInfo['is_register'] = 1;
+            $userInfo['code'] = $code;
+            $this->success('获取信息成功', $userInfo);
         }
+
     }
 
 
@@ -286,11 +288,12 @@ class User extends Api
 
     //苹果登录+预注册
     public function applelogin(){
-        $iosUserId = input_post('ios_user_id','');
+        $iosUserId = $this->request->param('ios_user_id','');
         if(!$iosUserId){
             $this->error(__('Invalid parameters'));
         }
 
+        //检查用户
         $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
         if ($user) {
             if ($user['status'] == -1) {
@@ -300,23 +303,25 @@ class User extends Api
                 $this->error(__('Account is locked'));
             }
             //如果已经有账号则直接登录
-            $is_register = 0;
             $ret = $this->auth->direct($user['id']);
-            $userInfo = $this->auth->getUserinfo();
-        } else {
-            $is_register = 1;
-            $ret = true;
-            $userInfo = [];
 
-            $data = ['ios_user_id'=>$iosUserId,'is_register' => $is_register, 'userinfo' => $userInfo];
-            $this->success('获取信息成功', $data, 2);
-        }
-        if ($ret) {
-            $data = ['ios_user_id'=>$iosUserId,'is_register' => $is_register, 'userinfo' => $userInfo];
-            $this->success(__('Logged in successful'), $data);
+            if ($ret) {
+                $userInfo = $this->auth->getUserinfo();
+                $userInfo['is_register'] = 0;
+                $userInfo['ios_user_id'] = $iosUserId;
+                $this->success(__('Logged in successful'), $userInfo);
+            } else {
+                $this->error($this->auth->getError());
+            }
+
         } else {
-            $this->error($this->auth->getError());
+            //直接返回
+            $userInfo = [];
+            $userInfo['is_register'] = 1;
+            $userInfo['ios_user_id'] = $iosUserId;
+            $this->success('获取信息成功', $userInfo);
         }
+
     }
 
     //用户详细资料
@@ -572,7 +577,7 @@ class User extends Api
             $this->error(__('Invalid parameters'));
         }
         /*if (!Validate::regex($mobile, "^1\d{10}$")) {
-            throw new Exception(__('Mobile is incorrect'));
+            $this->error(__('Mobile is incorrect'));
         }*/
         $result = Sms::check($mobile, $captcha, 'changemobile');
         if (!$result) {
@@ -638,7 +643,7 @@ class User extends Api
             $this->error(__('Invalid parameters'));
         }
         /*if (!Validate::regex($mobile, "^1\d{10}$")) {
-            throw new Exception(__('Mobile is incorrect'));
+            $this->error(__('Mobile is incorrect'));
         }*/
         $result = Sms::check($mobile, $captcha, 'changemobile');
         if (!$result) {
@@ -673,7 +678,7 @@ class User extends Api
                 model('User')->update(['wechat_openid' => $wechatCode['openid']],$where);//老用户更新openid
             } else {
                 if ($userData['wechat_openid'] != $wechatCode['openid']) {
-                    throw new Exception('该手机号已被其他用户绑定');
+                    $this->error('该手机号已被其他用户绑定');
                 }
             }
             $ret = $this->auth->direct($userData['id']);
@@ -684,7 +689,7 @@ class User extends Api
             $ret = $this->auth->register('', '','', $mobile, $extend);
         }
         if (!$ret) {
-            throw new Exception($this->auth->getError());
+            $this->error($this->auth->getError());
         }
 
         $this->success('success',$this->userInfo('return'));