feat(auth): 优化登录流程和错误处理
- 优化登录失败时的错误抛出机制,确保上层组件可以捕获 - 增加登录成功的统一提示信息 - 完善获取用户信息失败时的错误处理 - 简化登录成功提示逻辑,移除重复判断
This commit is contained in:
parent
fb1b2583bf
commit
a6898c0041
@ -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 || '/',
|
||||
})
|
||||
|
||||
Loading…
Reference in New Issue
Block a user