app.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import Footer from '@/components/Footer';
  2. import { Question, SelectLang } from '@/components/RightContent';
  3. import { LinkOutlined } from '@ant-design/icons';
  4. import type { Settings as LayoutSettings } from '@ant-design/pro-components';
  5. import { SettingDrawer } from '@ant-design/pro-components';
  6. import type { RunTimeLayoutConfig } from '@umijs/max';
  7. import { history, Link } from '@umijs/max';
  8. import defaultSettings from '../config/defaultSettings';
  9. import { errorConfig } from './requestErrorConfig';
  10. import { currentUser as queryCurrentUser } from './services/ant-design-pro/api';
  11. import React from 'react';
  12. import { AvatarDropdown, AvatarName } from './components/RightContent/AvatarDropdown';
  13. const isDev = process.env.NODE_ENV === 'development';
  14. const loginPath = '/user/login';
  15. // 不显示页脚页面配置
  16. const doNotFooter = ['/opus'];
  17. /**
  18. * @see https://umijs.org/zh-CN/plugins/plugin-initial-state
  19. * */
  20. export async function getInitialState(): Promise<{
  21. settings?: Partial<LayoutSettings>;
  22. currentUser?: API.CurrentUser;
  23. loading?: boolean;
  24. fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
  25. }> {
  26. const fetchUserInfo = async () => {
  27. try {
  28. const msg = await queryCurrentUser({
  29. skipErrorHandler: true,
  30. });
  31. return msg.data;
  32. } catch (error) {
  33. history.push(loginPath);
  34. }
  35. return undefined;
  36. };
  37. // 如果不是登录页面,执行
  38. const { location } = history;
  39. if (location.pathname !== loginPath) {
  40. const currentUser = await fetchUserInfo();
  41. return {
  42. fetchUserInfo,
  43. currentUser: {
  44. avatar: `https://xsgames.co/randomusers/avatar.php?g=pixel&key=${Math.floor(Math.random() * 10)}`,
  45. ...currentUser
  46. },
  47. settings: defaultSettings as Partial<LayoutSettings>,
  48. };
  49. }
  50. return {
  51. fetchUserInfo,
  52. settings: defaultSettings as Partial<LayoutSettings>,
  53. };
  54. }
  55. // ProLayout 支持的api https://procomponents.ant.design/components/layout
  56. export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) => {
  57. return {
  58. actionsRender: () => [<Question key="doc" />], // <SelectLang key="SelectLang" />
  59. avatarProps: {
  60. src: initialState?.currentUser?.avatar || `https://xsgames.co/randomusers/avatar.php?g=pixel&key=${Math.floor(Math.random() * 20)}`,
  61. title: <AvatarName />,
  62. render: (_, avatarChildren) => {
  63. return <AvatarDropdown>{avatarChildren}</AvatarDropdown>;
  64. },
  65. },
  66. waterMarkProps: {
  67. content: initialState?.currentUser?.mobile,
  68. },
  69. footerRender: () => !doNotFooter.includes(location.pathname) ? <Footer /> : null,
  70. onPageChange: () => {
  71. const { location } = history;
  72. // 如果没有登录,重定向到 login
  73. if (!initialState?.currentUser && location.pathname !== loginPath) {
  74. history.push(loginPath);
  75. }
  76. },
  77. layoutBgImgList: [
  78. {
  79. src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/D2LWSqNny4sAAAAAAAAAAAAAFl94AQBr',
  80. left: 85,
  81. bottom: 100,
  82. height: '303px',
  83. },
  84. {
  85. src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/C2TWRpJpiC0AAAAAAAAAAAAAFl94AQBr',
  86. bottom: -68,
  87. right: -45,
  88. height: '303px',
  89. },
  90. {
  91. src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/F6vSTbj8KpYAAAAAAAAAAAAAFl94AQBr',
  92. bottom: 0,
  93. left: 0,
  94. width: '331px',
  95. },
  96. ],
  97. links: isDev
  98. ? [
  99. <Link key="openapi" to="http://47.97.38.17:9988/swagger-ui.html#/" target="_blank">
  100. <LinkOutlined />
  101. <span>OpenAPI 文档</span>
  102. </Link>,
  103. ]
  104. : [],
  105. menuHeaderRender: undefined,
  106. // 自定义 403 页面
  107. // unAccessible: <div>unAccessible</div>,
  108. // 增加一个 loading 的状态
  109. childrenRender: (children) => {
  110. // if (initialState?.loading) return <PageLoading />;
  111. return (
  112. <>
  113. {children}
  114. <SettingDrawer
  115. disableUrlParams
  116. enableDarkTheme
  117. settings={initialState?.settings}
  118. onSettingChange={(settings) => {
  119. setInitialState((preInitialState) => ({
  120. ...preInitialState,
  121. settings,
  122. }));
  123. }}
  124. />
  125. </>
  126. );
  127. },
  128. ...initialState?.settings,
  129. };
  130. };
  131. /**
  132. * @name request 配置,可以配置错误处理
  133. * 它基于 axios 和 ahooks 的 useRequest 提供了一套统一的网络请求和错误处理方案。
  134. * @doc https://umijs.org/docs/max/request#配置
  135. */
  136. export const request = {
  137. ...errorConfig
  138. };