Ver código fonte

微信登录/绑定手机号调整

zhangxiaobin 1 ano atrás
pai
commit
c6c7298bc3

+ 40 - 60
application/api/controller/User.php

@@ -20,7 +20,7 @@ use think\Db;
  */
 class User extends Api
 {
-    protected $noNeedLogin = ['login', 'onLogin', 'mobilelogin', 'register', 'resetpwd', 'changemobile', 'third', 'getUserOpenid', 'wxMiniProgramLogin','getNickName','wechatlogin'];
+    protected $noNeedLogin = ['login', 'onLogin', 'mobilelogin', 'register', 'resetpwd', 'changemobile', 'third', 'getUserOpenid', 'wxMiniProgramLogin','getNickName','wechatlogin','bindmobile'];
     protected $noNeedRight = '*';
 
     public function _initialize()
@@ -737,20 +737,28 @@ class User extends Api
             //如果已经有账号则直接登录
             $ret = $this->auth->direct($user['id']);
             $is_register = 0;
+            $userInfo = $this->auth->getUserinfo();
         } else {
-            $extend = [
+            //记录code和openid,绑定手机号的时候更新openid
+            $wechatCodeData = [
+                'code' => $code,
                 'openid' => $openid,
-                'nickname' => $wxuserinfo['nickname'],
-                'sex' => $wxuserinfo['sex'],
-                'status' => 'new',
+                'createtime' => time(),
             ];
-            $mobile = '';
-            $ret = $this->auth->register($mobile, Random::alnum(), $mobile, $extend);
+            $wechatCode = Db::name('wechat_code')->where(['code'=>$code])->find();
+            if (empty($wechatCode)) {
+                Db::name('wechat_code')->insertGetId($wechatCodeData);
+            } else {
+                Db::name('wechat_code')->where(['code'=>$code])->update($wechatCodeData);
+            }
+            $ret = true;
             $is_register = 1;
-            //$ret = $this->auth->openid_register($openid,$extend);
+            $userInfo = [];
+            $data = ['code'=>$code,'is_register' => $is_register, 'userinfo' => $userInfo];
+            $this->success('获取信息成功', $data, 2);
         }
         if ($ret) {
-            $data = ['is_register' => $is_register, 'userinfo' => $this->auth->getUserinfo()];
+            $data = ['code'=>$code,'is_register' => $is_register, 'userinfo' => $userInfo];
             $this->success(__('Logged in successful'), $data);
         } else {
             $this->error($this->auth->getError());
@@ -1126,62 +1134,47 @@ class User extends Api
     {
         Db::startTrans();
         try {
-            $user = model('User')->find($this->auth->id);
+            $code = $this->request->param('code');
             $mobile = $this->request->param('mobile');
             $captcha = $this->request->param('captcha');
 
-            if(!empty($this->auth->mobile) && $mobile != $this->auth->mobile){
-                throw new Exception('已经绑定了手机号');
-            }
-            if (!$mobile || !$captcha) {
+            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 && $captcha != 1212 ) {
+            if (!$result) {
                 throw new Exception(__('Captcha is incorrect'));
             }
             $where['mobile'] = $mobile;
-            $where['id'] = ['neq',$this->auth->id];
-            $where['status'] = ['neq','new'];
             $userData = model('User')->where($where)->find();//老用户
+            $wechatCodeWhere['code'] = $code;
+            $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
+            if (empty($wechatCode)) {
+                throw new Exception('请先微信登录');
+            }
             if (!empty($userData)) {
                 if (empty($userData['openid'])) {
-                    model('User')->update(['openid'=>$user['openid']],$where);//老用户更新openid
+                    model('User')->update(['openid' => $wechatCode['openid']],$where);//老用户更新openid
                 } else {
-                    throw new Exception('该手机号已被其他用户绑定');
+                    if ($userData['openid'] != $wechatCode['openid']) {
+                        throw new Exception('该手机号已被其他用户绑定');
+                    }
                 }
+                $ret = $this->auth->direct($userData['id']);
+            } else {
+                $extend = [
+                    'openid' => $wechatCode['openid'],
+                ];
+                $ret = $this->auth->register($mobile, Random::alnum(), $mobile, $extend);
+            }
+            if (!$ret) {
+                throw new Exception($this->auth->getError());
             }
-            //清除微信登录没绑定手机号的数据
-            $whereDel['mobile'] = '';
-            $whereDel['status'] = 'new';
-            $userDel = model('User')->where($whereDel)->find();
-            $userPowerWhere['user_id'] = $userDel['id'];
-            model('UserPower')->where($userPowerWhere)->delete();
-            model('User')->where($whereDel)->delete();
 
             Sms::flush($mobile, 'changemobile');
-            $this->auth->direct($userData['id']);
-            //更新token
-            $userTokenWhere['user_id'] = $userDel['id'];
-            $userToken = Db::name('user_token')->where($userTokenWhere)->find();
-            if (!empty($userToken)) {
-                $userTokenStr = $userToken['token'];
-                $userTokenDelRes = Db::name('user_token')->where($userTokenWhere)->delete();
-                if (!$userTokenDelRes) {
-                    throw new Exception('绑定失败');
-                }
-                $userOldTokenWhere['user_id'] = $userData['id'];
-                $userOldTOken = Db::name('user_token')->where($userOldTokenWhere)->find();
-                if (!empty($userOldTOken)) {
-                    $userOldTOkenRes = Db::name('user_token')->where($userOldTokenWhere)->update(['token'=>$userTokenStr]);
-                    if (!$userOldTOkenRes) {
-                       throw new Exception('绑定登录失败');
-                    }
-                }
-            }
             Db::commit();
             $this->success('success',$this->userInfo('return'));
         } catch (Exception $e) {
@@ -1216,25 +1209,12 @@ class User extends Api
             if(!empty($this->auth->openid) && $openid != $this->auth->openid){
                 throw new Exception('已经绑定了微信号');
             }
-            $where['openid'] = $openid;
-            $where['id'] = ['neq',$this->auth->id];
-            $where['status'] = ['neq','new'];
-            $userData = model('User')->where($where)->find();
-            if (!empty($userData)) {
-                throw new Exception('该微信号已被其他用户绑定');
-            }
 
             $user->openid = $openid;
             $userRes = $user->save();
-            if ($userRes) {
-                $whereDel['openid'] = '';
-                $whereDel['status'] = 'new';
-                $userDel = model('User')->where($whereDel)->find();
-                $userPowerWhere['user_id'] = $userDel['id'];
-                model('UserPower')->where($userPowerWhere)->delete();
-                model('User')->where($whereDel)->delete();
+            if (!$userRes) {
+                throw new Exception('绑定微信失败');
             }
-
             Db::commit();
             $this->success('success',$this->userInfo('return'));
         } catch (Exception $e) {

+ 6 - 4
application/common/service/UserService.php

@@ -237,10 +237,12 @@ class UserService
                             $moneyLog = model('UserMoneyLog')->where($moneyLogWhere)->find();
                             if (empty($moneyLog)) {
                                 $money = config('site.invite_money');
-                                $remark = '邀请推广充值';
-                                $res = model('Wallet')->lockChangeAccountRemain($preUser['id'],$money,'+',$preUser['money'],$remark,103,'money',$userId);
-                                if (!$res['status']) {
-                                    throw new Exception($res['msg']);
+                                if ($money > 0) {
+                                    $remark = '邀请推广充值';
+                                    $res = model('Wallet')->lockChangeAccountRemain($preUser['id'],$money,'+',$preUser['money'],$remark,103,'money',$userId);
+                                    if (!$res['status']) {
+                                        throw new Exception($res['msg']);
+                                    }
                                 }
                             }
                         }

+ 3 - 1
application/extra/site.php

@@ -108,7 +108,7 @@ return [
     'domain_name' => 'https://ggyuyin.huxiukeji.cn',
     'intro_image' => '/uploads/20230703/35b10db56529aa19086eeb3d1d0bb6b0.png',
     'egggift_content' => '<p>我是奖励概览</p>',
-    'sign_gift' => '1',
+    'sign_gift' => '0',
     'money_to_jewel' => '10',
     'eggplay_paymoney_min' => '1000',
     'invite_money' => '1',
@@ -121,4 +121,6 @@ return [
         .'2、通过自己分享的活动页面地址下载注册成功,'."\r\n"
         .'即发放注册奖励,若好友充值即奖励1元。',
     'eggnew_global_show' => '1',
+    'invite_gift' => '175',
+    'invite_gift_num' => '1',
 ];