app.jsx 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { SettingDrawer } from '@ant-design/pro-layout';
  2. import { PageLoading } from '@ant-design/pro-layout';
  3. import { history, Link } from 'umi';
  4. import RightContent from '@/components/RightContent';
  5. import Footer from '@/components/Footer';
  6. import { currentUser as queryCurrentUser } from './services/ant-design-pro/api';
  7. import defaultSettings from '../config/defaultSettings';
  8. const isDev = process.env.NODE_ENV === 'development';
  9. const loginPath = '/user/login';
  10. /** 获取用户信息比较慢的时候会展示一个 loading */
  11. export const initialStateConfig = {
  12. loading: <PageLoading />,
  13. };
  14. /**
  15. * @see https://umijs.org/zh-CN/plugins/plugin-initial-state
  16. * */
  17. export async function getInitialState() {
  18. const fetchUserInfo = async () => {
  19. try {
  20. const msg = await queryCurrentUser();
  21. return msg.data;
  22. } catch (error) {
  23. history.push(loginPath);
  24. }
  25. return undefined;
  26. }; // 如果是登录页面,不执行
  27. if (history.location.pathname !== loginPath) {
  28. const currentUser = await fetchUserInfo();
  29. return {
  30. fetchUserInfo,
  31. currentUser,
  32. settings: defaultSettings,
  33. };
  34. }
  35. return {
  36. fetchUserInfo,
  37. settings: defaultSettings,
  38. };
  39. } // ProLayout 支持的api https://procomponents.ant.design/components/layout
  40. export const layout = ({ initialState, setInitialState }) => {
  41. return {
  42. rightContentRender: () => <RightContent />,
  43. disableContentMargin: false,
  44. footerRender: () => <Footer />,
  45. onPageChange: () => {
  46. const { location } = history; // 如果没有登录,重定向到 login
  47. if (!initialState?.currentUser && location.pathname !== loginPath) {
  48. history.push(loginPath);
  49. }
  50. },
  51. menuHeaderRender: undefined,
  52. // 自定义 403 页面
  53. // unAccessible: <div>unAccessible</div>,
  54. // 增加一个 loading 的状态
  55. childrenRender: (children, props) => {
  56. // if (initialState?.loading) return <PageLoading />;
  57. return (
  58. <>
  59. {children}
  60. {!props.location?.pathname?.includes('/login') && (
  61. <SettingDrawer
  62. enableDarkTheme
  63. settings={initialState?.settings}
  64. onSettingChange={(settings) => {
  65. setInitialState((preInitialState) => ({ ...preInitialState, settings }));
  66. }}
  67. />
  68. )}
  69. </>
  70. );
  71. },
  72. ...initialState?.settings,
  73. };
  74. };