Browse Source

feat:用户端-微信登录

刘学玺 4 months ago
parent
commit
01d650b40b
2 changed files with 12 additions and 8 deletions
  1. 6 6
      app/Services/Client/AccountService.php
  2. 6 2
      app/Services/Client/WechatService.php

+ 6 - 6
app/Services/Client/AccountService.php

@@ -233,7 +233,7 @@ class AccountService
     private function storeVerifyCode(string $mobile, int $code): void
     {
         // 构建缓存键名(verify_code:手机号)
-        $key = self::VERIFY_CODE_PREFIX . $mobile;
+        $key = self::VERIFY_CODE_PREFIX.$mobile;
 
         // 将验证码存入缓存,并设置过期时间
         Cache::put($key, $code, self::VERIFY_CODE_EXPIRE);
@@ -284,7 +284,7 @@ class AccountService
     private function verifyCode(string $mobile, string $code): void
     {
         // 使用手机号和前缀构建缓存键,获取存储的验证码
-        $cacheCode = Cache::get(self::VERIFY_CODE_PREFIX . $mobile);
+        $cacheCode = Cache::get(self::VERIFY_CODE_PREFIX.$mobile);
 
         // 验证码不存在或不匹配则抛出业务异常
         if (! $cacheCode || $cacheCode != $code) {
@@ -352,10 +352,10 @@ class AccountService
         // 创建新的用户记录
         return MemberUser::create([
             'state' => UserStatus::OPEN->value,  // 设置用户状态为开启
-            'register_area' => request()->header('area_code'),  // 从请求头获取注册区域
+            'register_area' => request()->header('area_code') ?? '0000',  // 从请求头获取注册区域
             'nickname' => $userInfo['nickname'] ?? null,  // 设置微信昵称,可选
             'avatar' => $userInfo['avatar'] ?? null,      // 设置微信头像,可选
-            'gender' => $userInfo['gender'] ?? null,      // 设置性别,可选
+            'gender' => $userInfo['gender'] ?? 0,      // 设置性别,可选
         ]);
     }
 
@@ -583,8 +583,8 @@ class AccountService
      * 3. 更新用户手机号
      * 4. 返回更新后的用户信息
      *
-     * @param string $mobile 新手机号
-     * @param string $code 验证码
+     * @param  string  $mobile  新手机号
+     * @param  string  $code  验证码
      * @return array 包含成功消息和更新后的用户信息
      *
      * @throws BusinessException 手机号已被使用时抛出异常

+ 6 - 2
app/Services/Client/WechatService.php

@@ -75,6 +75,9 @@ class WechatService
             // 获取用户信息
             $user = $this->socialite->userFromCode($code);
 
+            // 验证openid
+            abort_if(empty($user->openid), 400, '获取微信openid失败');
+
             // 整理用户信息
             $userInfo = $this->formatUserInfo($user);
 
@@ -84,7 +87,7 @@ class WechatService
             }
 
             // 执行登录
-            return $this->accountService->wxLogin($user->openid, $userInfo);
+            return $this->accountService->wxLogin($user->getId(), $userInfo);
         } catch (BusinessException $e) {
             throw $e;
         } catch (\Exception $e) {
@@ -92,6 +95,7 @@ class WechatService
                 'code' => $code,
                 'state' => $state,
                 'invite_code' => $inviteCode,
+                'user_info' => $user ?? null,
             ]);
             throw new BusinessException('微信授权失败,请稍后重试');
         }
@@ -179,7 +183,7 @@ class WechatService
     /**
      * 格式化性别信息
      *
-     * @param  int|null  $gender  微信返回性别值:1为男性,2为女性,0为未知
+     * @param  int|null  $gender  微信返回��性别值:1为男性,2为女性,0为未知
      * @return string|null male/female/null
      */
     protected function formatGender(?int $gender): ?string