|
@@ -1,4 +1,6 @@
|
|
|
import { queryCurrentUser, queryPermData } from './services/user'
|
|
import { queryCurrentUser, queryPermData } from './services/user'
|
|
|
|
|
+import type { RequestConfig } from 'umi'
|
|
|
|
|
+import { ErrorShowType } from 'umi'
|
|
|
import { history } from 'umi'
|
|
import { history } from 'umi'
|
|
|
import RightContent from '@/components/RightContent'
|
|
import RightContent from '@/components/RightContent'
|
|
|
import { message, notification } from 'antd'
|
|
import { message, notification } from 'antd'
|
|
@@ -95,23 +97,33 @@ export const layout = ({ initialState, setInitialState }) => {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const errorHandler = error => {
|
|
const errorHandler = error => {
|
|
|
- const { response } = error
|
|
|
|
|
-
|
|
|
|
|
- if (response && response.status) {
|
|
|
|
|
- const errorText = codeMessage[response.status] || response.statusText
|
|
|
|
|
- const { status, url } = response
|
|
|
|
|
- notification.error({
|
|
|
|
|
- message: `请求错误 ${status}: ${url}`,
|
|
|
|
|
- description: errorText
|
|
|
|
|
- })
|
|
|
|
|
- } else if (!response) {
|
|
|
|
|
|
|
+ const { data, info } = error
|
|
|
|
|
+ if (info) {
|
|
|
|
|
+ const { errorMessage = '请求失败', showType, errorCode } = info
|
|
|
|
|
+ if (consts.TOKEN_INVALID_CODE.includes(errorCode)) {
|
|
|
|
|
+ history.replace(consts.LOGIN_PATH)
|
|
|
|
|
+ }
|
|
|
|
|
+ switch (showType) {
|
|
|
|
|
+ case ErrorShowType.ERROR_MESSAGE:
|
|
|
|
|
+ message.error(errorMessage)
|
|
|
|
|
+ case ErrorShowType.WARN_MESSAGE:
|
|
|
|
|
+ message.warn(errorMessage)
|
|
|
|
|
+ case ErrorShowType.NOTIFICATION:
|
|
|
|
|
+ const title = consts.TOKEN_INVALID_CODE.includes(errorCode) ? '用户信息过期' : '请求失败'
|
|
|
|
|
+ notification.error({
|
|
|
|
|
+ message: title,
|
|
|
|
|
+ description: errorMessage
|
|
|
|
|
+ })
|
|
|
|
|
+ default:
|
|
|
|
|
+ break
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
notification.error({
|
|
notification.error({
|
|
|
description: '您的网络发生异常,无法连接服务器',
|
|
description: '您的网络发生异常,无法连接服务器',
|
|
|
message: '网络异常'
|
|
message: '网络异常'
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return response
|
|
|
|
|
|
|
+ return data
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const authHeaderInterceptor = (url, options) => {
|
|
const authHeaderInterceptor = (url, options) => {
|
|
@@ -128,28 +140,6 @@ const authHeaderInterceptor = (url, options) => {
|
|
|
return { url, options }
|
|
return { url, options }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-const refreshTokenIfNeed = async (response, config) => {
|
|
|
|
|
- const res = await response.clone().json()
|
|
|
|
|
- if (
|
|
|
|
|
- res &&
|
|
|
|
|
- res.code !== consts.RET_CODE.SUCCESS &&
|
|
|
|
|
- !consts.TOKEN_INVALID_CODE.includes(res.code) &&
|
|
|
|
|
- config.url !== '/api/login'
|
|
|
|
|
- ) {
|
|
|
|
|
- message.error((res && res.msg) || '请求错误')
|
|
|
|
|
- // return Promise.reject(response)
|
|
|
|
|
- return response
|
|
|
|
|
- }
|
|
|
|
|
- if (res && res.code && consts.TOKEN_INVALID_CODE.includes(res.code)) {
|
|
|
|
|
- history.replace(consts.LOGIN_PATH)
|
|
|
|
|
- }
|
|
|
|
|
- return response
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-/**
|
|
|
|
|
- * 配置request请求时的默认参数
|
|
|
|
|
- */
|
|
|
|
|
-
|
|
|
|
|
export const request: RequestConfig = {
|
|
export const request: RequestConfig = {
|
|
|
errorHandler,
|
|
errorHandler,
|
|
|
prefix: '/api',
|
|
prefix: '/api',
|
|
@@ -157,6 +147,15 @@ export const request: RequestConfig = {
|
|
|
credentials: 'include', // 默认请求是否带上cookie
|
|
credentials: 'include', // 默认请求是否带上cookie
|
|
|
prefix: consts.PREFIX_URL,
|
|
prefix: consts.PREFIX_URL,
|
|
|
cache: 'no-cache',
|
|
cache: 'no-cache',
|
|
|
- responseInterceptors: [(response, options) => refreshTokenIfNeed(response, options)],
|
|
|
|
|
|
|
+ errorConfig: {
|
|
|
|
|
+ adaptor: resData => {
|
|
|
|
|
+ return {
|
|
|
|
|
+ success: resData.code === consts.RET_CODE.SUCCESS,
|
|
|
|
|
+ errorMessage: resData.msg,
|
|
|
|
|
+ errorCode: resData.code,
|
|
|
|
|
+ showType: resData.showType
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
requestInterceptors: [authHeaderInterceptor]
|
|
requestInterceptors: [authHeaderInterceptor]
|
|
|
}
|
|
}
|