request.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. request.interceptors.request.use(
  24. (url, options: any) => {
  25. if (!options.hideLoading) {
  26. clearTimeout(toast);
  27. showLoadingToast({
  28. message: '加载中...',
  29. forbidClick: true,
  30. duration: 0
  31. });
  32. }
  33. initRequest = options.initRequest || false;
  34. const Authorization = storage.get(ACCESS_TOKEN) || '';
  35. const authHeaders: any = {};
  36. if (
  37. Authorization &&
  38. ![
  39. '/edu-app/userlogin',
  40. '/edu-app/smsLogin',
  41. '/edu-app/open/sendSms'
  42. ].includes(url) &&
  43. !options.noAuthorization
  44. ) {
  45. authHeaders.Authorization = Authorization;
  46. }
  47. return {
  48. url,
  49. options: {
  50. ...options,
  51. params: cleanDeep(options.params),
  52. data: cleanDeep(options.data),
  53. headers: {
  54. ...options.headers,
  55. ...authHeaders
  56. }
  57. }
  58. };
  59. },
  60. { global: false }
  61. );
  62. request.interceptors.response.use(
  63. async res => {
  64. toast = setTimeout(() => {
  65. closeToast();
  66. }, 100);
  67. if (res.status > 299 || res.status < 200) {
  68. const msg = '服务器错误,状态码' + res.status;
  69. clearTimeout(toast);
  70. setTimeout(() => {
  71. showToast(msg);
  72. }, 60);
  73. throw new Error(msg);
  74. }
  75. const data = await res.clone().json();
  76. // 999 为特殊code码
  77. const otherCode = [999, 5435, 5436, 5437];
  78. if (
  79. data.code !== 200 &&
  80. data.errCode !== 0 &&
  81. !otherCode.includes(data.code)
  82. ) {
  83. let msg = data.msg || data.message || '处理失败,请重试';
  84. if (initRequest) {
  85. if (data.code === 403 || data.code === 5000) {
  86. setLogout();
  87. } else {
  88. setLoginError();
  89. }
  90. }
  91. if (!(data.code === 403 || data.code === 5000)) {
  92. clearTimeout(toast);
  93. setTimeout(() => {
  94. showToast(msg);
  95. }, 60);
  96. }
  97. const browserInfo = browser();
  98. if (data.code === 5000 || data.code === 403) {
  99. msg += ' authentication ' + data.code;
  100. if (browserInfo.isApp) {
  101. postMessage({
  102. api: 'login'
  103. });
  104. } else {
  105. setLogout();
  106. }
  107. }
  108. throw new Error(msg);
  109. }
  110. return res;
  111. },
  112. { global: false }
  113. );
  114. export default request;