Request.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * @Class Request
  3. * @description luch-request http请求插件
  4. * @version 3.0.2
  5. * @Author lu-ch
  6. * @Date 2020-06-04
  7. * @Email webwork.s@qq.com
  8. * 文档: https://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: 2.7.9
  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。App和支付宝小程序不支持
  26. * @param {Object} arg.custom - 全局默认的自定义参数
  27. * @param {Number} arg.timeout - 全局默认的超时时间,单位 ms。默认30000。仅微信小程序(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 {Function(statusCode):Boolean} arg.validateStatus - 全局默认的自定义验证器。默认statusCode >= 200 && statusCode < 300
  31. */
  32. constructor(arg = {}) {
  33. if (!isPlainObject(arg)) {
  34. arg = {}
  35. //console.warn('设置全局参数必须接收一个Object')
  36. }
  37. this.config = {...defaults, ...arg}
  38. this.interceptors = {
  39. request: new InterceptorManager(),
  40. response: new InterceptorManager()
  41. }
  42. }
  43. /**
  44. * @Function
  45. * @param {Request~setConfigCallback} f - 设置全局默认配置
  46. */
  47. setConfig(f) {
  48. this.config = f(this.config)
  49. }
  50. _middleware(config) {
  51. config = mergeConfig(this.config, config)
  52. let chain = [dispatchRequest, undefined]
  53. let promise = Promise.resolve(config)
  54. this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
  55. chain.unshift(interceptor.fulfilled, interceptor.rejected)
  56. })
  57. this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
  58. chain.push(interceptor.fulfilled, interceptor.rejected)
  59. })
  60. while (chain.length) {
  61. promise = promise.then(chain.shift(), chain.shift())
  62. }
  63. return promise
  64. }
  65. /**
  66. * @Function
  67. * @param {Object} config - 请求配置项
  68. * @prop {String} options.url - 请求路径
  69. * @prop {Object} options.data - 请求参数
  70. * @prop {Object} [options.responseType = config.responseType] [text|arraybuffer] - 响应的数据类型
  71. * @prop {Object} [options.dataType = config.dataType] - 如果设为 json,会尝试对返回的数据做一次 JSON.parse
  72. * @prop {Object} [options.header = config.header] - 请求header
  73. * @prop {Object} [options.method = config.method] - 请求方法
  74. * @returns {Promise<unknown>}
  75. */
  76. request(config = {}) {
  77. return this._middleware(config)
  78. }
  79. get(url, options = {}) {
  80. return this.request({
  81. url,
  82. method: 'GET',
  83. ...options
  84. })
  85. }
  86. post(url, data, options = {}) {
  87. return this.request({
  88. url,
  89. data,
  90. method: 'POST',
  91. ...options
  92. })
  93. }
  94. // #ifndef MP-ALIPAY
  95. put(url, data, options = {}) {
  96. return this.request({
  97. url,
  98. data,
  99. method: 'PUT',
  100. ...options
  101. })
  102. }
  103. // #endif
  104. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-BAIDU
  105. delete(url, data, options = {}) {
  106. return this.request({
  107. url,
  108. data,
  109. method: 'DELETE',
  110. ...options
  111. })
  112. }
  113. // #endif
  114. // #ifdef APP-PLUS || H5 || MP-WEIXIN
  115. connect(url, data, options = {}) {
  116. return this.request({
  117. url,
  118. data,
  119. method: 'CONNECT',
  120. ...options
  121. })
  122. }
  123. // #endif
  124. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-BAIDU
  125. head(url, data, options = {}) {
  126. return this.request({
  127. url,
  128. data,
  129. method: 'HEAD',
  130. ...options
  131. })
  132. }
  133. // #endif
  134. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-BAIDU
  135. options(url, data, options = {}) {
  136. return this.request({
  137. url,
  138. data,
  139. method: 'OPTIONS',
  140. ...options
  141. })
  142. }
  143. // #endif
  144. // #ifdef APP-PLUS || H5 || MP-WEIXIN
  145. trace(url, data, options = {}) {
  146. return this.request({
  147. url,
  148. data,
  149. method: 'TRACE',
  150. ...options
  151. })
  152. }
  153. // #endif
  154. upload(url, config = {}) {
  155. config.url = url
  156. config.method = 'UPLOAD'
  157. return this._middleware(config)
  158. }
  159. download(url, config = {}) {
  160. config.url = url
  161. config.method = 'DOWNLOAD'
  162. return this._middleware(config)
  163. }
  164. }
  165. /**
  166. * setConfig回调
  167. * @return {Object} - 返回操作后的config
  168. * @callback Request~setConfigCallback
  169. * @param {Object} config - 全局默认config
  170. */