Browse Source

微信注册,能绑定老用户的手机号

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

+ 84 - 52
application/api/controller/User.php

@@ -153,54 +153,63 @@ class User extends Api
         }
     }*/
 
-    //微信登录+注册
+    //微信登录,预先假注册
     public function wechatlogin(){
-        $nickname = input('nickname','');
-        $avatar = input('avatar','');
-        $gender = input('gender',1);
-        $wechat_openid = input('wechat_openid','');
 
-        if (!$wechat_openid) {
+        $code = $this->request->param('code','');
+        if(!$code){
             $this->error(__('Invalid parameters'));
         }
-        if($gender != 1){
-            $gender = 0;
+        //微信
+        $wechat = new Wechat();
+
+        $wxuserinfo = $wechat->getwxuserinfo($code);
+
+        if(!$wxuserinfo){
+            $this->error('openid获取失败');
         }
+        $openid = $wxuserinfo['openid'];
 
-        $user = \app\common\model\User::getByOpenid($wechat_openid);
+        $user = Db::name('user')->where('openid',$openid)->find();
         if ($user) {
-            if ($user->status == -1) {
+            if ($user['status'] == -1) {
                 $this->error('账户已注销');
             }
-            if ($user->status != 1) {
+            if ($user['status'] != 1) {
                 $this->error(__('Account is locked'));
             }
             //如果已经有账号则直接登录
-            $ret = $this->auth->direct($user->id);
-
-
+            $ret = $this->auth->direct($user['id']);
+            $is_register = 0;
+            $userInfo = $this->auth->getUserinfo();
         } else {
-            if (!$nickname || !$avatar) {
-                $this->error(__('Invalid parameters'));
-            }
-
-            $reg_data = [
-                'nickname'=>$nickname,
-                'avatar'=>$avatar,
-                'gender'=>$gender,
-                'register_from' => input('register_from',''),
+            //记录code和openid,绑定手机号的时候更新openid
+            $wechatCodeData = [
+                'code' => $code,
+                'openid' => $openid,
+                'createtime' => time(),
             ];
-            $ret = $this->auth->openid_register($wechat_openid,$reg_data);
-
+            $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
+            if (empty($wechatCode)) {
+                Db::name('wechat_code')->insertGetId($wechatCodeData);
+            } 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 = $this->userInfo('return');
+            $data = ['code'=>$code,'is_register' => $is_register, 'userinfo' => $userInfo];
             $this->success(__('Logged in successful'), $data);
         } else {
             $this->error($this->auth->getError());
         }
     }
 
+
     /**
      * 运营商一键登录
      */
@@ -544,34 +553,57 @@ class User extends Api
      */
     public function bindmobile()
     {
-        $user = $this->auth->getUser();
-        $mobile = $this->request->request('mobile');
-        $captcha = $this->request->request('captcha');
+        Db::startTrans();
+        try {
+            $code = $this->request->param('code');
+            $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'));
-        }*/
-        if (\app\common\model\User::where('mobile', $mobile)->find()) {
-            $this->error('该手机号已被其他用户绑定');
-        }
-        $result = Sms::check($mobile, $captcha, 'changemobile');
-        if (!$result) {
-            $this->error(__('Captcha is incorrect'));
-        }
-        $verification = $user->verification;
-        $verification->mobile = 1;
-        $user->verification = $verification;
-        $user->mobile = $mobile;
-        $user->save();
+            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'));
+            }
 
-        Sms::flush($mobile, 'changemobile');
-        $this->success('success',$this->userInfo('return'));
+            $wechatCodeWhere['code'] = $code;
+            $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
+            if (empty($wechatCode)) {
+                throw new Exception('请先微信登录');
+            }
+
+            $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);
+            }
+            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 - 74
application/common/library/Auth.php

@@ -290,81 +290,7 @@ class Auth
         }
         return true;
     }
-    public function openid_register($wechat_openid = '', $extend = [])
-    {
-
-        if ($wechat_openid && User::getByOpenid($wechat_openid)) {
-            $this->setError('openid已存在');
-            return false;
-        }
-
-        $ip = request()->ip();
-        $time = time();
-
-        $introcode = User::column("introcode");
-
-        $data = [
-            'wechat_openid'   => $wechat_openid,
-            '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;
-    }
     public function ios_register($ios_user_id = '', $extend = [])
     {
 

+ 2 - 2
application/common/library/Wechat.php

@@ -74,7 +74,7 @@ class Wechat
               ["msg"] => string(307) "{"openid":"o8lxjwRjlDortQKhTk1dpHjQxcBU","nickname":"科","sex":1,"language":"zh_CN","city":"临沂","province":"山东","country":"中国","headimgurl":"https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/LGYWIv4F5vxZ2zCM9GEUynoQeJ6ibX9IfoKPAWLlGIugn1mgaAMPuqxzPBDQ3ktLEv2ia7HmOeJYTg5LofG8YlwQ\/132","privilege":[]}"
             }
      */
-    /*public function getwxuserinfo($code = '')
+    public function getwxuserinfo($code = '')
     {
         $wxuserinfo = Session::get('wxuserinfo');
         if (!$wxuserinfo) {
@@ -95,7 +95,7 @@ class Wechat
 
         }
         return $wxuserinfo;
-    }*/
+    }
 
 
     /**