Browse Source

微信注册手机号,用来登录

lizhen_gitee 2 months ago
parent
commit
e9c5da3ec9

+ 51 - 0
application/api/controller/User.php

@@ -25,6 +25,57 @@ class User extends Api
 
     }
 
+    /**
+     * 微信小程序登录+注册
+     * code得到注册手机号,此手机号登录+注册
+     */
+    public function wxmini_regmobile_login(){
+        $code     = input('code');
+        $opencode = input('opencode');
+        if (!$code || !$opencode) {
+            $this->error(__('Invalid parameters'));
+        }
+
+        $config = config('wxMiniProgram');
+        $wechat = new Wechat($config['appid'],$config['secret']);
+
+        $getuserphonenumber = $wechat->getuserphonenumber($code);
+        if(!isset($getuserphonenumber['phone_info']['purePhoneNumber'])){
+            $this->error('授权获取手机号失败');
+        }
+
+        //获取openid
+        $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$opencode.'&grant_type=authorization_code';
+        $openidInfo = $this->getJson($getopenid);
+        if(!isset($openidInfo['openid'])) {
+            $this->error('用户openid获取失败',$openidInfo);
+        }
+
+        $openid  = $openidInfo['openid'];
+        if (!$openid) {
+            $this->error('用户openid获取失败');
+        }
+
+        $mobile = $getuserphonenumber['phone_info']['purePhoneNumber'];
+
+        $userInfo = Db::name('user')->where('mobile',$mobile)->find();
+        // 判断用户是否已经存在
+        if($userInfo) { // 登录
+            if ($userInfo['status'] != 1) {
+                $this->error(__('Account is locked'));
+            }
+            //如果已经有账号则直接登录
+            $res = $this->auth->direct($userInfo['id']);
+        } else {
+            $extend = ['wxmini_openid'=>$openid];
+            $res = $this->auth->register('', '', '',$mobile, $extend);
+        }
+        if($res) {
+            $this->success("登录成功!",$this->auth->getUserinfo());
+        } else {
+            $this->error($this->auth->getError());
+        }
+    }
 
     //微信登录,预先假注册
     public function wechatlogin(){

+ 17 - 2
application/common/library/Auth.php

@@ -51,6 +51,18 @@ class Auth
     }
 
     /**
+     * 生成不重复的随机数字字母组合
+     */
+    function getUinqueNo($length = 8, $nos = [])
+    {
+        $newid = Random::build("alnum", $length);
+        if (in_array($newid, $nos)) {
+            $newid = $this->getUinqueNo($length, $nos);
+        }
+        return $newid;
+    }
+
+    /**
      * 获取User模型
      * @return User
      */
@@ -164,6 +176,8 @@ class Auth
         $ip = request()->ip();
         $time = time();
 
+        $introcode = User::column("introcode");
+
         $data = [
 //            'username' => $username,
 //            'password' => $password,
@@ -172,6 +186,7 @@ class Auth
 //            'level'    => 1,
 //            'score'    => 0,
             'avatar'   => config('site.user_default_avatar'),
+            'introcode' => $this->getUinqueNo(8, $introcode),
         ];
         $params = array_merge($data, [
 //            'nickname'  => preg_match("/^1[3-9]{1}\d{9}$/", $username) ? substr_replace($username, '****', 3, 4) : $username,
@@ -221,13 +236,13 @@ class Auth
             }*/
 
             //腾讯im注册用户
-            $tenim = new Tenim();
+            /*$tenim = new Tenim();
             $rs = $tenim->register('user'.$user->id,$params['nickname'],'');
             if($rs !== true){
                 $this->setError($rs);
                 Db::rollback();
                 return false;
-            }
+            }*/
 
             //注册成功的事件
             Hook::listen("user_register_successed", $this->_user, $data);

+ 19 - 3
application/common/library/Wechat.php

@@ -19,7 +19,7 @@ class Wechat
 
     public function __construct()
     {
-        $wxConfig = config('wechat_app_login');
+        $wxConfig = config('wxMiniProgram');
         $this->app_id = $wxConfig['appid'];
         $this->app_secret = $wxConfig['secret'];
     }
@@ -120,7 +120,23 @@ class Wechat
         return [];
     }
 
-   /* public function getPublicAccessToken(){
+    //获取微信注册手机号
+    public function getuserphonenumber($code = ''){
+        $access_token = $this->getPublicAccessToken();
+
+        $params = [
+            'code' => $code,
+        ];
+
+        $ret = Http::sendRequest('https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$access_token, json_encode($params), 'POST');
+
+        if ($ret['ret']) {
+            $ar = json_decode($ret['msg'], true);
+            return $ar;
+        }
+        return [];
+    }
+    public function getPublicAccessToken(){
         $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->app_id.'&secret='.$this->app_secret);
 
         if ($ret['ret']) {
@@ -128,7 +144,7 @@ class Wechat
 
             return $ar['access_token'];
         }
-    }*/
+    }
 
     //{"errcode":0,"errmsg":"ok","msgid":2054095443608862720}
     /*public function send($ac,$openid,$first,$keyword1,$keyword2,$keyword3,$remark,$color){

+ 7 - 0
application/config.php

@@ -397,6 +397,13 @@ return [
         'secret' => '96cae827c8a3941cdd61ad4cc1376e17',
     ],
 
+    //微信小程序 水饺正式
+    'wxMiniProgram' => [
+        'appid'  => 'wx1b91bcadaa2adb53',
+        'secret' => '96cae827c8a3941cdd61ad4cc1376e17',
+    ],
+
+
     //微信支付退款用,和epay保持一直, 水饺正式
     'wechatpay' => [
         'app_id' => 'wx1b91bcadaa2adb53',  // APP appid