|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** 请求白名单,放置一些不需要token的接口(通过设置请求白名单,防止token过期后再请求造成的死循环问题) */ | 
|---|
|  |  |  | const whiteList = [RefreshTokenUrl]; | 
|---|
|  |  |  | const RefreshTokenBlackList = ['wxmpLogin']; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | export function startLoading(showNavigationBarLoading: boolean) { | 
|---|
|  |  |  | if (showNavigationBarLoading) { | 
|---|
|  |  |  | 
|---|
|  |  |  | const errorInfo: ErrorInfo | undefined = (error as any).info; | 
|---|
|  |  |  | if (errorInfo) { | 
|---|
|  |  |  | const { errorMessage, errorCode } = errorInfo; | 
|---|
|  |  |  | if (Number(errorCode) === 401) { | 
|---|
|  |  |  | handleLogout(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | switch (errorInfo.showType) { | 
|---|
|  |  |  | case ErrorShowType.SILENT: | 
|---|
|  |  |  | // do nothing | 
|---|
|  |  |  | 
|---|
|  |  |  | } else if ((error as AxiosError<ResponseStructure, IRequestOptions>).response) { | 
|---|
|  |  |  | // Axios 的错误 | 
|---|
|  |  |  | // 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围 | 
|---|
|  |  |  | handleAxiosResponseError(error as AxiosError<ErrorResponse, IRequestOptions>); | 
|---|
|  |  |  | handleAxiosResponseError(error as AxiosError<ResponseStructure, IRequestOptions>); | 
|---|
|  |  |  | // Message.errorMessage(`Response status:${(error as AxiosError).response.status}`); | 
|---|
|  |  |  | } else if ((error as AxiosError).request) { | 
|---|
|  |  |  | // 请求已经成功发起,但没有收到响应 | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | const userStore = useUserStoreWithOut(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (response.headers['x-access-token']) { | 
|---|
|  |  |  | const tokenInfo: API.PasswordLoginCommandCallback = { | 
|---|
|  |  |  | if ( | 
|---|
|  |  |  | response.headers['x-access-token'] && | 
|---|
|  |  |  | RefreshTokenBlackList.every((url) => !response.config.url?.includes(url)) | 
|---|
|  |  |  | ) { | 
|---|
|  |  |  | const tokenInfo: API.LoginCommandCallback = { | 
|---|
|  |  |  | accessToken: response.headers['access-token'], | 
|---|
|  |  |  | refreshToken: response.headers['x-access-token'], | 
|---|
|  |  |  | }; | 
|---|
|  |  |  | 
|---|
|  |  |  | [505]: 'HTTP版本不受支持', | 
|---|
|  |  |  | }; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | function handleAxiosResponseError(error: AxiosError<ErrorResponse, IRequestOptions>) { | 
|---|
|  |  |  | function handleAxiosResponseError(error: AxiosError<ResponseStructure, IRequestOptions>) { | 
|---|
|  |  |  | if (error.response.config.url.toLowerCase().includes(RefreshTokenUrl.toLowerCase())) { | 
|---|
|  |  |  | handleLogout(); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (error && error.response) { | 
|---|
|  |  |  | let message = ErrorMessageMap[error.response?.status] ?? '请求错误'; | 
|---|
|  |  |  | if (error.response.data?.error?.message) { | 
|---|
|  |  |  | message = error.response.data?.error?.message; | 
|---|
|  |  |  | if (error.response.data?.msg) { | 
|---|
|  |  |  | message = error.response.data?.msg; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (error.response?.status === 401) { | 
|---|
|  |  |  | if (error.response?.status === 401 || error.response.data.code === 401) { | 
|---|
|  |  |  | handleLogout(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|