request.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { extend } from 'umi-request';
  2. import cleanDeep from 'clean-deep';
  3. import { browser } from '@/helpers/utils';
  4. import { setLogout, setLoginError } from '@/state';
  5. import { postMessage } from './native-message';
  6. import { showLoadingToast, showToast, closeToast } from 'vant';
  7. import { storage } from '@/helpers/storage';
  8. import { ACCESS_TOKEN } from '@/store/mutation-types';
  9. export interface SearchInitParams {
  10. rows?: string | number;
  11. page?: string | number;
  12. }
  13. const request = extend({
  14. // requestType: 'form',
  15. noAuthorization: false, // 默认添加token,在有的情况下
  16. hideLoading: true, // 默认都不显示加载
  17. timeout: 20000,
  18. timeoutMessage: '请求超时'
  19. });
  20. // 是否是初始化接口
  21. let initRequest = false;
  22. let toast: ReturnType<typeof setTimeout>;
  23. let toastMsg: ReturnType<typeof setTimeout>;
  24. request.interceptors.request.use(
  25. (url, options: any) => {
  26. if (!options.hideLoading) {
  27. clearTimeout(toast);
  28. showLoadingToast({
  29. message: '加载中...',
  30. forbidClick: true,
  31. duration: 0
  32. });
  33. }
  34. initRequest = options.initRequest || false;
  35. const Authorization = storage.get(ACCESS_TOKEN) || '';
  36. const authHeaders: any = {};
  37. if (
  38. Authorization &&
  39. ![
  40. '/edu-app/userlogin',
  41. '/edu-app/smsLogin',
  42. '/edu-app/open/sendSms'
  43. ].includes(url) &&
  44. !options.noAuthorization
  45. ) {
  46. authHeaders.Authorization = Authorization;
  47. }
  48. return {
  49. url,
  50. options: {
  51. ...options,
  52. params: cleanDeep(options.params),
  53. data: cleanDeep(options.data),
  54. headers: {
  55. ...options.headers,
  56. ...authHeaders
  57. }
  58. }
  59. };
  60. },
  61. { global: false }
  62. );
  63. request.interceptors.response.use(
  64. async res => {
  65. toast = setTimeout(() => {
  66. closeToast();
  67. }, 100);
  68. if (res.status > 299 || res.status < 200) {
  69. const msg = '服务器错误,状态码' + res.status;
  70. clearTimeout(toast);
  71. clearTimeout(toastMsg);
  72. toastMsg = setTimeout(() => {
  73. showToast(msg);
  74. }, 60);
  75. throw new Error(msg);
  76. }
  77. const data = await res.clone().json();
  78. // 999 为特殊code码
  79. const otherCode = [999, 5435, 5436, 5437, 5439];
  80. if (
  81. data.code !== 200 &&
  82. data.errCode !== 0 &&
  83. !otherCode.includes(data.code)
  84. ) {
  85. let msg = data.msg || data.message || '处理失败,请重试';
  86. if (initRequest) {
  87. if (data.code === 403 || data.code === 5000) {
  88. setLogout();
  89. } else {
  90. setLoginError();
  91. }
  92. }
  93. if (!(data.code === 403 || data.code === 5000)) {
  94. clearTimeout(toast);
  95. clearTimeout(toastMsg);
  96. toastMsg = setTimeout(() => {
  97. showToast(msg);
  98. }, 60);
  99. }
  100. const browserInfo = browser();
  101. if (data.code === 5000 || data.code === 403) {
  102. msg += ' authentication ' + data.code;
  103. if (browserInfo.isApp) {
  104. postMessage({
  105. api: 'login'
  106. });
  107. } else {
  108. setLogout();
  109. }
  110. }
  111. throw new Error(msg);
  112. }
  113. return res;
  114. },
  115. { global: false }
  116. );
  117. export default request;