Browse Source

苹果登录+预注册

lizhen_gitee 1 năm trước cách đây
mục cha
commit
3526949bff

+ 80 - 13
application/api/controller/User.php

@@ -170,7 +170,7 @@ class User extends Api
         }
         $openid = $wxuserinfo['openid'];
 
-        $user = Db::name('user')->where('openid',$openid)->find();
+        $user = Db::name('user')->where('wechat_openid',$openid)->find();
         if ($user) {
             if ($user['status'] == -1) {
                 $this->error('账户已注销');
@@ -284,7 +284,7 @@ class User extends Api
         }
     }
 
-    //苹果登录+注册
+    //苹果登录+注册
     public function applelogin(){
         $iosUserId = input_post('ios_user_id','');
         if(!$iosUserId){
@@ -300,13 +300,20 @@ 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 = [];
 
-            $ret = $this->auth->ios_register($iosUserId);
+            $data = ['ios_user_id'=>$iosUserId,'is_register' => $is_register, 'userinfo' => $userInfo];
+            $this->success('获取信息成功', $data, 2);
         }
         if ($ret) {
-            $this->success(__('Logged in successful'), $this->auth->getUserinfo());
+            $data = ['ios_user_id'=>$iosUserId,'is_register' => $is_register, 'userinfo' => $userInfo];
+            $this->success(__('Logged in successful'), $data);
         } else {
             $this->error($this->auth->getError());
         }
@@ -474,17 +481,20 @@ class User extends Api
         $latitude  = input_post('latitude',0);
         $cityname  = input_post('cityname','');
         $plat_unique_id  = input_post('plat_unique_id','');
-        if(empty($longitude) || empty($latitude) || empty($cityname)){
+        /*if(empty($longitude) || empty($latitude) || empty($cityname)){
             $this->error();
-        }
+        }*/
 
-        $data = [
-            'longitude' => $longitude,
-            'latitude'  => $latitude,
-            'cityname'  => $cityname,
-            'plat_unique_id'  => $plat_unique_id,
-        ];
-        Db::name('user')->where('id',$this->auth->id)->update($data);
+        $data = [];
+
+        $longitude      && $data['longitude'] = $longitude;
+        $latitude       && $data['latitude'] = $latitude;
+        $cityname       && $data['cityname'] = $cityname;
+        $plat_unique_id && $data['plat_unique_id'] = $plat_unique_id;
+
+        if(!empty($data)){
+            Db::name('user')->where('id',$this->auth->id)->update($data);
+        }
         $this->success();
     }
 
@@ -544,6 +554,63 @@ class User extends Api
         Sms::flush($mobile, 'changemobile');
         $this->success();
     }
+
+    /**
+     * 苹果注册来的,绑定手机号
+     *
+     * @ApiMethod (POST)
+     * @param string $mobile   手机号
+     * @param string $captcha 验证码
+     */
+    public function applebindmobile()
+    {
+        Db::startTrans();
+        try {
+            $iosUserId = $this->request->param('ios_user_id','');
+            $mobile    = $this->request->param('mobile');
+            $captcha   = $this->request->param('captcha');
+
+            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'));
+            }
+
+
+            $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);
+            }
+            if (!$ret) {
+                throw new Exception($this->auth->getError());
+            }
+
+            Sms::flush($mobile, 'changemobile');
+            Db::commit();
+            $this->success('success',$this->userInfo('return'));
+        } catch (Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+    }
     /**
      * 微信注册来的,绑定手机号
      *

+ 0 - 75
application/common/library/Auth.php

@@ -291,81 +291,6 @@ class Auth
         return true;
     }
 
-    public function ios_register($ios_user_id = '', $extend = [])
-    {
-
-        if ($ios_user_id && User::getByiosuserid($ios_user_id)) {
-            $this->setError('ios_user_id已存在');
-            return false;
-        }
-
-        $ip = request()->ip();
-        $time = time();
-
-        $introcode = User::column("introcode");
-
-        $data = [
-            'ios_user_id'   => $ios_user_id,
-            'avatar' => '',
-            'introcode' => $this->getUinqueNo(8, $introcode),
-            'nickname'  => $this->get_rand_nick_name(),
-        ];
-        $params = array_merge($data, [
-            'jointime'  => $time,
-            'joinip'    => $ip,
-            'logintime' => $time,
-            'loginip'   => $ip,
-            'prevtime'  => $time,
-            'status'    => 1
-        ]);
-        $params = array_merge($params, $extend);
-
-        //账号注册时需要开启事务,避免出现垃圾数据
-        Db::startTrans();
-        try {
-            $user = User::create($params, true);
-
-            $this->_user = User::get($user->id);
-            $this->_user->username = 100000 + $user->id;
-            $this->_user->save();
-
-            //设置Token
-            $this->_token = Random::uuid();
-            Token::set($this->_token, $user->id, $this->keeptime);
-
-            //设置登录状态
-            $this->_logined = true;
-
-            //注册钱包
-            Db::name('user_wallet')->insertGetId(['user_id'=>$user->id]);
-            //注册用户活跃
-            Db::name('user_active')->insertGetId(['user_id'=>$user->id]);
-            //注册用户权限
-            Db::name('user_power')->insertGetId(['user_id'=>$user->id]);
-
-            //邀请用户注册,给邀请人奖励
-            if(isset($extend['intro_uid']) && !empty($extend['intro_uid'])){
-                $intro_gold = config('site.intro_newuser_gift_goldnum') ?: 0;
-                if($intro_gold > 0){
-                    $wallet_rs = model('wallet')->lockChangeAccountRemain($extend['intro_uid'],'gold',$intro_gold,63,'邀请'.$this->_user->username);
-                    if($wallet_rs['status'] === false){
-                        Db::rollback();
-                        $this->setError($wallet_rs['msg']);
-                        return false;
-                    }
-                }
-            }
-
-            //注册成功的事件
-            Hook::listen("user_register_successed", $this->_user, $data);
-            Db::commit();
-        } catch (Exception $e) {
-            $this->setError($e->getMessage());
-            Db::rollback();
-            return false;
-        }
-        return true;
-    }
 
     /**
      * 用户登录

+ 2 - 2
application/config.php

@@ -357,8 +357,8 @@ return [
 
     //开放平台的,不是小程序的
     'wxMiniProgram' => [
-        /*'appid'  => 'wx4148b31582a36cbc',
-        'secret' => '5c56f3d50113caf5d105279305a2ca44',*/
+        'appid'  => 'wx4148b31582a36cbc',
+        'secret' => '5c56f3d50113caf5d105279305a2ca44',
     ],
 
     //阿里银行卡三要素