|
@@ -9,8 +9,6 @@ import RightContent from '@/components/RightContent'
|
|
import consts from './utils/consts'
|
|
import consts from './utils/consts'
|
|
import { queryCurrentUser } from './services/api/login'
|
|
import { queryCurrentUser } from './services/api/login'
|
|
|
|
|
|
-const loginPath = '/user/login'
|
|
|
|
-
|
|
|
|
/** 获取用户信息比较慢的时候会展示一个 loading */
|
|
/** 获取用户信息比较慢的时候会展示一个 loading */
|
|
export const initialStateConfig = {
|
|
export const initialStateConfig = {
|
|
loading: <PageLoading />
|
|
loading: <PageLoading />
|
|
@@ -29,12 +27,12 @@ export async function getInitialState(): Promise<{
|
|
const msg = await queryCurrentUser()
|
|
const msg = await queryCurrentUser()
|
|
return msg.data
|
|
return msg.data
|
|
} catch (error) {
|
|
} catch (error) {
|
|
- history.push(loginPath)
|
|
|
|
|
|
+ history.push(consts.loginPath)
|
|
}
|
|
}
|
|
return undefined
|
|
return undefined
|
|
}
|
|
}
|
|
// 如果是登录页面,不执行
|
|
// 如果是登录页面,不执行
|
|
- if (history.location.pathname !== loginPath) {
|
|
|
|
|
|
+ if (history.location.pathname !== consts.loginPath) {
|
|
const currentUser = await fetchUserInfo()
|
|
const currentUser = await fetchUserInfo()
|
|
return {
|
|
return {
|
|
fetchUserInfo,
|
|
fetchUserInfo,
|
|
@@ -69,8 +67,8 @@ const codeMaps = {
|
|
const authHeaderInterceptor = (url: string, options: RequestOptionsInit) => {
|
|
const authHeaderInterceptor = (url: string, options: RequestOptionsInit) => {
|
|
const token = window.localStorage.getItem('TOKEN_ID')
|
|
const token = window.localStorage.getItem('TOKEN_ID')
|
|
// 如果是登录页面,不执行
|
|
// 如果是登录页面,不执行
|
|
- if (!token && history.location.pathname !== loginPath) {
|
|
|
|
- return history.push(loginPath)
|
|
|
|
|
|
+ if (!token && history.location.pathname !== consts.loginPath) {
|
|
|
|
+ return history.replace(consts.loginPath)
|
|
}
|
|
}
|
|
// const authHeader = { Authorization: `Bearer ${JSON.parse(token)}` }
|
|
// const authHeader = { Authorization: `Bearer ${JSON.parse(token)}` }
|
|
const authHeader = { Authorization: `Bearer ${token}` }
|
|
const authHeader = { Authorization: `Bearer ${token}` }
|
|
@@ -95,14 +93,14 @@ export const request: RequestConfig = {
|
|
}
|
|
}
|
|
if (
|
|
if (
|
|
consts.TOKEN_INVALID_CODE.includes(response.code) &&
|
|
consts.TOKEN_INVALID_CODE.includes(response.code) &&
|
|
- window.location.pathname !== loginPath
|
|
|
|
|
|
+ window.location.pathname !== consts.loginPath
|
|
) {
|
|
) {
|
|
notification.error({
|
|
notification.error({
|
|
message: '用户信息过期',
|
|
message: '用户信息过期',
|
|
description: '请重新登录'
|
|
description: '请重新登录'
|
|
})
|
|
})
|
|
history.replace({
|
|
history.replace({
|
|
- pathname: loginPath,
|
|
|
|
|
|
+ pathname: consts.loginPath,
|
|
search: stringify({
|
|
search: stringify({
|
|
redirect: window.location.pathname
|
|
redirect: window.location.pathname
|
|
})
|
|
})
|
|
@@ -150,11 +148,14 @@ export const layout: RunTimeLayoutConfig = ({ initialState }) => {
|
|
// content: initialState?.currentUser?.name
|
|
// content: initialState?.currentUser?.name
|
|
// },
|
|
// },
|
|
onPageChange: () => {
|
|
onPageChange: () => {
|
|
- // const { location } = history
|
|
|
|
- // // 如果没有登录,重定向到 login
|
|
|
|
- // if (!initialState?.currentUser && location.pathname !== loginPath) {
|
|
|
|
- // history.push(loginPath)
|
|
|
|
- // }
|
|
|
|
|
|
+ if (!initialState?.currentUser?.ID && location.pathname !== consts.loginPath) {
|
|
|
|
+ history.replace({
|
|
|
|
+ pathname: consts.loginPath,
|
|
|
|
+ search: stringify({
|
|
|
|
+ redirect: history.location.pathname
|
|
|
|
+ })
|
|
|
|
+ })
|
|
|
|
+ }
|
|
},
|
|
},
|
|
menuHeaderRender: undefined,
|
|
menuHeaderRender: undefined,
|
|
itemRender: (route, _, routes) => {
|
|
itemRender: (route, _, routes) => {
|