request.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. console.log(userStore.getUserInfo, 'userStore');
  24. // if (
  25. // userStore.getUserInfo &&
  26. // userStore.getUserInfo.schoolInfos &&
  27. // userStore.getUserInfo.schoolInfos[0]?.id &&
  28. // options.data
  29. // ) {
  30. // // console.log(
  31. // // userStore.getUserInfo && userStore.getUserInfo.schoolInfos[0]?.id,
  32. // // ' userStore.getUserInfo && userStore.getUserInfo.schoolInfos[0]?.id',
  33. // // options
  34. // // );
  35. // options.data['schoolId'] =
  36. // (userStore.getUserInfo && userStore.getUserInfo.schoolInfos[0]?.id) ||
  37. // '';
  38. // }
  39. if (
  40. Authorization &&
  41. !['/api-oauth/userlogin', '/api-auth/open/sendSms'].includes(url)
  42. ) {
  43. authHeaders.Authorization = Authorization;
  44. }
  45. return {
  46. url,
  47. options: {
  48. ...options,
  49. params: cleanDeep(options.params),
  50. data: cleanDeep(options.data),
  51. headers: {
  52. ...options.headers,
  53. ...authHeaders
  54. }
  55. }
  56. };
  57. },
  58. { global: false }
  59. );
  60. request.interceptors.response.use(
  61. async (res: any) => {
  62. const userStore = useUserStore();
  63. if (res.status > 299 || res.status < 200) {
  64. const msg = '服务器错误,状态码' + res.status;
  65. window.$message.error(msg);
  66. throw new Error(msg);
  67. }
  68. const data = await res.clone().json();
  69. if (
  70. data.code === 401 ||
  71. data.code === 4001 ||
  72. data.code == 403 ||
  73. data.code == 5000
  74. ) {
  75. userStore.logout(); // 删除登录 - 清除缓存
  76. router.replace('/login');
  77. location.reload();
  78. return;
  79. }
  80. // if (
  81. // (((data.code < 200 && data.code != 100) ||
  82. // (data.code >= 300 && data.code != 100)) &&
  83. // data.code != 0 &&
  84. // data.code == 5200) ||
  85. // data.code == 5400 ||
  86. // (data.code >= 5000 && data.code < 6000) ||
  87. // data.code == -1
  88. // ) {
  89. // const str = res.message || `请求失败code码为${data.code}`;
  90. // window.$message.error(str);
  91. // throw new Error(str);
  92. // }
  93. if (data.code !== 200 && data.errCode !== 0) {
  94. const msg = data.msg || data.message || '处理失败,请重试';
  95. if (!(data.code === 403 || data.code === 401)) {
  96. window.$message.error(msg);
  97. }
  98. throw new Error(msg);
  99. }
  100. return res;
  101. },
  102. { global: false }
  103. );
  104. export default request;