Explorar o código

feat: request 响应拦截器增加对token过期的逻辑

lanjianrong %!s(int64=3) %!d(string=hai) anos
pai
achega
05da9c27f0
Modificáronse 2 ficheiros con 6 adicións e 3 borrados
  1. 4 2
      src/app.tsx
  2. 2 1
      src/utils/consts.ts

+ 4 - 2
src/app.tsx

@@ -2,8 +2,7 @@ import type { Settings as LayoutSettings } from '@ant-design/pro-layout'
 import { notification } from 'antd'
 import { PageLoading } from '@ant-design/pro-layout'
 import type { RequestConfig, RunTimeLayoutConfig } from 'umi'
-import { Link } from 'umi'
-import { history } from 'umi'
+import { Link, history } from 'umi'
 import type { RequestOptionsInit, ResponseError } from 'umi-request'
 import RightContent from '@/components/RightContent'
 // import { currentUser as queryCurrentUser } from './services/ant-design-pro/api'
@@ -89,6 +88,9 @@ export const request: RequestConfig = {
   errorHandler: (error: ResponseError) => {
     // const { messages } = getIntl(getLocale())
     const { response } = error
+    if (response.code === consts.TOKEN_EXPIRED) {
+      return history.push(loginPath)
+    }
     if (response && response.msg) {
       // const { status, statusText, url } = response
       // const requestErrorMessage = messages['app.request.error']

+ 2 - 1
src/utils/consts.ts

@@ -2,5 +2,6 @@ export default {
   RET_CODE: {
     SUCCESS: 0,
     ERROR: -1
-  }
+  },
+  TOKEN_EXPIRED: 2
 }