Browse Source

登录与注册

lizhen_gitee 8 months ago
parent
commit
4c651f9091
3 changed files with 150 additions and 130 deletions
  1. 78 105
      application/api/controller/User.php
  2. 61 22
      application/common/library/Auth.php
  3. 11 3
      application/config.php

+ 78 - 105
application/api/controller/User.php

@@ -14,7 +14,7 @@ use think\Validate;
  */
 class User extends Api
 {
-    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
+    protected $noNeedLogin = ['login', 'mobilelogin', 'wxmini_login', 'resetpwd',  'changemobile'];
     protected $noNeedRight = '*';
 
     public function _initialize()
@@ -80,7 +80,7 @@ class User extends Api
         }
         $user = \app\common\model\User::getByMobile($mobile);
         if ($user) {
-            if ($user->status != 'normal') {
+            if ($user->status != 1) {
                 $this->error(__('Account is locked'));
             }
             //如果已经有账号则直接登录
@@ -97,44 +97,6 @@ class User extends Api
         }
     }
 
-    /**
-     * 注册会员
-     *
-     * @ApiMethod (POST)
-     * @param string $username 用户名
-     * @param string $password 密码
-     * @param string $email    邮箱
-     * @param string $mobile   手机号
-     * @param string $code     验证码
-     */
-    public function register()
-    {
-        $username = $this->request->post('username');
-        $password = $this->request->post('password');
-        $email = $this->request->post('email');
-        $mobile = $this->request->post('mobile');
-        $code = $this->request->post('code');
-        if (!$username || !$password) {
-            $this->error(__('Invalid parameters'));
-        }
-        if ($email && !Validate::is($email, "email")) {
-            $this->error(__('Email is incorrect'));
-        }
-        if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
-            $this->error(__('Mobile is incorrect'));
-        }
-        $ret = Sms::check($mobile, $code, 'register');
-        if (!$ret) {
-            $this->error(__('Captcha is incorrect'));
-        }
-        $ret = $this->auth->register($username, $password, $email, $mobile, []);
-        if ($ret) {
-            $data = ['userinfo' => $this->auth->getUserinfo()];
-            $this->success(__('Sign up successful'), $data);
-        } else {
-            $this->error($this->auth->getError());
-        }
-    }
 
     /**
      * 退出登录
@@ -185,40 +147,6 @@ class User extends Api
         $this->success();
     }
 
-    /**
-     * 修改邮箱
-     *
-     * @ApiMethod (POST)
-     * @param string $email   邮箱
-     * @param string $captcha 验证码
-     */
-    public function changeemail()
-    {
-        $user = $this->auth->getUser();
-        $email = $this->request->post('email');
-        $captcha = $this->request->post('captcha');
-        if (!$email || !$captcha) {
-            $this->error(__('Invalid parameters'));
-        }
-        if (!Validate::is($email, "email")) {
-            $this->error(__('Email is incorrect'));
-        }
-        if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
-            $this->error(__('Email already exists'));
-        }
-        $result = Ems::check($email, $captcha, 'changeemail');
-        if (!$result) {
-            $this->error(__('Captcha is incorrect'));
-        }
-        $verification = $user->verification;
-        $verification->email = 1;
-        $user->verification = $verification;
-        $user->email = $email;
-        $user->save();
-
-        Ems::flush($email, 'changeemail');
-        $this->success();
-    }
 
     /**
      * 修改手机号
@@ -255,37 +183,6 @@ class User extends Api
         $this->success();
     }
 
-    /**
-     * 第三方登录
-     *
-     * @ApiMethod (POST)
-     * @param string $platform 平台名称
-     * @param string $code     Code码
-     */
-    public function third()
-    {
-        $url = url('user/index');
-        $platform = $this->request->post("platform");
-        $code = $this->request->post("code");
-        $config = get_addon_config('third');
-        if (!$config || !isset($config[$platform])) {
-            $this->error(__('Invalid parameters'));
-        }
-        $app = new \addons\third\library\Application($config);
-        //通过code换access_token和绑定会员
-        $result = $app->{$platform}->getUserInfo(['code' => $code]);
-        if ($result) {
-            $loginret = \addons\third\library\Service::connect($platform, $result);
-            if ($loginret) {
-                $data = [
-                    'userinfo'  => $this->auth->getUserinfo(),
-                    'thirdinfo' => $result
-                ];
-                $this->success(__('Logged in successful'), $data);
-            }
-        }
-        $this->error(__('Operation failed'), $url);
-    }
 
     /**
      * 重置密码
@@ -345,4 +242,80 @@ class User extends Api
             $this->error($this->auth->getError());
         }
     }
+
+    ///////////////////////////////////////////
+
+    /**
+     * 微信小程序登录+注册
+     */
+    public function wxmini_login() {
+        $code = input('code');
+        if (!$code) {
+            $this->error(__('Invalid parameters'));
+        }
+
+        $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'])) {
+            $this->error('用户openid获取失败',$openidInfo);
+        }
+
+        $openid  = $openidInfo['openid'];
+        if (!$openid) {
+            $this->error('用户openid获取失败');
+        }
+
+        //用户信息
+        $userInfo = Db::name('user')->where(['mini_openid'=>$openid])->find();
+
+        if($userInfo) {
+            if ($userInfo['status'] != 1) {
+                $this->error(__('Account is locked'));
+            }
+            //如果已经有账号则直接登录
+            $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("登录失败!");
+        }
+
+    }
+
+    /**
+     * json 请求
+     * @param $url
+     * @return mixed
+     */
+    private function getJson($url){
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        $output = curl_exec($ch);
+        curl_close($ch);
+        return json_decode($output, true);
+    }
+
+
+    //用户详细资料
+    public function getUserinfo(){
+        $info = $this->auth->getUserinfo();
+
+        $this->success(__('success'),$info);
+    }
 }

File diff suppressed because it is too large
+ 61 - 22
application/common/library/Auth.php


+ 11 - 3
application/config.php

@@ -328,9 +328,17 @@ return [
     ],
 
     //后台网站配置
-    'newconfiggroup' =>
-        [
+    'newconfiggroup' =>[
             'website' => '全站基础配置',
-        ],
+    ],
+
+    //微信小程序
+    'wxMiniProgram' => [
+        'appid' => 'wxbc902cf8a5354c32',
+        'secret' => '912822fc14fc9f7ca0b0d8294d26f113',
+    ],
+
+    //默认头像
+    'default_avatar' => '',
 
 ];

Some files were not shown because too many files changed in this diff