request.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 } 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. // 为了处理课堂乐器后台预览课件的功能
  28. const authSource = sessionStorage.getItem('authSource');
  29. if (authSource === 'admin' && storage.get(ACCESS_TOKEN_ADMIN)) {
  30. Authorization = storage.get(ACCESS_TOKEN_ADMIN);
  31. }
  32. const authHeaders: any = {};
  33. // if (
  34. // userStore.getUserInfo &&
  35. // userStore.getUserInfo.schoolInfos &&
  36. // userStore.getUserInfo.schoolInfos[0]?.id &&
  37. // options.data
  38. // ) {
  39. // options.data['schoolId'] =
  40. // (userStore.getUserInfo && userStore.getUserInfo.schoolInfos[0]?.id) ||
  41. // '';
  42. // }
  43. if (
  44. userStore.getUserInfo &&
  45. (userStore.getUserInfo.schoolInfos as any) &&
  46. userStore.getUserInfo.schoolInfos[0]?.id
  47. ) {
  48. // console.log(
  49. // userStore.getUserInfo && userStore.getUserInfo.schoolInfos[0]?.id,
  50. // ' userStore.getUserInfo && userStore.getUserInfo.schoolInfos[0]?.id',
  51. // options
  52. // );
  53. options.headers['schoolId'] =
  54. (userStore.getUserInfo && userStore.getUserInfo.schoolInfos[0]?.id) ||
  55. '';
  56. }
  57. if (
  58. Authorization &&
  59. !['/api-oauth/userlogin', '/api-auth/open/sendSms'].includes(url)
  60. ) {
  61. authHeaders.Authorization = Authorization;
  62. }
  63. return {
  64. url,
  65. options: {
  66. ...options,
  67. params: cleanDeep(options.params),
  68. data: cleanDeep(options.data),
  69. headers: {
  70. ...options.headers,
  71. ...authHeaders
  72. }
  73. }
  74. };
  75. },
  76. { global: false }
  77. );
  78. request.interceptors.response.use(
  79. async (res: any) => {
  80. const userStore = useUserStore();
  81. if (res.status > 299 || res.status < 200) {
  82. const msg = '服务器错误,状态码' + res.status;
  83. // 判断是否有资源需要证书,不提示错误信息
  84. if (res.status === 511) {
  85. eventGlobal.emit('auth-not-installed');
  86. } else {
  87. !hideErrorMesage && window.$message.error(msg);
  88. }
  89. throw new Error(msg);
  90. }
  91. const data = await res.clone().json();
  92. if (
  93. data.code === 401 ||
  94. data.code === 4001 ||
  95. data.code == 403 ||
  96. data.code == 5000
  97. ) {
  98. userStore.logout(); // 删除登录 - 清除缓存
  99. router.replace('/login');
  100. location.reload();
  101. return;
  102. }
  103. // if (
  104. // (((data.code < 200 && data.code != 100) ||
  105. // (data.code >= 300 && data.code != 100)) &&
  106. // data.code != 0 &&
  107. // data.code == 5200) ||
  108. // data.code == 5400 ||
  109. // (data.code >= 5000 && data.code < 6000) ||
  110. // data.code == -1
  111. // ) {
  112. // const str = res.message || `请求失败code码为${data.code}`;
  113. // window.$message.error(str);
  114. // throw new Error(str);
  115. // }
  116. if (data.code !== 200 && data.errCode !== 0) {
  117. const msg = data.msg || data.message || '处理失败,请重试';
  118. if (!(data.code === 403 || data.code === 401)) {
  119. window.$message.error(msg);
  120. }
  121. throw new Error(msg);
  122. }
  123. return res;
  124. },
  125. { global: false }
  126. );
  127. export default request;