request.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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) {
  73. return fetch.get(url)
  74. },
  75. getImage(url, retries = 3) {
  76. return http.retry(
  77. () =>
  78. new Promise((resolve, reject) => {
  79. let img = new Image()
  80. img.src = url
  81. img.crossOrigin = 'anonymous'
  82. img.onload = function() {
  83. resolve(img)
  84. }
  85. img.onerror = function() {
  86. reject(`[${url}] load fail`)
  87. }
  88. }),
  89. retries
  90. )
  91. },
  92. getBueffer(url) {
  93. return fetch.get(url, {
  94. responseType: 'arraybuffer'
  95. })
  96. },
  97. getBlob(url) {
  98. return fetch.get(url, {
  99. responseType: 'blob'
  100. })
  101. },
  102. post(url, data) {
  103. return fetch.post(url, data)
  104. },
  105. postFile(url, data) {
  106. const form = new FormData()
  107. let cb = null
  108. if (data.onUploadProgress) {
  109. cb = data.onUploadProgress
  110. delete data.onUploadProgress
  111. }
  112. for (let key in data) {
  113. // if (key === 'files' && data[key].length > 0) {
  114. // for (let i = 0; i < data[key].length; i++) {
  115. // form.append(key, data[key][i])
  116. // }
  117. // } else {
  118. // form.append(key, data[key])
  119. // }
  120. form.append(key, data[key])
  121. }
  122. return fetch.post(url, form, {
  123. headers: {
  124. 'Content-Type': 'multipart/form-data'
  125. },
  126. onUploadProgress: cb
  127. })
  128. }
  129. }
  130. export { http, fetch }