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
| import { Footer, Question, SelectLang, AvatarDropdown, AvatarName } from '@/components'; import { LinkOutlined } from '@ant-design/icons'; 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, Link } from '@umijs/max'; import defaultSettings from '../config/defaultSettings'; import { errorConfig } from './requestErrorConfig'; import { currentUser as queryCurrentUser } from '@/services/ant-design-pro/api'; import React from 'react'; const isDev = process.env.NODE_ENV === 'development'; const loginPath = '/user/login';
export async function getInitialState(): Promise<{ settings?: Partial<LayoutSettings>; currentUser?: API.CurrentUser; loading?: boolean; fetchUserInfo?: () => Promise<API.CurrentUser | undefined>; }> { const fetchUserInfo = async () => { try { const msg = await queryCurrentUser({ skipErrorHandler: true, }); return msg.data; } catch (error) { history.push(loginPath); } return undefined; }; const { location } = history; if (location.pathname !== loginPath) { const currentUser = await fetchUserInfo(); return { fetchUserInfo, currentUser, settings: defaultSettings as Partial<LayoutSettings>, }; } return { fetchUserInfo, settings: defaultSettings as Partial<LayoutSettings>, }; }
export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) => { return { actionsRender: () => [<Question key="doc" />, <SelectLang key="SelectLang" />], avatarProps: { src: initialState?.currentUser?.avatar, title: <AvatarName />, render: (_, avatarChildren) => { return <AvatarDropdown>{avatarChildren}</AvatarDropdown>; }, }, waterMarkProps: { content: initialState?.currentUser?.name, }, footerRender: () => <Footer />, onPageChange: () => { const { location } = history; if (!initialState?.currentUser && location.pathname !== loginPath) { history.push(loginPath); } }, bgLayoutImgList: [ { src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/D2LWSqNny4sAAAAAAAAAAAAAFl94AQBr', left: 85, bottom: 100, height: '303px', }, { src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/C2TWRpJpiC0AAAAAAAAAAAAAFl94AQBr', bottom: -68, right: -45, height: '303px', }, { src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/F6vSTbj8KpYAAAAAAAAAAAAAFl94AQBr', bottom: 0, left: 0, width: '331px', }, ], links: isDev ? [ <Link key="openapi" to="/umi/plugin/openapi" target="_blank"> <LinkOutlined /> <span>OpenAPI 文档</span> </Link>, ] : [], menuHeaderRender: undefined, childrenRender: (children) => { return ( <> {children} {isDev && ( <SettingDrawer disableUrlParams enableDarkTheme settings={initialState?.settings} onSettingChange={(settings) => { setInitialState((preInitialState) => ({ ...preInitialState, settings, })); }} /> )} </> ); }, ...initialState?.settings, }; };
export const request = { baseURL: isDev ? 'http://localhost:8080' : 'http://usercenter.hejiajun.icu', withCredentials: true, ...errorConfig, };
|