import type { Settings as LayoutSettings } from '@ant-design/pro-layout' ; import { PageLoading } from '@ant-design/pro-layout' ; import { notification } from 'antd' ; import type { RequestConfig , RunTimeLayoutConfig } from 'umi' ; import { history } from 'umi' ; import RightContent from '@/components/RightContent' ; import Footer from '@/components/Footer' ; import { getPersistenceData } from "@/utils/persistence" ; import { CURRENT_USER , TOKEN } from "@/constants" ; const loginPath = '/user/login' ; /** 获取用户信息比较慢的时候会展示一个 loading */ export const initialStateConfig = { loading : < PageLoading />, } ; see https export async function getInitialState ( ) { const fetchUserInfo = async ( ) => { const token = getPersistenceData ( TOKEN ) ; const user = getPersistenceData ( CURRENT_USER ) ; if ( token ) { return { name : JSON . parse ( user ) ?. name , access : 'admin' , } ; } else { history . push ( loginPath ) ; } return undefined ; } ; if ( history . location . pathname !== loginPath ) { const currentUser = await fetchUserInfo ( ) ; return { fetchUserInfo , currentUser , settings : { } , } ; } return { fetchUserInfo , settings : { } , } ; } see https export const request = { errorHandler : ( error ) => { const { response } = error ; if ( ! response ) { notification . error ( { description : '您的网络发生异常，无法连接服务器' , message : '网络异常' , } ) ; } throw error ; } , } ; export const layout = ( { initialState } ) => { return { rightContentRender : ( ) => < RightContent />, disableContentMargin : false , waterMarkProps : { content : initialState ?. currentUser ?. name , } , footerRender : ( ) => < Footer />, onPageChange : ( ) => { const { location } = history ; const token = getPersistenceData ( TOKEN ) ; if ( ! token && location . pathname !== loginPath ) { history . push ( loginPath ) ; } } , menuHeaderRender : undefined , ... initialState ?. settings , } ; } ;