Browse Source

用户相关接口,微信登录与注册

lizhen_gitee 6 months ago
parent
commit
b421a03b1a
3 changed files with 147 additions and 5 deletions
  1. 138 1
      application/api/controller/User.php
  2. 1 1
      application/common/library/Auth.php
  3. 8 3
      application/config.php

+ 138 - 1
application/api/controller/User.php

@@ -7,7 +7,7 @@ use app\common\library\Ems;
 use app\common\library\Sms;
 use think\Validate;
 use think\Db;
-
+use app\common\library\Wechat;
 /**
  * 会员接口
  */
@@ -125,6 +125,143 @@ class User extends Api
         }
     }
 
+    //微信登录,预先假注册
+    public function wechatlogin(){
+        $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_simple();
+                $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()
+    {
+
+        $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, 'wechatregister');
+        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','','trim'),
+            ];
+            $ret = $this->auth->register('', '','', $mobile, $extend);
+        }
+        if (!$ret) {
+            $this->error($this->auth->getError());
+        }
+
+        $this->success('success',$this->auth->getUserinfo_simple());
+
+    }
+
     /**
      * 退出登录
      * @ApiMethod (POST)

+ 1 - 1
application/common/library/Auth.php

@@ -401,7 +401,7 @@ class Auth
         $allowFields = $this->getAllowFields();
         $userinfo = array_intersect_key($data, array_flip($allowFields));
         $userinfo = array_merge($userinfo, Token::get($this->_token));
-        
+
         $userinfo['avatar'] = localpath_to_netpath($userinfo['avatar']);
 
         return $userinfo;

+ 8 - 3
application/config.php

@@ -363,13 +363,18 @@ return [
     //后台网站配置
     'newconfiggroup' =>
         [
+            'user'    => '会员配置',
             'website' => '全站基础配置',
         ],
 
     //各种url
-    'pay_notify_url' => 'https://*.huxiukeji.cn',  //支付异步回调域名
+    'pay_notify_url' => 'https://xiaoshan.huxiukeji.cn',  //支付异步回调域名
 
-    //默认头像
-    'user_default_avatar' => '/assets/img/avatar.png',
+
+    //开放平台的,微信登录
+    'wechat_app_login' => [
+        'appid'  => 'wx4af83573cc08f1e8',
+        'secret' => 'c7e104f7c3bae79f3f7b999057aa3b52',
+    ],
 
 ];