app.tsx 3.3 KB

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