-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
app.tsx
117 lines (113 loc) · 3.46 KB
/
app.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { PageLoading } from '@ant-design/pro-layout';
import type { RunTimeLayoutConfig } from 'umi';
import { history, Link } from 'umi';
import RightContent from '@/components/RightContent';
import Footer from '@/components/Footer';
import { currentUser as queryCurrentUser } from './services/ant-design-pro/api';
import { BookOutlined, LinkOutlined } from '@ant-design/icons';
import SwitchTabsLayout from './layouts/SwitchTabsLayout';
import type { Settings } from '../config/defaultSettings';
import defaultSettings from '../config/defaultSettings';
const isDev = process.env.NODE_ENV === 'development';
const loginPath = '/user/login';
/** 获取用户信息比较慢的时候会展示一个 loading */
export const initialStateConfig = {
loading: <PageLoading />,
};
/**
* @see https://umijs.org/zh-CN/plugins/plugin-initial-state
* */
export async function getInitialState(): Promise<{
settings?: Partial<Settings>;
currentUser?: API.CurrentUser;
fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
}> {
const fetchUserInfo = async () => {
try {
const msg = await queryCurrentUser();
return msg.data;
} catch (error) {
history.push(loginPath);
}
return undefined;
};
if (process.env.NODE_ENV === 'production') {
return {
fetchUserInfo,
currentUser: {
name: 'Yuns',
avatar: 'https://avatars.githubusercontent.com/u/18096089',
},
settings: defaultSettings,
};
}
if (history.location.pathname !== loginPath) {
// 如果是登录页面,不执行
const currentUser = await fetchUserInfo();
return {
fetchUserInfo,
currentUser,
settings: defaultSettings,
};
}
return {
fetchUserInfo,
settings: defaultSettings,
};
}
// ProLayout 支持的api https://procomponents.ant.design/components/layout
export const layout: RunTimeLayoutConfig = ({ initialState }) => {
const { switchTabs, ...restSettings } = initialState?.settings || {};
return {
rightContentRender: () => (
<RightContent switchTabsReloadable={switchTabs?.mode && switchTabs.reloadable} />
),
disableContentMargin: false,
waterMarkProps: {
content: initialState?.currentUser?.name,
},
className: switchTabs?.mode && 'custom-by-switch-tabs',
childrenRender: (children, props) => {
const { route } = props;
return (
<SwitchTabsLayout
mode={switchTabs?.mode}
persistent={switchTabs?.persistent}
fixed={switchTabs?.fixed}
routes={route!.routes}
footerRender={() => <Footer />}
>
{children}
</SwitchTabsLayout>
);
},
onPageChange: () => {
const { location } = history;
// 如果没有登录,重定向到 login
if (!initialState?.currentUser && location.pathname !== loginPath) {
history.push(loginPath);
}
},
links: isDev
? [
<Link to="/umi/plugin/openapi" target="_blank">
<LinkOutlined />
<span>OpenAPI 文档</span>
</Link>,
<Link to="/~docs">
<BookOutlined />
<span>业务组件文档</span>
</Link>,
]
: [],
menuHeaderRender: undefined,
// 自定义 403 页面
// unAccessible: <div>unAccessible</div>,
// 增加一个 loading 的状态
// childrenRender: (children) => {
// if (initialState.loading) return <PageLoading />;
// return children;
// },
...restSettings,
};
};