import { AvatarGif , LogoPng } from '@/assets' ; import { BasicLayoutProps , Settings as LayoutSettings } from '@ant-design/pro-layout' ; import { ErrorShowType , RequestConfig , history } from 'umi' ; import { MenuAsyncAPI , MenuListAsync } from '@/services/menu' ; import { BaseUrl } from '@/configs' ; import Cookies from 'js-cookie' ; import { LoadUser } from '@/services/user' ; import React from 'react' ; import RightContent from '@/components/RightContent' ; import defaultSettings from '../config/default' ; /**
 * 权限相关必须要用的东西,自己去改函数名
 */ export const getInitialState = async ( ) : Promise < { currentUser ? : Types . CurrentUser ; settings ? : LayoutSettings ; } > => { if ( history . location . pathname !== '/login' ) { try { let userid = Cookies . get ( 'userId' ) ?? '' ; let response = await LoadUser ( { id : userid } ) ; const userInfo = response . data ; const { nickName } = userInfo ; const menustr = window . localStorage . getItem ( 'menu' ) ; let menu = [ ] ; let menulist = [ ] ; if ( menustr != 'undefined' ) { menu = menustr ? JSON . parse ( menustr ) : [ ] ; } if ( menu . length <= 0 ) { let menuRes = await MenuAsyncAPI ( ) ; const { data } = menuRes ; menu = data ; window . localStorage . setItem ( 'menu' , JSON . stringify ( menu ) ) ; } if ( menulist . length <= 0 ) { let menulists = await MenuListAsync ( ) ; const { data } = menulists ; window . localStorage . setItem ( 'menulist' , JSON . stringify ( data ) ) ; } return { currentUser : { name : nickName ?? '默认用户名' , userid , avatar : AvatarGif , access : menu } , settings : defaultSettings } ; } catch ( error ) { history . push ( '/login' ) ; throw error ; } } else return { settings : defaultSettings } ; } ; param param export const layout = ( { initialState } ) : BasicLayoutProps => { return { logo : < img src = { LogoPng } style = { { borderRadius : 7 , marginLeft : 12 , marginRight : 6 } } />, siderWidth : 220 , rightContentRender : ( ) => < RightContent />, disableContentMargin : false , disableMobile : true , menuHeaderRender : undefined , onPageChange : ( ) => { if ( ! initialState ?. currentUser ?. userid && history . location . pathname !== '/login' ) history . push ( '/login' ) ; } , ... initialState ?. settings } ; } ; /**
 * 运行时request配置
 */ export const request = { errorConfig : { adaptor : ( res ) => { return { data : res . data || res . result , success : res . ok || res . Success || res . success , errorMessage : res . message || res . msg || res . Message , errorCode : res . code || res . type , showType : ErrorShowType . NOTIFICATION } ; } } , prefix : BaseUrl , credentials : 'include' , middlewares : [ ] , requestInterceptors : [ ( url , options ) => { options . headers = { Authorization : `Bearer ${ Cookies . get ( 'accessToken' ) ?? '' }` } ; return { url , options } ; } ] , responseInterceptors : [ ] } ;