app.tsx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import HeaderLogo from '@/components/header-logo';
  2. import HeaderRight from '@/components/header-right';
  3. import adaptor, { errorThrower } from '@/utils/adaptor';
  4. import errorHandler from '@/utils/error-handler';
  5. import type { AxiosResponse, RequestConfig } from '@@/plugin-request/request';
  6. import { ProBreadcrumb } from '@ant-design/pro-layout';
  7. import { history } from 'core/mz';
  8. import './style/index.less';
  9. import { InitialState } from './types';
  10. interface InitDate {
  11. initialState: InitialState;
  12. }
  13. export async function getInitialState() {
  14. if (history.location?.pathname === '/login') {
  15. return {};
  16. } else {
  17. // const { data } = await services.user.getUserInfo();
  18. // if (data === null) {
  19. // history.replace('/login');
  20. // }
  21. // return {
  22. // userId: data?.userId,
  23. // userName: data?.userName
  24. // };
  25. }
  26. }
  27. export const layout = ({ initialState }: InitDate) => {
  28. return {
  29. headerBg: 'transparent',
  30. bodyBg: 'transparent',
  31. navTheme: 'light',
  32. layout: 'mix',
  33. title: '',
  34. siderWidth: 200,
  35. contentStyle: { padding: 0 },
  36. // 自定义页面标题的显示方法,浏览器选项卡显示的title信息
  37. pageTitleRender: () => 'Assistants',
  38. // 自定义产品Logo和文字
  39. logo: <HeaderLogo title={'Assistants'} />,
  40. // 自定义头部中间内容为面包屑导航
  41. headerContentRender: () => <ProBreadcrumb />,
  42. // 自定义头部导航右侧有关用户内容
  43. rightContentRender: () => <HeaderRight />
  44. // 验证用户身份
  45. // onPageChange: async () => {
  46. // if (!initialState?.userId) {
  47. // const user = await services.user.getUserInfo();
  48. // if (!user) {
  49. // history.push('/login');
  50. // return;
  51. // }
  52. // }
  53. // }
  54. };
  55. };
  56. // 配置 request
  57. export const request: RequestConfig = {
  58. baseURL: APIURL,
  59. validateStatus(status) {
  60. return status >= 200 && status < 300;
  61. },
  62. errorConfig: {
  63. errorHandler, // 自定义异常处理
  64. errorThrower
  65. },
  66. requestInterceptors: [
  67. (url, options) => {
  68. return {
  69. url,
  70. options: {
  71. ...options,
  72. headers: {
  73. ...options.headers,
  74. Authorization: 'Bearer ' + localStorage.getItem('myKey')
  75. }
  76. }
  77. };
  78. }
  79. ],
  80. responseInterceptors: [
  81. adaptor,
  82. (response: AxiosResponse) => {
  83. const code = String(response?.data?.code || response?.status);
  84. if (code === '401' || code === '-4004') {
  85. console.log('error:登录失效或身份失效!');
  86. history.push('/login');
  87. }
  88. return response;
  89. }
  90. ]
  91. };