request.ts 3.2 KB

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