request.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { extend } from 'umi-request';
  2. import cleanDeep from 'clean-deep';
  3. import { useUserStore } from '../store/modules/users';
  4. import router from '@/router';
  5. import { eventGlobal, getAuthForAdmin } from '.';
  6. import { storage } from './storage';
  7. import { ACCESS_TOKEN_ADMIN } from '../store/mutation-types';
  8. export interface SearchInitParams {
  9. rows?: string | number;
  10. page?: string | number;
  11. }
  12. let hideErrorMesage = false; // 是否显示错误信息
  13. const request = extend({
  14. // requestType: 'form',
  15. hideLoading: true, // 默认都不显示加载
  16. timeout: 20000,
  17. timeoutMessage: '请求超时'
  18. });
  19. request.interceptors.request.use(
  20. (url, options: any) => {
  21. hideErrorMesage = options.hideErrorMesage || false;
  22. if (!options.hideLoading) {
  23. window.$message.loading('加载中...');
  24. }
  25. const userStore = useUserStore();
  26. let Authorization = userStore.getToken || '';
  27. const authHeaders: any = {};
  28. // if (
  29. // userStore.getUserInfo &&
  30. // userStore.getUserInfo.schoolInfos &&
  31. // userStore.getUserInfo.schoolInfos[0]?.id &&
  32. // options.data
  33. // ) {
  34. // options.data['schoolId'] =
  35. // (userStore.getUserInfo && userStore.getUserInfo.schoolInfos[0]?.id) ||
  36. // '';
  37. // }
  38. if (
  39. userStore.getUserInfo &&
  40. (userStore.getUserInfo.schoolInfos as any) &&
  41. userStore.getUserInfo.schoolInfos[0]?.id
  42. ) {
  43. // console.log(
  44. // userStore.getUserInfo && userStore.getUserInfo.schoolInfos[0]?.id,
  45. // ' userStore.getUserInfo && userStore.getUserInfo.schoolInfos[0]?.id',
  46. // options
  47. // );
  48. options.headers['schoolId'] =
  49. (userStore.getUserInfo && userStore.getUserInfo.schoolInfos[0]?.id) ||
  50. '';
  51. }
  52. if (
  53. Authorization &&
  54. !['/api-oauth/userlogin', '/api-auth/open/sendSms'].includes(url)
  55. ) {
  56. authHeaders.Authorization = Authorization;
  57. }
  58. return {
  59. url,
  60. options: {
  61. ...options,
  62. params: cleanDeep(options.params),
  63. data: cleanDeep(options.data),
  64. headers: {
  65. ...options.headers,
  66. ...authHeaders
  67. }
  68. }
  69. };
  70. },
  71. { global: false }
  72. );
  73. request.interceptors.response.use(
  74. async (res: any) => {
  75. const userStore = useUserStore();
  76. if (res.status > 299 || res.status < 200) {
  77. const msg = '服务器错误,状态码' + res.status;
  78. // 判断是否有资源需要证书,不提示错误信息
  79. if (res.status === 511) {
  80. eventGlobal.emit('auth-not-installed');
  81. } else {
  82. !hideErrorMesage && window.$message.error(msg);
  83. }
  84. throw new Error(msg);
  85. }
  86. const data = await res.clone().json();
  87. if (
  88. data.code === 401 ||
  89. data.code === 4001 ||
  90. data.code == 403 ||
  91. data.code == 5000
  92. ) {
  93. userStore.logout(); // 删除登录 - 清除缓存
  94. router.replace('/login');
  95. location.reload();
  96. return;
  97. }
  98. // if (
  99. // (((data.code < 200 && data.code != 100) ||
  100. // (data.code >= 300 && data.code != 100)) &&
  101. // data.code != 0 &&
  102. // data.code == 5200) ||
  103. // data.code == 5400 ||
  104. // (data.code >= 5000 && data.code < 6000) ||
  105. // data.code == -1
  106. // ) {
  107. // const str = res.message || `请求失败code码为${data.code}`;
  108. // window.$message.error(str);
  109. // throw new Error(str);
  110. // }
  111. if (data.code !== 200 && data.errCode !== 0) {
  112. const msg = data.msg || data.message || '处理失败,请重试';
  113. if (!(data.code === 403 || data.code === 401)) {
  114. window.$message.error(msg);
  115. }
  116. throw new Error(msg);
  117. }
  118. return res;
  119. },
  120. { global: false }
  121. );
  122. export default request;