request.ts 4.1 KB

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