平安校园
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.

Request.js 5.2 KiB

2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * @Class Request
  3. * @description luch-request http请求插件
  4. * @version 3.0.6
  5. * @Author lu-ch
  6. * @Date 2021-05-10
  7. * @Email webwork.s@qq.com
  8. * 文档: https://www.quanzhan.co/luch-request/
  9. * github: https://github.com/lei-mu/luch-request
  10. * DCloud: http://ext.dcloud.net.cn/plugin?id=392
  11. * HBuilderX: beat-3.0.4 alpha-3.0.4
  12. */
  13. import dispatchRequest from './dispatchRequest'
  14. import InterceptorManager from './InterceptorManager'
  15. import mergeConfig from './mergeConfig'
  16. import defaults from './defaults'
  17. import { isPlainObject } from '../utils'
  18. export default class Request {
  19. /**
  20. * @param {Object} arg - 全局配置
  21. * @param {String} arg.baseURL - 全局根路径
  22. * @param {Object} arg.header - 全局header
  23. * @param {String} arg.method = [GET|POST|PUT|DELETE|CONNECT|HEAD|OPTIONS|TRACE] - 全局默认请求方式
  24. * @param {String} arg.dataType = [json] - 全局默认的dataType
  25. * @param {String} arg.responseType = [text|arraybuffer] - 全局默认的responseType。支付宝小程序不支持
  26. * @param {Object} arg.custom - 全局默认的自定义参数
  27. * @param {Number} arg.timeout - 全局默认的超时时间,单位 ms。默认60000。H5(HBuilderX 2.9.9+)、APP(HBuilderX 2.9.9+)、微信小程序(2.10.0)、支付宝小程序
  28. * @param {Boolean} arg.sslVerify - 全局默认的是否验证 ssl 证书。默认true.仅App安卓端支持(HBuilderX 2.3.3+)
  29. * @param {Boolean} arg.withCredentials - 全局默认的跨域请求时是否携带凭证(cookies)。默认false。仅H5支持(HBuilderX 2.6.15+)
  30. * @param {Boolean} arg.firstIpv4 - 全DNS解析时优先使用ipv4。默认false。仅 App-Android 支持 (HBuilderX 2.8.0+)
  31. * @param {Function(statusCode):Boolean} arg.validateStatus - 全局默认的自定义验证器。默认statusCode >= 200 && statusCode < 300
  32. */
  33. constructor(arg = {}) {
  34. if (!isPlainObject(arg)) {
  35. arg = {}
  36. console.warn('设置全局参数必须接收一个Object')
  37. }
  38. this.config = {...defaults, ...arg}
  39. this.interceptors = {
  40. request: new InterceptorManager(),
  41. response: new InterceptorManager()
  42. }
  43. }
  44. /**
  45. * @Function
  46. * @param {Request~setConfigCallback} f - 设置全局默认配置
  47. */
  48. setConfig(f) {
  49. this.config = f(this.config)
  50. }
  51. middleware(config) {
  52. config = mergeConfig(this.config, config)
  53. let chain = [dispatchRequest, undefined]
  54. let promise = Promise.resolve(config)
  55. this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
  56. chain.unshift(interceptor.fulfilled, interceptor.rejected)
  57. })
  58. this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
  59. chain.push(interceptor.fulfilled, interceptor.rejected)
  60. })
  61. while (chain.length) {
  62. promise = promise.then(chain.shift(), chain.shift())
  63. }
  64. return promise
  65. }
  66. /**
  67. * @Function
  68. * @param {Object} config - 请求配置项
  69. * @prop {String} options.url - 请求路径
  70. * @prop {Object} options.data - 请求参数
  71. * @prop {Object} [options.responseType = config.responseType] [text|arraybuffer] - 响应的数据类型
  72. * @prop {Object} [options.dataType = config.dataType] - 如果设为 json,会尝试对返回的数据做一次 JSON.parse
  73. * @prop {Object} [options.header = config.header] - 请求header
  74. * @prop {Object} [options.method = config.method] - 请求方法
  75. * @returns {Promise<unknown>}
  76. */
  77. request(config = {}) {
  78. return this.middleware(config)
  79. }
  80. get(url, options = {}) {
  81. return this.middleware({
  82. url,
  83. method: 'GET',
  84. ...options
  85. })
  86. }
  87. post(url, data, options = {}) {
  88. return this.middleware({
  89. url,
  90. data,
  91. method: 'POST',
  92. ...options
  93. })
  94. }
  95. // #ifndef MP-ALIPAY
  96. put(url, data, options = {}) {
  97. return this.middleware({
  98. url,
  99. data,
  100. method: 'PUT',
  101. ...options
  102. })
  103. }
  104. // #endif
  105. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-BAIDU
  106. delete(url, data, options = {}) {
  107. return this.middleware({
  108. url,
  109. data,
  110. method: 'DELETE',
  111. ...options
  112. })
  113. }
  114. // #endif
  115. // #ifdef H5 || MP-WEIXIN
  116. connect(url, data, options = {}) {
  117. return this.middleware({
  118. url,
  119. data,
  120. method: 'CONNECT',
  121. ...options
  122. })
  123. }
  124. // #endif
  125. // #ifdef H5 || MP-WEIXIN || MP-BAIDU
  126. head(url, data, options = {}) {
  127. return this.middleware({
  128. url,
  129. data,
  130. method: 'HEAD',
  131. ...options
  132. })
  133. }
  134. // #endif
  135. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-BAIDU
  136. options(url, data, options = {}) {
  137. return this.middleware({
  138. url,
  139. data,
  140. method: 'OPTIONS',
  141. ...options
  142. })
  143. }
  144. // #endif
  145. // #ifdef H5 || MP-WEIXIN
  146. trace(url, data, options = {}) {
  147. return this.middleware({
  148. url,
  149. data,
  150. method: 'TRACE',
  151. ...options
  152. })
  153. }
  154. // #endif
  155. upload(url, config = {}) {
  156. config.url = url
  157. config.method = 'UPLOAD'
  158. return this.middleware(config)
  159. }
  160. download(url, config = {}) {
  161. config.url = url
  162. config.method = 'DOWNLOAD'
  163. return this.middleware(config)
  164. }
  165. }
  166. /**
  167. * setConfig回调
  168. * @return {Object} - 返回操作后的config
  169. * @callback Request~setConfigCallback
  170. * @param {Object} config - 全局默认config
  171. */