app.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { PageLoading } from '@ant-design/pro-layout';
  2. import { history, useModel } from 'umi';
  3. import { queryTenantData } from '@/services/sagacare_service/member';
  4. /** 获取用户信息比较慢的时候会展示一个 loading */
  5. export const initialStateConfig = {
  6. loading: <PageLoading />,
  7. };
  8. /**
  9. * @see https://umijs.org/zh-CN/plugins/plugin-initial-state
  10. * */
  11. export async function getInitialState(): Promise<{
  12. projectId: String;
  13. currentUser?: API.CurrentUser;
  14. companyConfig?: any;
  15. //fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
  16. }> {
  17. const { location } = history;
  18. var token = localStorage.getItem('token');
  19. // if (location.pathname.indexOf('login') == -1 && !token) {
  20. // history.push('/login');
  21. // }
  22. if (location.pathname.indexOf('login') > -1 && token) {
  23. history.push('/home');
  24. }
  25. //debugger;
  26. let currentUser = {
  27. appId: 'wxda2ef261ac3cca32',
  28. companyId: '17fe48afcdde4d6797583c6d6270a035', // ff80c708fe72446eb790b764c3a7b66b,
  29. loginType: 'app',
  30. manageUserType: 3,
  31. name: '赵静',
  32. openId: 'oDcGY5Qq3Vk-7WKTNsn-lLGNT118',
  33. phone: '13810794283',
  34. projectId: 'Pj1101080259', // Pj1101099999
  35. userId: 'db445a9cd7be4341a14448928014605c',
  36. };
  37. let projectId = 'Pj1101080259'; // Pj1101099999
  38. if (location.pathname.indexOf('login') == -1 && token) {
  39. var localUser = localStorage.getItem('user') || '';
  40. currentUser = JSON.parse(localUser);
  41. projectId = currentUser?.projectId;
  42. }
  43. let companyConfig = {};
  44. if (currentUser.userId) {
  45. const res = await queryTenantData(currentUser);
  46. companyConfig = res.data;
  47. }
  48. return {
  49. projectId,
  50. currentUser,
  51. companyConfig,
  52. };
  53. }
  54. export const request = {
  55. requestInterceptors: [
  56. (url: any, options: any) => {
  57. return {
  58. url,
  59. options: {
  60. ...options,
  61. interceptors: true,
  62. headers: {
  63. ...options.headers,
  64. 'saga-token': localStorage.getItem('token'),
  65. },
  66. },
  67. };
  68. },
  69. ],
  70. responseInterceptors: [
  71. async (response: any) => {
  72. return response;
  73. },
  74. ],
  75. errorHandler: (error: any) => {
  76. const { response } = error;
  77. if (!response) {
  78. // message.error('您的网络发生异常,无法连接服务器');
  79. }
  80. throw error;
  81. },
  82. };