request.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * @Author: Rindy
  3. * @Date: 2021-04-25 15:58:21
  4. * @LastEditors: Rindy
  5. * @LastEditTime: 2021-05-08 15:49:54
  6. * @Description: 注释
  7. */
  8. // @ts-nocheck
  9. import axios from 'axios';
  10. import browser from './browser';
  11. // TextDecoder polyfills for lower browser
  12. if (undefined === window.TextEncoder) {
  13. window.TextEncoder = class _TextEncoder {
  14. encode(s) {
  15. return unescape(encodeURIComponent(s))
  16. .split('')
  17. .map(function (val) {
  18. return val.charCodeAt();
  19. });
  20. }
  21. };
  22. window.TextDecoder = class _TextDecoder {
  23. decode(code_arr) {
  24. return decodeURIComponent(escape(String.fromCharCode.apply(null, code_arr)));
  25. }
  26. };
  27. }
  28. const fetch = axios.create();
  29. fetch.interceptors.request.use(
  30. (config) => {
  31. const token = browser.getURLParam('token');
  32. if (token) {
  33. config.headers['token'] = token;
  34. } else {
  35. config.headers['token'] = 123;
  36. }
  37. return config;
  38. },
  39. (error) => {
  40. return Promise.reject(error);
  41. },
  42. );
  43. fetch.interceptors.response.use(
  44. (response) => {
  45. // 正常的文件流
  46. if (!/json/gi.test(response.headers['content-type'])) {
  47. return response.data;
  48. }
  49. // 以文件流方式请求但是返回json,需要解析为JSON对象
  50. if (response.request.responseType === 'arraybuffer') {
  51. const enc = new TextDecoder('utf-8');
  52. const res = JSON.parse(enc.decode(new Uint8Array(response.data)));
  53. return res;
  54. }
  55. return response.data;
  56. },
  57. (error) => {
  58. console.error(error);
  59. },
  60. );
  61. const http = {
  62. retry(func, retries = 0, delay = 1000) {
  63. return new Promise((resolve, reject) => {
  64. func()
  65. .then(resolve)
  66. .catch((error) => {
  67. if (retries <= 1) {
  68. reject(error);
  69. } else {
  70. setTimeout(() => {
  71. http
  72. .retry(func, retries - 1, delay)
  73. .then(resolve)
  74. .catch(reject);
  75. }, delay);
  76. }
  77. });
  78. });
  79. },
  80. get(url, data) {
  81. return fetch({
  82. method: 'get',
  83. url: url,
  84. params: data,
  85. });
  86. },
  87. getImage(url, retries = 3) {
  88. return http.retry(
  89. () =>
  90. new Promise((resolve, reject) => {
  91. const img = new Image();
  92. img.src = url;
  93. img.crossOrigin = 'anonymous';
  94. img.onload = function () {
  95. resolve(img);
  96. };
  97. img.onerror = function () {
  98. reject(`[${url}] load fail`);
  99. };
  100. }),
  101. retries,
  102. );
  103. },
  104. getBueffer(url) {
  105. return fetch.get(url, {
  106. responseType: 'arraybuffer',
  107. });
  108. },
  109. getBlob(url) {
  110. return fetch.get(url, {
  111. responseType: 'blob',
  112. });
  113. },
  114. post(url, data) {
  115. return fetch.post(url, data);
  116. },
  117. postFile(url, data) {
  118. const form = new FormData();
  119. let cb = null;
  120. if (data.onUploadProgress) {
  121. cb = data.onUploadProgress;
  122. delete data.onUploadProgress;
  123. }
  124. for (const key in data) {
  125. // if (key === 'files' && data[key].length > 0) {
  126. // for (let i = 0; i < data[key].length; i++) {
  127. // form.append(key, data[key][i])
  128. // }
  129. // } else {
  130. // form.append(key, data[key])
  131. // }
  132. form.append(key, data[key]);
  133. }
  134. return fetch.post(url, form, {
  135. headers: {
  136. 'Content-Type': 'multipart/form-data',
  137. },
  138. onUploadProgress: cb,
  139. });
  140. },
  141. };
  142. export { http, fetch };