request.ts 3.0 KB

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