From a6898c0041f7b5b976ebebdc89de3dd147001d26 Mon Sep 17 00:00:00 2001 From: Leo <98382335+gaoziman@users.noreply.github.com> Date: Mon, 7 Jul 2025 09:16:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(auth):=20=E4=BC=98=E5=8C=96=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=B5=81=E7=A8=8B=E5=92=8C=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优化登录失败时的错误抛出机制,确保上层组件可以捕获 - 增加登录成功的统一提示信息 - 完善获取用户信息失败时的错误处理 - 简化登录成功提示逻辑,移除重复判断 --- src/store/auth.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/store/auth.ts b/src/store/auth.ts index 95673ac..1192334 100644 --- a/src/store/auth.ts +++ b/src/store/auth.ts @@ -1,6 +1,7 @@ import { router } from '@/router' import { fetchLogin, fetchLoginUserInfo, fetchLogout } from '@/service/api/auth' import { local } from '@/utils' +import { coiMsgSuccess } from '@/utils/coi' import { useRouteStore } from './router' import { useTabStore } from './tab' @@ -66,8 +67,10 @@ export const useAuthStore = defineStore('auth-store', { async login(loginName: string, password: string, codeKey: string, securityCode: string, rememberMe = false) { try { const { isSuccess, data } = await fetchLogin({ loginName, password, codeKey, securityCode, rememberMe }) - if (!isSuccess) - return + if (!isSuccess) { + // 登录失败时抛出错误,让上层组件处理验证码刷新 + throw new Error('登录失败') + } // 保存Token local.set('accessToken', data.tokenValue) @@ -75,8 +78,10 @@ export const useAuthStore = defineStore('auth-store', { // 获取用户信息 const userInfoResult = await fetchLoginUserInfo() - if (!userInfoResult.isSuccess) - return + if (!userInfoResult.isSuccess) { + // 获取用户信息失败时也抛出错误 + throw new Error('获取用户信息失败') + } // 处理登录信息 - 转换后端返回的数据结构 const userInfo = { @@ -93,6 +98,8 @@ export const useAuthStore = defineStore('auth-store', { } catch (e) { console.warn('[Login Error]:', e) + // 重新抛出错误,确保上层组件能够捕获 + throw e } }, @@ -112,6 +119,10 @@ export const useAuthStore = defineStore('auth-store', { // 进行重定向跳转 const route = unref(router.currentRoute) const query = route.query as { redirect: string } + + // 登录成功提示 + coiMsgSuccess('登录成功!') + router.push({ path: query.redirect || '/', })