app.tsx 3.0 KB

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