Browse Source

登录与注册

lizhen_gitee 8 months ago
parent
commit
9fe051b8e5
1 changed files with 42 additions and 13 deletions
  1. 42 13
      application/api/controller/User.php

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

@@ -248,6 +248,45 @@ class User extends Api
 
     /**
      * 微信小程序登录+注册
+     * code得到注册手机号
+     */
+    public function wxregmobile_login(){
+        $code = input('code');
+        if (!$code) {
+            $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('授权获取手机号失败');
+        }
+
+        $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 {
+            $res = $this->auth->register('', '', '',$mobile, []);
+        }
+        if($res) {
+            $this->success("登录成功!",$this->auth->getUserinfo());
+        } else {
+            $this->error($this->auth->getError());
+        }
+    }
+
+    /**
+     * 微信小程序登录+注册
+     * code得到openid
      */
     public function wxmini_login() {
         $code = input('code');
@@ -255,7 +294,7 @@ class User extends Api
             $this->error(__('Invalid parameters'));
         }
 
-        /*$config = config('wxMiniProgram');
+        $config = config('wxMiniProgram');
         $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
         $openidInfo = $this->getJson($getopenid);
         if(!isset($openidInfo['openid'])) {
@@ -265,8 +304,7 @@ class User extends Api
         $openid  = $openidInfo['openid'];
         if (!$openid) {
             $this->error('用户openid获取失败');
-        }*/
-        $openid = 'asdf';
+        }
 
         //用户信息
         $userInfo = Db::name('user')->where(['mini_openid'=>$openid])->find();
@@ -278,21 +316,12 @@ class User extends Api
             //如果已经有账号则直接登录
             $res = $this->auth->direct($userInfo['id']);
         } else {
-            // 注册
-            /*$extend = [
-                'mini_openid' => $openid,
-            ];*/
-
             $res = $this->auth->openid_register($openid);
-            if (!$res) {
-                $this->error($this->auth->getError());
-            }
-
         }
         if($res) {
             $this->success("登录成功!",$this->auth->getUserinfo());
         } else {
-            $this->error("登录失败!");
+            $this->error($this->auth->getError());
         }
 
     }