-
Notifications
You must be signed in to change notification settings - Fork 359
/
app.jsx
168 lines (163 loc) · 4.89 KB
/
app.jsx
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import Footer from '@/components/Footer';
import RightContent from '@/components/RightContent';
import { BookOutlined, LinkOutlined } from '@ant-design/icons';
import { PageLoading, SettingDrawer } from '@ant-design/pro-layout';
import { history, Link } from 'umi';
import defaultSettings from '../config/defaultSettings';
import { fetchAll } from './services/van-blog/api';
const isDev = process.env.NODE_ENV === 'development';
const loginPath = '/user/login';
const basePath = '/';
/** 获取用户信息比较慢的时候会展示一个 loading */
export const initialStateConfig = {
loading: <PageLoading />,
};
/**
* @see https://umijs.org/zh-CN/plugins/plugin-initial-state
* */
export async function getInitialState() {
// console.log("init")
const fetchInitData = async (option) => {
try {
const msg = await fetchAll(option);
// console.log(msg);
// console.log(msg,window.location.pathname)
if (msg.statusCode == 233) {
history.push('/init');
} else if (window.location.pathname == '/init' && msg.statusCode == 200) {
history.push('/');
} else {
const result = msg.data;
if (result.articles && result.meta && result.meta.about) {
// 把 about 加进去
result.articles = [
{
id: 0,
category: '关于',
title: '关于',
tag: ['关于'],
about: true,
content: result.meta.about.content,
updatedAt:
result.meta.about.updatedAt || result.meta?.siteInfo?.since || '2022-06-26',
createdAt: result.meta?.siteInfo?.since || '2022-06-26',
},
...result.articles,
];
}
return msg.data;
}
} catch (error) {
// console.log("error",error)
history.push(loginPath);
}
return undefined;
}; // 如果不是登录页面,执行
let option = {};
if (location.pathname == loginPath || location.pathname == '/init') {
option.skipErrorHandler = true;
}
const initData = await fetchInitData(option);
return {
fetchInitData,
...initData,
settings: defaultSettings,
};
} // ProLayout 支持的api https://procomponents.ant.design/components/layout
export const layout = ({ initialState, setInitialState }) => {
return {
rightContentRender: () => <RightContent />,
disableContentMargin: false,
waterMarkProps: {
content: initialState?.currentUser?.name,
},
footerRender: () => <Footer />,
onPageChange: () => {
const { location } = history; // 如果没有登录,重定向到 login
// console.log('onchange,',location,initialState)
if (location.pathname === '/init' && !initialState?.user) {
return;
}
if (!initialState?.user && location.pathname !== loginPath) {
history.push(loginPath);
}
if (location.pathname == loginPath && Boolean(initialState?.user)) {
history.push('/');
}
},
links: isDev
? [
<Link key="openapi" to="/umi/plugin/openapi" target="_blank">
<LinkOutlined />
<span>OpenAPI 文档</span>
</Link>,
<Link to="/~docs" key="docs">
<BookOutlined />
<span>业务组件文档</span>
</Link>,
]
: [],
menuHeaderRender: undefined,
// 自定义 403 页面
// unAccessible: <div>unAccessible</div>,
// 增加一个 loading 的状态
childrenRender: (children, props) => {
// if (initialState?.loading) return <PageLoading />;
return (
<>
{children}
{!props.location?.pathname?.includes('/login') && (
<SettingDrawer
disableUrlParams
enableDarkTheme
settings={initialState?.settings}
onSettingChange={(settings) => {
setInitialState((preInitialState) => ({ ...preInitialState, settings }));
}}
/>
)}
</>
);
},
...initialState?.settings,
};
};
export const request = {
errorConfig: {
adaptor: (resData) => {
return {
...resData,
success: resData?.statusCode == 200 || resData?.statusCode == 233,
errorMessage: resData.message,
};
},
},
requestInterceptors: [
(url, options) => {
return {
url: url,
options: {
...options,
interceptors: true,
headers: {
token: (() => {
return window.localStorage.getItem('token') || 'null';
})(),
},
},
};
},
],
// responseInterceptors: [
// response => {
// if (response.statusCode === 233) {
// console.log("go to init!")
// // window.location.pathname = '/init'
// history.push('/init')
// return response
// } else {
// return response
// }
// }
// ]
};