request.js 3.7 KB

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