消毒机前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.0 KiB

2 years ago
  1. import axios from 'axios'
  2. class Request {
  3. // axios 实例
  4. instance
  5. // 拦截器对象
  6. interceptorsObj
  7. /*
  8. 存放取消方法的集合
  9. * 在创建请求后将取消请求方法 push 到该集合中
  10. * 封装一个方法可以取消请求传入 url: string|string[]
  11. * 在请求之前判断同一URL是否存在如果存在就取消请求
  12. */
  13. cancelRequestSourceList
  14. /*
  15. 存放所有请求URL的集合
  16. * 请求之前需要将url push到该集合中
  17. * 请求完毕后将url从集合中删除
  18. * 添加在发送请求之前完成删除在响应之后删除
  19. */
  20. requestUrlList
  21. constructor(config) {
  22. this.requestUrlList = []
  23. this.cancelRequestSourceList = []
  24. this.instance = axios.create(config)
  25. this.interceptorsObj = config.interceptors
  26. // 拦截器执行顺序 接口请求 -> 实例请求 -> 全局请求 -> 实例响应 -> 全局响应 -> 接口响应
  27. this.instance.interceptors.request.use(
  28. res => res,
  29. err => err,
  30. )
  31. // 使用实例拦截器
  32. this.instance.interceptors.request.use(
  33. this.interceptorsObj?.requestInterceptors,
  34. this.interceptorsObj?.requestInterceptorsCatch,
  35. )
  36. this.instance.interceptors.response.use(
  37. this.interceptorsObj?.responseInterceptors,
  38. this.interceptorsObj?.responseInterceptorsCatch,
  39. )
  40. // 全局响应拦截器保证最后执行
  41. this.instance.interceptors.response.use(
  42. // 因为我们接口的数据都在res.data下,所以我们直接返回res.data
  43. res => {
  44. return res.data
  45. },
  46. err => err,
  47. )
  48. }
  49. /**
  50. * @description: 获取指定 url cancelRequestSourceList 中的索引
  51. * @param {string} url
  52. * @returns {number} 索引位置
  53. */
  54. getSourceIndex(url) {
  55. return this.cancelRequestSourceList?.findIndex(item => {
  56. return Object.keys(item)[0] === url
  57. })
  58. }
  59. /**
  60. * @description: 删除 requestUrlList cancelRequestSourceList
  61. * @param {string} url
  62. * @returns {*}
  63. */
  64. delUrl(url) {
  65. const urlIndex = this.requestUrlList?.findIndex(u => u === url)
  66. const sourceIndex = this.getSourceIndex(url)
  67. // 删除url和cancel方法
  68. urlIndex !== -1 && this.requestUrlList?.splice(urlIndex, 1)
  69. sourceIndex !== -1 && this.cancelRequestSourceList?.splice(sourceIndex, 1)
  70. }
  71. request(config) {
  72. return new Promise((resolve, reject) => {
  73. // 如果我们为单个请求设置拦截器,这里使用单个请求的拦截器
  74. if (config.interceptors?.requestInterceptors) {
  75. config = config.interceptors.requestInterceptors(config)
  76. }
  77. const url = config.url
  78. // url存在保存取消请求方法和当前请求url
  79. if (url) {
  80. this.requestUrlList?.push(url)
  81. config.cancelToken = new axios.CancelToken(c => {
  82. this.cancelRequestSourceList?.push({
  83. [url]: c,
  84. })
  85. })
  86. }
  87. this.instance
  88. .request(config)
  89. .then(res => {
  90. // 如果我们为单个响应设置拦截器,这里使用单个响应的拦截器
  91. if (config.interceptors?.responseInterceptors) {
  92. res = config.interceptors.responseInterceptors.res
  93. }
  94. resolve(res)
  95. })
  96. .catch(err => {
  97. reject(err)
  98. })
  99. .finally(() => {
  100. url && this.delUrl(url)
  101. })
  102. })
  103. }
  104. // 取消请求
  105. cancelRequest(url) {
  106. if (typeof url === 'string') {
  107. // 取消单个请求
  108. const sourceIndex = this.getSourceIndex(url)
  109. sourceIndex >= 0 && this.cancelRequestSourceList?.[sourceIndex][url]()
  110. } else {
  111. // 存在多个需要取消请求的地址
  112. url.forEach(u => {
  113. const sourceIndex = this.getSourceIndex(u)
  114. sourceIndex >= 0 && this.cancelRequestSourceList?.[sourceIndex][u]()
  115. })
  116. }
  117. }
  118. // 取消全部请求
  119. cancelAllRequest() {
  120. this.cancelRequestSourceList?.forEach(source => {
  121. const key = Object.keys(source)[0]
  122. source[key]()
  123. })
  124. }
  125. }
  126. export default Request