request.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. export default class Request {
  2. // 判断url是否为绝对路径
  3. static absolutelyUrl(url) {
  4. return /(http|https):\/\/([\w.]+\/?)\S*/.test(url)
  5. };
  6. // 发起请求前
  7. static beforeRequest(config) {
  8. return config
  9. };
  10. // 发起请求后
  11. static afterRequest(response) {
  12. return response
  13. };
  14. // 基本配置
  15. config = {
  16. baseUrl: '',
  17. header: {
  18. 'Content-Type': 'application/json;charset=UTF-8'
  19. },
  20. method: 'GET',
  21. dataType: 'json',
  22. responseType: 'text',
  23. success() {},
  24. fail() {},
  25. complete() {}
  26. };
  27. // 过滤器
  28. interceptor = {
  29. beforeRequest(callback) {
  30. if (callback) {
  31. Request.beforeRequest = callback
  32. }
  33. },
  34. afterRequest(callback) {
  35. if (callback) {
  36. Request.afterRequest = callback
  37. }
  38. }
  39. };
  40. // 设置配置
  41. setConfig(callback) {
  42. this.config = callback(this.config)
  43. }
  44. // 发起请求
  45. request(options = {}) {
  46. // 基本参数
  47. options.baseUrl = options.baseUrl || this.config.baseUrl
  48. options.dataType = options.dataType || this.config.dataType
  49. options.url = Request.absolutelyUrl(options.url) ? options.url : (options.baseUrl + options.url)
  50. options.data = options.data || {}
  51. options.header = options.header || this.config.header
  52. options.method = options.method || this.config.method
  53. // console.log(options.header);
  54. // options.
  55. // promise 请求
  56. return new Promise((resolve, reject) => {
  57. let execute = true
  58. let _config = null
  59. // 请求完成
  60. options.complete = (response) => {
  61. response.config = _config
  62. return resolve(Request.afterRequest(response))
  63. }
  64. // 默认配置 + 用户配置【复制作用】
  65. let mConfig = {
  66. ...this.config,
  67. ...options
  68. }
  69. // 取消请求 - 回调
  70. let cancel = (msg = 'request cancel') => {
  71. execute = false
  72. return resolve([{
  73. config: mConfig,
  74. data: {
  75. code: 0,
  76. msg: msg
  77. }
  78. }])
  79. }
  80. // 默认配置 + 用户配置 + 初始配置【复制作用】
  81. _config = {
  82. ...mConfig,
  83. ...Request.beforeRequest(mConfig, cancel)
  84. }
  85. // 是否取消请求【如果execute == false, 那么已经resolve了,所以不需要return resolve】
  86. if (!execute) return
  87. // 发起请求
  88. if (_config.hasOwnProperty('uploadFile')) {
  89. const data = _config.data
  90. let tConfig = {
  91. url: _config.url,
  92. filePath: data.file.file,
  93. name: data.file.name,
  94. formData: data.form,
  95. // #ifdef MP-WEIXIN
  96. header: _config.header,
  97. // #endif
  98. success: _config.success,
  99. fail: _config.fail,
  100. complete: _config.complete,
  101. }
  102. uni.uploadFile(tConfig)
  103. } else {
  104. uni.request(_config)
  105. }
  106. })
  107. };
  108. // 接口请求 - 支持重试
  109. autoRetry(options, retryMax = 3) {
  110. // console.log('autoRetry', options, retryMax);
  111. return new Promise((resolve, reject) => {
  112. if (retryMax <= 0) {
  113. return resolve(['网络错误']);
  114. }
  115. if (retryMax == 2) {
  116. // console.log('this.request exect');
  117. this.request(options).then(([err, res]) => {
  118. // console.log('this.request', err, res);
  119. if (!err) {
  120. resolve([err, res]);
  121. } else {
  122. this.autoRetry(options, retryMax - 1);
  123. }
  124. });
  125. } else {
  126. return this.autoRetry(options, retryMax - 1);
  127. }
  128. });
  129. }
  130. // GET 请求
  131. get(url, data, options = {}) {
  132. options.url = url
  133. options.data = data
  134. options.method = 'GET'
  135. return this.request(options)
  136. }
  137. // POST 请求
  138. post(url, data, options = {}) {
  139. options.url = url
  140. options.data = data
  141. options.method = 'POST'
  142. return this.request(options)
  143. }
  144. }