()
| 26 | * @see https://umijs.org/zh-CN/plugins/plugin-initial-state |
| 27 | * */ |
| 28 | export async function getInitialState() { |
| 29 | // 获取 LAYOUT 的值 |
| 30 | const Layout_Settings = getLocalStorageItem<LayoutSettings>(LOCAL_STORAGE.LAYOUT) || defaultSettings; |
| 31 | // 获取 ACCESS_TOKEN |
| 32 | const ACCESS_TOKEN = getLocalStorageItem<string>(LOCAL_STORAGE.ACCESS_TOKEN) || undefined; |
| 33 | // 存储到 localstorage |
| 34 | setLocalStorageItem(LOCAL_STORAGE.LAYOUT, Layout_Settings) |
| 35 | // 初始化多语言 |
| 36 | const Locales = get(await getAllLocalesLang(), 'data', {}) |
| 37 | // 动态添加多语言 |
| 38 | if (!isEmpty(Locales) && !isNil(Locales)) { |
| 39 | forEach(Locales, (value: Record<string, string>, key: Langs) => { |
| 40 | addLocale(key, value, ANTD_LANGS[key]); |
| 41 | }) |
| 42 | } |
| 43 | // 初始化数据 |
| 44 | const initialState: InitialStateTypes = { |
| 45 | Locales, |
| 46 | Access_token: ACCESS_TOKEN, |
| 47 | Settings: Layout_Settings, |
| 48 | Collapsed: false, |
| 49 | } |
| 50 | // 判断是否登录,没有登录跳转到登录页 |
| 51 | if (!ACCESS_TOKEN) { |
| 52 | history.push(ROUTES.LOGIN); |
| 53 | return initialState |
| 54 | } |
| 55 | // 判断在登录页是否已登录,已登录则跳转主页 |
| 56 | if (eq(location.pathname, ROUTES.LOGIN) && ACCESS_TOKEN) { |
| 57 | history.push('/'); |
| 58 | } |
| 59 | // 如果不是登录页面,执行 |
| 60 | if (!eq(location.pathname, ROUTES.LOGIN)) { |
| 61 | const result = await initUserAuthority() |
| 62 | // 初始化全局状态 |
| 63 | return assign(initialState, result) |
| 64 | } |
| 65 | return initialState |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @description: 全局 lyout 布局 |
nothing calls this directly
no test coverage detected