import type { Settings as LayoutSettings } from '@ant-design/pro-components';
|
import { SettingDrawer } from '@ant-design/pro-components';
|
import type { RunTimeLayoutConfig } from '@umijs/max';
|
import { history } from '@umijs/max';
|
import defaultSettings from '../config/defaultSettings';
|
import { AvatarDropdown, AvatarName } from './components/RightContent/AvatarDropdown';
|
import { errorConfig } from './requestErrorConfig';
|
const loginPath = '/login';
|
import '../public/font.css'
|
import { useEffect, useState } from 'react';
|
|
|
/**
|
* @see https://umijs.org/zh-CN/plugins/plugin-initial-state
|
* */
|
export async function getInitialState(): Promise<{
|
settings?: Partial<LayoutSettings>;
|
currentUser?: {};
|
permission?: Array<Permissions>;
|
loading?: boolean;
|
}> {
|
// 如果不是登录页面,执行
|
const { location } = history;
|
|
if (location.pathname !== loginPath && !localStorage.getItem('userInfo')) {
|
history.replace(loginPath)
|
|
return {
|
permission: JSON.parse(localStorage.getItem('access') || '') || [],
|
currentUser: JSON.parse(localStorage.getItem('userInfo') || '') || {},
|
settings: { ...defaultSettings, fixedHeader: true } as Partial<LayoutSettings>,
|
};
|
}
|
return {
|
permission: JSON.parse(localStorage.getItem('access') || '') || [],
|
currentUser: JSON.parse(localStorage.getItem('userInfo') || '') || {},
|
settings: { ...defaultSettings, fixedHeader: true } as Partial<LayoutSettings>,
|
};
|
}
|
|
// ProLayout 支持的api https://procomponents.ant.design/components/layout
|
export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) => {
|
const [title, setTitle] = useState('');
|
|
useEffect(() => {
|
const updateTitle = () => {
|
const now = new Date();
|
const days = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
|
const formattedDate = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()} ${now.getHours()}:${now.getMinutes()} ${days[now.getDay()]}`;
|
setTitle(formattedDate);
|
};
|
updateTitle();
|
const interval = setInterval(updateTitle, 1000); // 每分钟更新一次
|
return () => clearInterval(interval); // 清除定时器
|
}, []);
|
return {
|
title,
|
logo: false,
|
avatarProps: {
|
title: <AvatarName />,
|
render: (_, avatarChildren) => {
|
return <AvatarDropdown>{avatarChildren}</AvatarDropdown>;
|
},
|
},
|
waterMarkProps: {
|
content: false,
|
},
|
// 自定义 403 页面
|
unAccessible: <div></div>,
|
// 自定义 404 页面
|
noFound: <div></div>,
|
// 增加一个 loading 的状态
|
childrenRender: (children) => {
|
return (
|
<>
|
{children}
|
<SettingDrawer
|
disableUrlParams
|
enableDarkTheme
|
settings={initialState?.settings}
|
onSettingChange={(settings) => {
|
setInitialState((preInitialState) => ({
|
...preInitialState,
|
settings,
|
}));
|
}}
|
/>
|
</>
|
);
|
},
|
...initialState?.settings,
|
pageTitleRender: () => '“三个身边”群众工作机制平台'
|
};
|
};
|
|
/**
|
* @name request 配置,可以配置错误处理
|
* 它基于 axios 和 ahooks 的 useRequest 提供了一套统一的网络请求和错误处理方案。
|
* @doc https://umijs.org/docs/max/request#配置
|
*/
|
export const request = {
|
...errorConfig,
|
};
|