Jelajahi Sumber

用户登录+注册

lizhen_gitee 6 bulan lalu
induk
melakukan
8400049d79
2 mengubah file dengan 19 tambahan dan 5 penghapusan
  1. 18 4
      application/api/controller/User.php
  2. 1 1
      application/extra/site.php

+ 18 - 4
application/api/controller/User.php

@@ -123,19 +123,32 @@ class User extends Api
      * code得到注册手机号,此手机号登录+注册
      */
     public function wxmini_regmobile_login(){
-        $code = input('code');
-        if (!$code) {
+        $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);
 
+        $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='.$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获取失败');
+        }
+
         $mobile = $getuserphonenumber['phone_info']['purePhoneNumber'];
 
         $userInfo = Db::name('user')->where('mobile',$mobile)->find();
@@ -147,7 +160,8 @@ class User extends Api
             //如果已经有账号则直接登录
             $res = $this->auth->direct($userInfo['id']);
         } else {
-            $res = $this->auth->register('', '', '',$mobile, []);
+            $extend = ['wxmini_openid'=>$openid];
+            $res = $this->auth->register('', '', '',$mobile, $extend);
         }
         if($res) {
             $this->success("登录成功!",$this->auth->getUserinfo());

+ 1 - 1
application/extra/site.php

@@ -45,7 +45,7 @@ return array (
   ),
   'apisite_switch' => '1',
   'apisite_notice' => '全站维护中',
-  'kefu_mobile' => '13333333333',
+  'kefu_mobile' => '13018525577',
   'order_refund_rule' => '未使用、未激活可随时申请退订,订单不支持部分退。 
 如需改期,请申请退订后重新预订。',
   'kefu_qrcode' => '/uploads/20240906/aeeb86c804d5097559fe2948b8258155.png',