u
This commit is contained in:
1 parent
500fab7a80
commit
8f3f6982db
54 files changed
+2076
-1877
No files matched your search
@@ -1,6 +1,5 @@
|
||||
import type { Method } from 'alova'
|
||||
import type { AxiosError } from 'axios'
|
||||
import type { RefreshTokenResult } from '@/types/auth'
|
||||
import { axiosRequestAdapter } from '@alova/adapter-axios'
|
||||
import { createAlova } from 'alova'
|
||||
import { useNavStore } from '@/store/navigation'
|
||||
@@ -11,14 +10,13 @@ import { toast } from '@/utils/toast'
|
||||
enum HttpCode {
|
||||
Success = 200,
|
||||
Unauthorized = 401,
|
||||
RefreshToken = 402,
|
||||
Fail = 500,
|
||||
}
|
||||
|
||||
// 仅保留「无需登录」接口
|
||||
const WhiteList = [
|
||||
'/auth/login/**',
|
||||
'/auth/register/**',
|
||||
'/auth/refresh/token',
|
||||
]
|
||||
|
||||
function isWhiteListed(url: string) {
|
||||
@@ -32,13 +30,14 @@ function isWhiteListed(url: string) {
|
||||
}
|
||||
|
||||
const alovaInstance = createAlova({
|
||||
// baseURL: 'http://192.168.5.6:8086/app',
|
||||
baseURL: 'http://192.168.1.137:8086/app',
|
||||
baseURL: 'http://192.168.5.6:8086/app',
|
||||
// baseURL: 'http://192.168.1.137:8086/app',
|
||||
requestAdapter: axiosRequestAdapter(),
|
||||
timeout: 5000,
|
||||
cacheFor: null,
|
||||
|
||||
responded: {
|
||||
onSuccess: async (response, method: Method) => {
|
||||
onSuccess: async (response, _method: Method) => {
|
||||
const resData = response.data as ResponseData<any>
|
||||
|
||||
const result = {
|
||||
@@ -46,116 +45,78 @@ const alovaInstance = createAlova({
|
||||
isSuccess: resData.code === HttpCode.Success,
|
||||
}
|
||||
|
||||
// Access Token 失效 → 重新登录
|
||||
/**
|
||||
* 登录态失效:
|
||||
* - 清空本地状态
|
||||
* - 跳转登录页
|
||||
* - 不做 token 刷新、不重试请求
|
||||
*/
|
||||
if (resData.code === HttpCode.Unauthorized) {
|
||||
useUserStore.getState().clear()
|
||||
useNavStore.getState().navigateToAuth()
|
||||
return
|
||||
}
|
||||
|
||||
// Access Token 过期但 Refresh Token 有效 → 刷新 token 并重试
|
||||
if (resData.code === HttpCode.RefreshToken) {
|
||||
try {
|
||||
const refreshToken = useUserStore.getState().userInfo?.refreshToken
|
||||
if (!refreshToken) {
|
||||
useNavStore.getState().navigateToAuth()
|
||||
return
|
||||
}
|
||||
|
||||
// 调用刷新接口
|
||||
const res = await alovaInstance.Get<ResponseData<RefreshTokenResult>>('/auth/refresh/token', {
|
||||
headers: { refreshToken },
|
||||
}).send()
|
||||
|
||||
const newToken = res.data.refreshToken
|
||||
|
||||
// 更新 store
|
||||
useUserStore.getState().setUserInfo({
|
||||
...useUserStore.getState().userInfo!,
|
||||
token: newToken,
|
||||
})
|
||||
|
||||
// 重试原请求
|
||||
method.config.headers.token = newToken
|
||||
const retryResult = await method.send()
|
||||
|
||||
return retryResult
|
||||
}
|
||||
catch (err) {
|
||||
return Promise.reject(err)
|
||||
}
|
||||
return Promise.reject(new Error('登录已失效'))
|
||||
}
|
||||
|
||||
return result
|
||||
},
|
||||
onError: (error: AxiosError, _method) => {
|
||||
console.log(error.status)
|
||||
|
||||
switch (error.status) {
|
||||
onError: (error: AxiosError) => {
|
||||
const status = error.status
|
||||
|
||||
switch (status) {
|
||||
case 400:
|
||||
toast.show({
|
||||
title: '请求错误,请检查输入参数',
|
||||
})
|
||||
toast.show({ title: '请求错误,请检查输入参数' })
|
||||
break
|
||||
case 401:
|
||||
toast.show({
|
||||
title: '未授权,请重新登录',
|
||||
})
|
||||
toast.show({ title: '未授权,请重新登录' })
|
||||
break
|
||||
case 403:
|
||||
toast.show({
|
||||
title: '没有权限访问该资源',
|
||||
})
|
||||
toast.show({ title: '没有权限访问该资源' })
|
||||
break
|
||||
case 404:
|
||||
toast.show({
|
||||
title: '请求丢失啦,请稍后再试!',
|
||||
})
|
||||
toast.show({ title: '请求丢失啦,请稍后再试!' })
|
||||
break
|
||||
case 500:
|
||||
toast.show({
|
||||
title: '服务器出错啦,请稍后重试',
|
||||
})
|
||||
toast.show({ title: '服务器出错啦,请稍后重试' })
|
||||
break
|
||||
case 502:
|
||||
toast.show({
|
||||
title: '网关错误,请稍后重试',
|
||||
})
|
||||
toast.show({ title: '网关错误,请稍后重试' })
|
||||
break
|
||||
case 503:
|
||||
toast.show({
|
||||
title: '服务暂不可用,请稍后再试',
|
||||
})
|
||||
toast.show({ title: '服务暂不可用,请稍后再试' })
|
||||
break
|
||||
case 504:
|
||||
toast.show({
|
||||
title: '网关超时,请稍后再试',
|
||||
})
|
||||
toast.show({ title: '网关超时,请稍后再试' })
|
||||
break
|
||||
default:
|
||||
toast.show({
|
||||
title: `服务器出错啦,请稍后重试`,
|
||||
})
|
||||
toast.show({ title: '服务器出错啦,请稍后重试' })
|
||||
break
|
||||
}
|
||||
|
||||
return Promise.reject(error)
|
||||
},
|
||||
},
|
||||
|
||||
beforeRequest(method) {
|
||||
// 白名单接口直接放行
|
||||
if (isWhiteListed(method.url))
|
||||
return
|
||||
|
||||
if (!useUserStore.getState().isLoggedIn()) {
|
||||
const userStore = useUserStore.getState()
|
||||
|
||||
// 未登录直接拦截
|
||||
if (!userStore.isLoggedIn()) {
|
||||
useNavStore.getState().navigateToAuth()
|
||||
return Promise.reject(new Error('未登录'))
|
||||
}
|
||||
|
||||
method.config.headers.token = useUserStore.getState().userInfo?.token ?? ''
|
||||
// 注入 JWT
|
||||
method.config.headers.token = userStore.userInfo!.token
|
||||
|
||||
// 注入 space 信息(如果存在)
|
||||
const userSpace = useSpaceStore.getState().userSpace
|
||||
if (userSpace !== null) {
|
||||
const spaceID = userSpace.space_id
|
||||
|
||||
method.config.headers['x-space-id'] = spaceID
|
||||
if (userSpace) {
|
||||
method.config.headers['x-space-id'] = userSpace.space_id
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user