Quellcode durchsuchen

修复:微信小程序端新用户注册时未记录登录态

Yahoo vor 4 Jahren
Ursprung
Commit
43b5bb6742

+ 16 - 18
app/api/service/passport/Login.php

@@ -80,7 +80,7 @@ class Login extends BaseService
         $userInfo = !empty($userId) ? UserModel::detail($userId) : null;
 
         // 用户信息存在, 更新登录信息
-        if (!empty($userInfo) && $userInfo instanceof UserModel) {
+        if (!empty($userInfo)) {
             // 更新用户登录信息
             $this->updateUser($userInfo, true, $form['partyData']);
             // 记录登录态
@@ -93,15 +93,15 @@ class Login extends BaseService
         if ($setting['isForceBindMpweixin']) {
             throwError('当前用户未绑定手机号', null, ['isBindMobile' => true]);
         }
-        // 后台设置了强制绑定手机号, 直接保存新用户
+        // 后台未开启强制绑定手机号, 直接保存新用户
         if (!$setting['isForceBindMpweixin']) {
             // 用户不存在: 创建一个新用户
             $this->createUser('', true, $form['partyData']);
             // 保存第三方用户信息
             $this->createUserOauth($this->getUserId(), true, $form['partyData']);
-            return true;
         }
-        return true;
+        // 记录登录态
+        return $this->setSession();
     }
 
     /**
@@ -139,19 +139,18 @@ class Login extends BaseService
      * @param int $userId 用户ID
      * @param bool $isParty 是否为第三方用户
      * @param array $partyData 第三方用户数据
-     * @return bool
+     * @return void
      * @throws BaseException
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    private function createUserOauth(int $userId, bool $isParty, array $partyData = []): bool
+    private function createUserOauth(int $userId, bool $isParty, array $partyData = []): void
     {
         if ($isParty) {
             $Oauth = new PartyService;
-            return $Oauth->createUserOauth($userId, $partyData);
+            $Oauth->createUserOauth($userId, $partyData);
         }
-        return true;
     }
 
     /**
@@ -174,22 +173,23 @@ class Login extends BaseService
     /**
      * 自动登录注册
      * @param array $data
-     * @return bool
+     * @return void
      * @throws \think\Exception
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    private function register(array $data): bool
+    private function register(array $data): void
     {
         // 查询用户是否已存在
         // 用户存在: 更新用户登录信息
         $userInfo = UserModel::detail(['mobile' => $data['mobile']]);
         if ($userInfo) {
-            return $this->updateUser($userInfo, $data['isParty'], $data['partyData']);
+            $this->updateUser($userInfo, $data['isParty'], $data['partyData']);
+            return;
         }
         // 用户不存在: 创建一个新用户
-        return $this->createUser($data['mobile'], $data['isParty'], $data['partyData']);
+        $this->createUser($data['mobile'], $data['isParty'], $data['partyData']);
     }
 
     /**
@@ -197,13 +197,13 @@ class Login extends BaseService
      * @param string $mobile 手机号
      * @param bool $isParty 是否存在第三方用户信息
      * @param array $partyData 用户信息(第三方)
-     * @return bool
+     * @return void
      * @throws \think\Exception
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    private function createUser(string $mobile, bool $isParty, array $partyData = []): bool
+    private function createUser(string $mobile, bool $isParty, array $partyData = []): void
     {
         // 用户信息
         $data = [
@@ -223,7 +223,6 @@ class Login extends BaseService
         $status = $model->save($data);
         // 记录用户信息
         $this->userInfo = $model;
-        return $status;
     }
 
     /**
@@ -231,9 +230,9 @@ class Login extends BaseService
      * @param UserModel $userInfo
      * @param bool $isParty 是否存在第三方用户信息
      * @param array $partyData 用户信息(第三方)
-     * @return bool
+     * @return void
      */
-    private function updateUser(UserModel $userInfo, bool $isParty, array $partyData = []): bool
+    private function updateUser(UserModel $userInfo, bool $isParty, array $partyData = []): void
     {
         // 用户信息
         $data = [
@@ -250,7 +249,6 @@ class Login extends BaseService
         $status = $userInfo->save($data) !== false;
         // 记录用户信息
         $this->userInfo = $userInfo;
-        return $status;
     }
 
     /**

+ 33 - 0
app/common/library/wechat/ErrorCode.php

@@ -0,0 +1,33 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\common\library\wechat;
+
+/**
+ * error code 说明.
+ * <ul>
+ *    <li>-41001: encodingAesKey 非法</li>
+ *    <li>-41003: aes 解密失败</li>
+ *    <li>-41004: 解密后得到的buffer非法</li>
+ *    <li>-41005: base64加密失败</li>
+ *    <li>-41016: base64解密失败</li>
+ * </ul>
+ */
+class ErrorCode
+{
+    public static $OK = 0;
+    public static $IllegalAesKey = -41001;
+    public static $IllegalIv = -41002;
+    public static $IllegalBuffer = -41003;
+    public static $DecodeBase64Error = -41004;
+}
+

+ 65 - 0
app/common/library/wechat/WXBizDataCrypt.php

@@ -0,0 +1,65 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\common\library\wechat;
+
+class WXBizDataCrypt
+{
+    private $appid;
+    private $sessionKey;
+
+    /**
+     * 构造函数
+     * @param string $appid string 小程序的appid
+     * @param string $sessionKey string 用户在小程序登录后获取的会话密钥
+     */
+    public function __construct(string $appid, string $sessionKey)
+    {
+        $this->sessionKey = $sessionKey;
+        $this->appid = $appid;
+    }
+
+    /**
+     * 检验数据的真实性,并且获取解密后的明文.
+     * @param string $encryptedData 加密的用户数据
+     * @param string $iv 与用户数据一同返回的初始向量
+     * @param mixed $content 解密后的原文
+     * @return int 成功0,失败返回对应的错误码
+     */
+    public function decryptData(string $encryptedData, string $iv, &$content): int
+    {
+        if (strlen($this->sessionKey) != 24) {
+            return ErrorCode::$IllegalAesKey;
+        }
+        if (strlen($iv) != 24) {
+            return ErrorCode::$IllegalIv;
+        }
+        $aesKey = base64_decode($this->sessionKey);
+        $aesIV = base64_decode($iv);
+        $aesCipher = base64_decode($encryptedData);
+        $result = openssl_decrypt($aesCipher, 'AES-128-CBC', $aesKey, 1, $aesIV);
+
+        if (empty($result)) {
+            return ErrorCode::$IllegalBuffer;
+        }
+        $resultArr = json_decode($result, true);
+        if (empty($resultArr)) {
+            return ErrorCode::$IllegalBuffer;
+        }
+        if ($resultArr['watermark']['appid'] != $this->appid) {
+            return ErrorCode::$IllegalBuffer;
+        }
+        $content = $resultArr;
+        return ErrorCode::$OK;
+    }
+}
+

+ 1 - 1
app/common/model/User.php

@@ -86,7 +86,7 @@ class User extends BaseModel
      * 获取用户信息
      * @param $where
      * @param array $with
-     * @return array|\think\Model|null
+     * @return static|array|false|null
      */
     public static function detail($where, array $with = [])
     {

+ 1 - 1
cores/BaseModel.php

@@ -57,7 +57,7 @@ abstract class BaseModel extends Model
      * 查找单条记录
      * @param $data
      * @param array $with
-     * @return array|false|Model|null
+     * @return array|false|static|null
      */
     public static function get($data, array $with = [])
     {