1
0

app.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import Footer from '@/components/Footer';
  2. import RightContent from '@/components/RightContent';
  3. import type { Settings as LayoutSettings } from '@ant-design/pro-components';
  4. import { PageLoading, SettingDrawer } from '@ant-design/pro-components';
  5. import type { RunTimeLayoutConfig } from 'umi';
  6. import { history } from 'umi';
  7. import defaultSettings from '../config/defaultSettings';
  8. import { currentUser as queryCurrentUser } from './services/ant-design-pro/api';
  9. import { message } from 'antd';
  10. const loginPath = '/user/login';
  11. /** 获取用户信息比较慢的时候会展示一个 loading */
  12. export const initialStateConfig = {
  13. loading: <PageLoading />,
  14. };
  15. /**
  16. * @see https://umijs.org/zh-CN/plugins/plugin-initial-state
  17. * */
  18. export async function getInitialState(): Promise<{
  19. settings?: Partial<LayoutSettings>;
  20. currentUser?: API.CurrentUser;
  21. loading?: boolean;
  22. fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
  23. }> {
  24. const fetchUserInfo = async () => {
  25. try {
  26. const msg: any = await queryCurrentUser(localStorage.getItem('token'));
  27. return {
  28. name: msg.data.user_name,
  29. };
  30. } catch (error) {
  31. history.push(loginPath);
  32. }
  33. return undefined;
  34. };
  35. // 如果不是登录页面,执行
  36. if (history.location.pathname !== loginPath) {
  37. const currentUser = await fetchUserInfo();
  38. return {
  39. fetchUserInfo,
  40. currentUser,
  41. settings: defaultSettings,
  42. };
  43. }
  44. return {
  45. fetchUserInfo,
  46. settings: defaultSettings,
  47. };
  48. }
  49. // ProLayout 支持的api https://procomponents.ant.design/components/layout
  50. export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) => {
  51. return {
  52. rightContentRender: () => <RightContent />,
  53. disableContentMargin: false,
  54. waterMarkProps: {
  55. content: initialState?.currentUser?.name,
  56. },
  57. footerRender: () => <Footer />,
  58. onPageChange: () => {
  59. const { location } = history;
  60. // 如果没有登录,重定向到 login
  61. if (!initialState?.currentUser && location.pathname !== loginPath) {
  62. history.push(loginPath);
  63. }
  64. },
  65. menuHeaderRender: undefined,
  66. // 自定义 403 页面
  67. // unAccessible: <div>unAccessible</div>,
  68. // 增加一个 loading 的状态
  69. childrenRender: (children: any, props: { location: { pathname: string | string[] } }) => {
  70. // if (initialState?.loading) return <PageLoading />;
  71. return (
  72. <>
  73. {children}
  74. {!props.location?.pathname?.includes('/login') && (
  75. <SettingDrawer
  76. disableUrlParams
  77. enableDarkTheme
  78. settings={initialState?.settings}
  79. onSettingChange={(settings) => {
  80. setInitialState((preInitialState: any) => ({
  81. ...preInitialState,
  82. settings,
  83. }));
  84. }}
  85. />
  86. )}
  87. </>
  88. );
  89. },
  90. ...initialState?.settings,
  91. };
  92. };
  93. const addToken = async (url: any, options: any) => {
  94. // 此处为拦截器,每次发送请求之前判断能否取到token
  95. if (localStorage.getItem('token')) {
  96. const headers = {
  97. Authorization: `${localStorage.getItem('token')}`,
  98. };
  99. return {
  100. url,
  101. options: { ...options, headers },
  102. };
  103. }
  104. return false;
  105. };
  106. const MainResponseInterceptors = (response: { status: number }) => {
  107. if (response.status === 401) {
  108. message.warn('帐号登录过期,请重新登录');
  109. history.push(loginPath);
  110. }
  111. return response;
  112. };
  113. export const request = {
  114. errorConfig: {
  115. adaptor: (resData: any) => {
  116. return {
  117. showType: resData.code ? 2 : 0,
  118. errorMessage: resData.message,
  119. };
  120. },
  121. },
  122. requestInterceptors: [addToken],
  123. responseInterceptors: [MainResponseInterceptors],
  124. };