|
|
|
@ -1,7 +1,7 @@ |
|
|
|
/** |
|
|
|
* 该文件可自行根据业务逻辑进行调整 |
|
|
|
*/ |
|
|
|
import type { RequestClientOptions } from '@vben/request'; |
|
|
|
import type { RequestClientConfig, RequestClientOptions } from '@vben/request'; |
|
|
|
|
|
|
|
import { useAppConfig } from '@vben/hooks'; |
|
|
|
import { preferences } from '@vben/preferences'; |
|
|
|
@ -29,18 +29,19 @@ function createRequestClient(baseURL: string, options?: RequestClientOptions) { |
|
|
|
/** |
|
|
|
* 重新认证逻辑 |
|
|
|
*/ |
|
|
|
async function doReAuthenticate() { |
|
|
|
async function doReAuthenticate(config?: RequestClientConfig) { |
|
|
|
console.warn('Access token or refresh token is invalid or expired. '); |
|
|
|
const accessStore = useAccessStore(); |
|
|
|
const authStore = useAuthStore(); |
|
|
|
accessStore.setAccessToken(null); |
|
|
|
const navigate = !config?.skipAuthNavigate; |
|
|
|
if ( |
|
|
|
preferences.app.loginExpiredMode === 'modal' && |
|
|
|
accessStore.isAccessChecked |
|
|
|
) { |
|
|
|
accessStore.setAccessToken(null); |
|
|
|
accessStore.setLoginExpired(true); |
|
|
|
} else { |
|
|
|
await authStore.logout(); |
|
|
|
await authStore.logout(true, navigate); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -71,13 +72,7 @@ function createRequestClient(baseURL: string, options?: RequestClientOptions) { |
|
|
|
}); |
|
|
|
|
|
|
|
// 处理返回的响应数据格式
|
|
|
|
client.addResponseInterceptor( |
|
|
|
defaultResponseInterceptor({ |
|
|
|
codeField: 'code', |
|
|
|
dataField: 'data', |
|
|
|
successCode: 0, |
|
|
|
}), |
|
|
|
); |
|
|
|
client.addResponseInterceptor(defaultResponseInterceptor({})); |
|
|
|
|
|
|
|
// token过期的处理
|
|
|
|
client.addResponseInterceptor( |
|
|
|
@ -96,7 +91,8 @@ function createRequestClient(baseURL: string, options?: RequestClientOptions) { |
|
|
|
// 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg
|
|
|
|
// 当前mock接口返回的错误字段是 error 或者 message
|
|
|
|
const responseData = error?.response?.data ?? {}; |
|
|
|
const errorMessage = responseData?.error ?? responseData?.message ?? ''; |
|
|
|
const errorMessage = |
|
|
|
responseData?.error ?? responseData?.message ?? responseData?.msg ?? ''; |
|
|
|
// 如果没有错误信息,则会根据状态码进行提示
|
|
|
|
message.error(errorMessage || msg); |
|
|
|
}), |
|
|
|
|