Browse Source

微信注册与手机号注册,大改动

lizhen_gitee 10 months ago
parent
commit
ec3247c5f6
2 changed files with 287 additions and 102 deletions
  1. 200 102
      application/api/controller/User.php
  2. 87 0
      application/common/library/Auth.php

+ 200 - 102
application/api/controller/User.php

@@ -146,9 +146,16 @@ class User extends Api
 
 
 
-    //微信登录,预先假注册
-    public function wechatlogin(){
-        $code = $this->request->param('code','');
+
+    /**
+     * 手机注册来的,绑定微信
+     *
+     * @ApiMethod (POST)
+     * @param string $wechat_openid
+     */
+    public function bindopenid()
+    {
+        $code = input('code','');
         if(!$code){
             $this->error(__('Invalid parameters'));
         }
@@ -163,84 +170,48 @@ class User extends Api
             $this->error('openid获取失败');
         }
 
-        $openid = $wxuserinfo['openid'];
+        $wechat_openid = $wxuserinfo['openid'];
 
-        //检查用户
-        $user = Db::name('user')->where('wechat_openid',$openid)->find();
-        if ($user) {
-            if ($user['status'] == -1) {
-                $this->error('账户已注销');
-            }
-            if ($user['status'] != 1) {
-                $this->error(__('Account is locked'));
-            }
-            //如果已经有账号则直接登录
-            $ret = $this->auth->direct($user['id']);
-
-            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 = [
-                'code' => $code,
-                'openid' => $openid,
-                'createtime' => time(),
-            ];
-            $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);
-            }
+        if(!empty($this->auth->wechat_openid)){
+            //$this->error('已经绑定了微信号');  //让openid能修改,不报错了
+        }
 
-            //直接返回
-            $userInfo = [];
-            $userInfo['is_register'] = 1;
-            $userInfo['code'] = $code;
-            $this->success('获取信息成功', $userInfo);
+        $otherUserWhere['wechat_openid'] = $wechat_openid;
+        $otherUserWhere['id'] = ['neq',$this->auth->id];
+        if (\app\common\model\User::where($otherUserWhere)->find()) {
+            $this->error('该微信号已被其他用户绑定');
         }
 
+        $user = $this->auth->getUser();
+        $user->wechat_openid = $wechat_openid;
+        $user->save();
+
+        $this->success('绑定成功',$this->auth->getUserinfo_simple());
     }
 
-    /**
-     * 微信注册来的,绑定手机号
-     *
-     * @ApiMethod (POST)
-     * @param string $mobile   手机号
-     * @param string $captcha 验证码
-     */
-    public function bindmobile()
-    {
-        $mobile = $this->request->param('mobile');
-        $captcha = $this->request->param('captcha');
-        $code = $this->request->param('code');
+    //微信登录+注册
+    public function wechatlogin(){
 
-        if (!$mobile || !$captcha || !$code) {
+        $code = $this->request->param('code','');
+        if(!$code){
             $this->error(__('Invalid parameters'));
         }
-        if (!Validate::regex($mobile, "^1\d{10}$")) {
-            $this->error(__('Mobile is incorrect'));
+        //微信
+        $wechat = new Wechat();
+        $wxuserinfo = $wechat->getAccessToken($code);
+
+        if(!$wxuserinfo){
+            $this->error('openid获取失败');
         }
-        $result = Sms::check($mobile, $captcha, 'changemobile');
-        if (!$result) {
-            $this->error(__('Captcha is incorrect'));
+        if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
+            $this->error('openid获取失败');
         }
 
-        $wechatCodeWhere['code'] = $code;
-        $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
-        if (empty($wechatCode)) {
-            $this->error('请先微信登录');
-        }
+        $openid = $wxuserinfo['openid'];
 
-        //检查appid绑定的用户
-        $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
+        //
+        $user = Db::name('user')->where('wechat_openid',$openid)->find();
         if ($user) {
             if ($user['status'] == -1) {
                 $this->error('账户已注销');
@@ -250,65 +221,56 @@ class User extends Api
             }
             //如果已经有账号则直接登录
             $ret = $this->auth->direct($user['id']);
-            $this->success('success',$this->auth->getUserinfo_simple());
-        }
 
-        //新的openid用户
-        $where = [];
-        $where['mobile'] = $mobile;
-        $userData = Db::name('user')->where($where)->find();//老用户
-        if (!empty($userData)) {
-            if (empty($userData['wechat_openid'])) {
-                Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
-            } else {
-                if ($userData['wechat_openid'] != $wechatCode['openid']) {
-                    $this->error('该手机号已被其他用户绑定');
-                }
-            }
-            $ret = $this->auth->direct($userData['id']);
         } else {
             $extend = [
-                'wechat_openid' => $wechatCode['openid'],
                 'plat_unique_id' => input('plat_unique_id',''),
                 'plat_from'      => input('plat_from',''),
             ];
-            $ret = $this->auth->register('', '','', $mobile, $extend);
+            $ret = $this->auth->openid_register($openid,$extend);
         }
-        if (!$ret) {
+        if ($ret) {
+            $data = $this->auth->getUserinfo_simple();
+            $this->success(__('Logged in successful'), $data);
+        } else {
             $this->error($this->auth->getError());
         }
-
-        $this->success('success',$this->auth->getUserinfo_simple());
-
     }
 
     /**
-     * 手机注册来的,绑定微信
+     * 微信注册来的,绑定手机号
      *
      * @ApiMethod (POST)
-     * @param string $wechat_openid
+     * @param string $mobile   手机号
+     * @param string $captcha 验证码
      */
-    public function bindopenid()
+    public function bindmobile()
     {
-        $wechat_openid = input('wechat_openid');
-        if (!$wechat_openid) {
+        $mobile = $this->request->request('mobile');
+        $captcha = $this->request->request('captcha');
+
+        if(!empty($this->auth->mobile)){
+            $this->error('已经绑定了手机号');
+        }
+        if (!$mobile || !$captcha) {
             $this->error(__('Invalid parameters'));
         }
-
-        if(!empty($this->auth->wechat_openid)){
-            //$this->error('已经绑定了微信号');
+        if (!Validate::regex($mobile, "^1\d{10}$")) {
+            $this->error(__('Mobile is incorrect'));
         }
-
-        $otherUserWhere['wechat_openid'] = $wechat_openid;
-        $otherUserWhere['id'] = ['neq',$this->auth->id];
-        if (\app\common\model\User::where($otherUserWhere)->find()) {
-            $this->error('该微信号已被其他用户绑定');
+        if (\app\common\model\User::where('mobile', $mobile)->find()) {
+            $this->error('该手机号已被其他用户绑定');
+        }
+        $result = Sms::check($mobile, $captcha, 'changemobile');
+        if (!$result) {
+            $this->error(__('Captcha is incorrect'));
         }
 
         $user = $this->auth->getUser();
-        $user->wechat_openid = $wechat_openid;
+        $user->mobile = $mobile;
         $user->save();
 
+        Sms::flush($mobile, 'changemobile');
         $this->success('绑定成功',$this->auth->getUserinfo_simple());
     }
 
@@ -844,6 +806,142 @@ class User extends Api
 
     //////////////////////////////////////////////////////////////////////////
 
+    //微信登录,预先假注册
+    public function wechatlogin_fake(){
+        $code = $this->request->param('code','');
+        if(!$code){
+            $this->error(__('Invalid parameters'));
+        }
+        //微信
+        $wechat = new Wechat();
+        $wxuserinfo = $wechat->getAccessToken($code);
+
+        if(!$wxuserinfo){
+            $this->error('openid获取失败');
+        }
+        if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
+            $this->error('openid获取失败');
+        }
+
+        $openid = $wxuserinfo['openid'];
+
+        //检查用户
+        $user = Db::name('user')->where('wechat_openid',$openid)->find();
+        if ($user) {
+            if ($user['status'] == -1) {
+                $this->error('账户已注销');
+            }
+            if ($user['status'] != 1) {
+                $this->error(__('Account is locked'));
+            }
+            //如果已经有账号则直接登录
+            $ret = $this->auth->direct($user['id']);
+
+            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 = [
+                'code' => $code,
+                'openid' => $openid,
+                'createtime' => time(),
+            ];
+            $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);
+            }
+
+            //直接返回
+            $userInfo = [];
+            $userInfo['is_register'] = 1;
+            $userInfo['code'] = $code;
+            $this->success('获取信息成功', $userInfo);
+        }
+
+    }
+
+    /**
+     * 微信注册来的,绑定手机号
+     *
+     * @ApiMethod (POST)
+     * @param string $mobile   手机号
+     * @param string $captcha 验证码
+     */
+    public function bindmobile_fake()
+    {
+        $mobile = $this->request->param('mobile');
+        $captcha = $this->request->param('captcha');
+        $code = $this->request->param('code');
+
+        if (!$mobile || !$captcha || !$code) {
+            $this->error(__('Invalid parameters'));
+        }
+        if (!Validate::regex($mobile, "^1\d{10}$")) {
+            $this->error(__('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)) {
+            $this->error('请先微信登录');
+        }
+
+        //检查appid绑定的用户
+        $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
+        if ($user) {
+            if ($user['status'] == -1) {
+                $this->error('账户已注销');
+            }
+            if ($user['status'] != 1) {
+                $this->error(__('Account is locked'));
+            }
+            //如果已经有账号则直接登录
+            $ret = $this->auth->direct($user['id']);
+            $this->success('success',$this->auth->getUserinfo_simple());
+        }
+
+        //新的openid用户
+        $where = [];
+        $where['mobile'] = $mobile;
+        $userData = Db::name('user')->where($where)->find();//老用户
+        if (!empty($userData)) {
+            if (empty($userData['wechat_openid'])) {
+                Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
+            } else {
+                if ($userData['wechat_openid'] != $wechatCode['openid']) {
+                    $this->error('该手机号已被其他用户绑定');
+                }
+            }
+            $ret = $this->auth->direct($userData['id']);
+        } else {
+            $extend = [
+                'wechat_openid' => $wechatCode['openid'],
+                'plat_unique_id' => input('plat_unique_id',''),
+                'plat_from'      => input('plat_from',''),
+            ];
+            $ret = $this->auth->register('', '','', $mobile, $extend);
+        }
+        if (!$ret) {
+            $this->error($this->auth->getError());
+        }
+
+        $this->success('success',$this->auth->getUserinfo_simple());
+
+    }
+
 
     /**
      * 运营商一键登录

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

@@ -326,6 +326,93 @@ class Auth
         return true;
     }
 
+    public function openid_register($wechat_openid = '', $extend = [])
+    {
+        //检测设备号是否拉黑
+        if(isset($extend['plat_unique_id']) && !empty($extend['plat_unique_id'])){
+            $check = Db::name('shebei_black')->where('plat_unique_id',$extend['plat_unique_id'])->find();
+            if($check){
+                $this->setError('设备已拉黑,禁止注册');
+                return false;
+            }
+        }
+
+
+        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' => '',
+            'bio'   => '美好生活从这里开始~',
+        ];
+        $params = array_merge($data, [
+            'nickname'  => $this->get_rand_nick_name(),
+            '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->introcode = 100000 + $user->id;
+            $this->_user->save();
+
+            //设置Token
+            $this->_token = Random::uuid();
+            Token::set($this->_token, $user->id, $this->keeptime);
+
+            //设置登录状态
+            $this->_logined = true;
+
+            //注册钱包
+            $rs1 = Db::name('user_wallet')->insertGetId(['user_id'=>$user->id]);
+            if(!$rs1){
+                $this->setError('注册失败');
+                Db::rollback();
+                return false;
+            }
+            //注册用户活跃
+            $rs2 = Db::name('user_active')->insertGetId(['user_id'=>$user->id]);
+            if(!$rs2){
+                $this->setError('注册失败');
+                Db::rollback();
+                return false;
+            }
+            //注册用户权限
+            $rs3 = Db::name('user_power')->insertGetId(['user_id'=>$user->id]);
+            if(!$rs3){
+                $this->setError('注册失败');
+                Db::rollback();
+                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;
+    }
 
     /**
      * 用户登录