|
|
@@ -1,6 +1,6 @@
|
|
|
import { queryCurrentUser, queryPermData } from './services/user'
|
|
|
import type { ErrorShowType, RequestConfig } from '@umijs/max'
|
|
|
-import { history } from '@umijs/max'
|
|
|
+import { history, createSearchParams } from '@umijs/max'
|
|
|
import RightContent from '@/components/RightContent'
|
|
|
import { message, notification } from 'antd'
|
|
|
import logo from '../public/logo.svg'
|
|
|
@@ -80,7 +80,7 @@ export const layout = ({ initialState, setInitialState }) => {
|
|
|
if (!initialState?.currentUser?.staffId && location.pathname !== loginPath) {
|
|
|
history.replace({
|
|
|
pathname: loginPath,
|
|
|
- search: new URLSearchParams({ redirect: window.location.pathname }).toString()
|
|
|
+ search: createSearchParams({ redirect: window.location.pathname }).toString()
|
|
|
})
|
|
|
} else {
|
|
|
location.pathname !== loginPath &&
|
|
|
@@ -95,58 +95,68 @@ export const layout = ({ initialState, setInitialState }) => {
|
|
|
}
|
|
|
|
|
|
let notificationLimit = 0 // 最大1
|
|
|
-const errorHandler = error => {
|
|
|
- const { data, info } = error
|
|
|
- if (info) {
|
|
|
- const { errorMessage = '请求失败', showType, errorCode } = info
|
|
|
- if (consts.TOKEN_INVALID_CODE.includes(errorCode) && window.location.pathname !== loginPath) {
|
|
|
- history.replace({
|
|
|
- pathname: loginPath,
|
|
|
- search: new URLSearchParams({ redirect: window.location.pathname }).toString()
|
|
|
+const errorHandler = (error: any, opts: any) => {
|
|
|
+ if (opts?.skipErrorHandler) throw error
|
|
|
+ if (error.name === 'BizError') {
|
|
|
+ const errorInfo = error.info
|
|
|
+ if (errorInfo) {
|
|
|
+ if (notificationLimit >= 1) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const { errorMessage = '请求失败', showType, errorCode } = info
|
|
|
+ if (consts.TOKEN_INVALID_CODE.includes(errorCode) && window.location.pathname !== loginPath) {
|
|
|
+ history.replace({
|
|
|
+ pathname: loginPath,
|
|
|
+ search: createSearchParams({ redirect: window.location.pathname }).toString()
|
|
|
+ })
|
|
|
+ }
|
|
|
+ switch (showType) {
|
|
|
+ case ErrorShowType.ERROR_MESSAGE:
|
|
|
+ message.error(errorMessage)
|
|
|
+ break
|
|
|
+ case ErrorShowType.WARN_MESSAGE:
|
|
|
+ message.warn(errorMessage)
|
|
|
+ break
|
|
|
+ case ErrorShowType.NOTIFICATION:
|
|
|
+ if (!notificationLimit) {
|
|
|
+ const title = consts.TOKEN_INVALID_CODE.includes(errorCode) ? '用户信息过期' : '请求失败'
|
|
|
+ notification.error({
|
|
|
+ message: title,
|
|
|
+ description: errorMessage
|
|
|
+ })
|
|
|
+ notificationLimit++
|
|
|
+ }
|
|
|
+ break
|
|
|
+ default:
|
|
|
+ break
|
|
|
+ }
|
|
|
+ } else if (error.response) {
|
|
|
+ // Axios 的错误
|
|
|
+ // 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围
|
|
|
+ notification.error({
|
|
|
+ description: `状态码为${error.response.status}, 请联系管理员进行处理`,
|
|
|
+ message: '请求异常'
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ // 请求已经成功发起,但没有收到响应
|
|
|
+ // 或请求根本没有发送出去
|
|
|
+ notification.error({
|
|
|
+ description: '您的网络发生异常,无法连接服务器',
|
|
|
+ message: '网络异常'
|
|
|
})
|
|
|
}
|
|
|
- switch (showType) {
|
|
|
- case ErrorShowType.ERROR_MESSAGE:
|
|
|
- message.error(errorMessage)
|
|
|
- break
|
|
|
- case ErrorShowType.WARN_MESSAGE:
|
|
|
- message.warn(errorMessage)
|
|
|
- break
|
|
|
- case ErrorShowType.NOTIFICATION:
|
|
|
- if (!notificationLimit) {
|
|
|
- const title = consts.TOKEN_INVALID_CODE.includes(errorCode) ? '用户信息过期' : '请求失败'
|
|
|
- notification.error({
|
|
|
- message: title,
|
|
|
- description: errorMessage
|
|
|
- })
|
|
|
- notificationLimit++
|
|
|
- }
|
|
|
- break
|
|
|
- default:
|
|
|
- break
|
|
|
- }
|
|
|
- return Promise.reject(data)
|
|
|
- } else {
|
|
|
- notification.error({
|
|
|
- description: '您的网络发生异常,无法连接服务器',
|
|
|
- message: '网络异常'
|
|
|
- })
|
|
|
}
|
|
|
- return Promise.reject(error)
|
|
|
}
|
|
|
-
|
|
|
-const authHeaderInterceptor = (url, options) => {
|
|
|
+const authHeaderInterceptor = (options: any) => {
|
|
|
const token = getToken()
|
|
|
if (token) {
|
|
|
// 在白名单里的请求放过
|
|
|
- if (consts.TOKEN_WHITE_LIST.includes(url)) {
|
|
|
- return { url, options }
|
|
|
+ if (consts.TOKEN_WHITE_LIST.includes(options.url)) {
|
|
|
+ return options
|
|
|
}
|
|
|
- // eslint-disable-next-line no-param-reassign
|
|
|
options.headers[consts.TOKEN_HEADER] = `bearer ${token}`
|
|
|
- return { url, options }
|
|
|
}
|
|
|
- return { url, options }
|
|
|
+ return options
|
|
|
}
|
|
|
|
|
|
export const request: RequestConfig = {
|
|
|
@@ -164,22 +174,18 @@ export const request: RequestConfig = {
|
|
|
errorHandler
|
|
|
},
|
|
|
baseURL: consts.PREFIX_URL,
|
|
|
- // 默认错误处理
|
|
|
- credentials: 'include', // 默认请求是否带上cookie
|
|
|
- cache: 'no-cache',
|
|
|
- errorConfig: {
|
|
|
- adaptor: resData => {
|
|
|
- if (notificationLimit !== 0 && resData.code === consts.RET_CODE.SUCCESS) {
|
|
|
+ requestInterceptors: [authHeaderInterceptor],
|
|
|
+ responseInterceptors: [
|
|
|
+ response => {
|
|
|
+ // 处理notificationLimit,请求成功应重置
|
|
|
+ if (
|
|
|
+ notificationLimit >= 1 &&
|
|
|
+ response.status === 200 &&
|
|
|
+ response.data?.code === consts.RET_CODE.SUCCESS
|
|
|
+ ) {
|
|
|
notificationLimit = 0
|
|
|
}
|
|
|
- return {
|
|
|
- success: resData.code === consts.RET_CODE.SUCCESS,
|
|
|
- errorMessage: resData.msg,
|
|
|
- errorCode: resData.code,
|
|
|
- showType: resData.showType
|
|
|
- }
|
|
|
+ return response
|
|
|
}
|
|
|
- },
|
|
|
- requestInterceptors: [authHeaderInterceptor],
|
|
|
- responseInterceptors: []
|
|
|
+ ]
|
|
|
}
|