index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import deepMerge from "../function/deepMerge";
  2. import validate from "../function/test";
  3. class Request {
  4. // 设置全局默认配置
  5. setConfig(customConfig) {
  6. // 深度合并对象,否则会造成对象深层属性丢失
  7. this.config = deepMerge(this.config, customConfig);
  8. }
  9. // 主要请求部分
  10. request(options = {}) {
  11. // 检查请求拦截
  12. if (this.interceptor.request && typeof this.interceptor.request === 'function') {
  13. let tmpConfig = {};
  14. let interceptorRequest = this.interceptor.request(options);
  15. if (interceptorRequest === false) {
  16. // 返回一个处于pending状态中的Promise,来取消原promise,避免进入then()回调
  17. return new Promise(()=>{});
  18. }
  19. this.options = interceptorRequest;
  20. }
  21. options.dataType = options.dataType || this.config.dataType;
  22. options.responseType = options.responseType || this.config.responseType;
  23. options.url = options.url || '';
  24. options.params = options.params || {};
  25. options.header = Object.assign({}, this.config.header, options.header);
  26. options.method = options.method || this.config.method;
  27. return new Promise((resolve, reject) => {
  28. options.complete = (response) => {
  29. // 清除定时器,如果请求回来了,就无需loading
  30. clearTimeout(this.config.timer);
  31. this.config.timer = null;
  32. // 微信小程序 hideLoading 与 showToast 共用原生层:
  33. // 请求结束后立刻 hideLoading,紧接着弹 toast 会被一起关掉(一闪而过)。
  34. // 仅在真正展示过 loading 时才关闭,并错开一拍再处理响应。
  35. const handleComplete = () => {
  36. // 判断用户对拦截返回数据的要求,如果originalData为true,返回所有的数据(response)到拦截器,否则只返回response.data
  37. if(this.config.originalData) {
  38. // 判断是否存在拦截器
  39. if (this.interceptor.response && typeof this.interceptor.response === 'function') {
  40. let resInterceptors = this.interceptor.response(response);
  41. // 如果拦截器不返回false,就将拦截器返回的内容给this.$u.post的then回调
  42. if (resInterceptors !== false) {
  43. resolve(resInterceptors);
  44. } else {
  45. // 如果拦截器返回false,意味着拦截器定义者认为返回有问题,直接接入catch回调
  46. reject(response);
  47. }
  48. } else {
  49. // 如果要求返回原始数据,就算没有拦截器,也返回最原始的数据
  50. resolve(response);
  51. }
  52. } else {
  53. if (response.statusCode == 200) {
  54. if (this.interceptor.response && typeof this.interceptor.response === 'function') {
  55. let resInterceptors = this.interceptor.response(response.data);
  56. if (resInterceptors !== false) {
  57. resolve(resInterceptors);
  58. } else {
  59. reject(response.data);
  60. }
  61. } else {
  62. // 如果不是返回原始数据(originalData=false),且没有拦截器的情况下,返回纯数据给then回调
  63. resolve(response.data);
  64. }
  65. } else {
  66. // 不返回原始数据的情况下,服务器状态码不为200,modal弹框提示
  67. // if(response.errMsg) {
  68. // uni.showModal({
  69. // title: response.errMsg
  70. // });
  71. // }
  72. reject(response)
  73. }
  74. }
  75. };
  76. if (this.config.loadingShow) {
  77. this.config.loadingShow = false;
  78. uni.hideLoading();
  79. setTimeout(handleComplete, 100);
  80. } else {
  81. handleComplete();
  82. }
  83. }
  84. // 判断用户传递的URL是否/开头,如果不是,加上/,这里使用了uView的test.js验证库的url()方法
  85. options.url = validate.url(options.url) ? options.url : (this.config.baseUrl + (options.url.indexOf('/') == 0 ?
  86. options.url : '/' + options.url));
  87. // 是否显示loading
  88. // 加一个是否已有timer定时器的判断,否则有两个同时请求的时候,后者会清除前者的定时器id
  89. // 而没有清除前者的定时器,导致前者超时,一直显示loading
  90. if(this.config.showLoading && !this.config.timer) {
  91. this.config.timer = setTimeout(() => {
  92. this.config.loadingShow = true;
  93. uni.showLoading({
  94. title: this.config.loadingText,
  95. mask: this.config.loadingMask
  96. })
  97. this.config.timer = null;
  98. }, this.config.loadingTime);
  99. }
  100. uni.request(options);
  101. })
  102. // .catch(res => {
  103. // // 如果返回reject(),不让其进入this.$u.post().then().catch()后面的catct()
  104. // // 因为很多人都会忘了写后面的catch(),导致报错捕获不到catch
  105. // return new Promise(()=>{});
  106. // })
  107. }
  108. constructor() {
  109. this.config = {
  110. baseUrl: '', // 请求的根域名
  111. // 默认的请求头
  112. header: {},
  113. method: 'POST',
  114. // 设置为json,返回后uni.request会对数据进行一次JSON.parse
  115. dataType: 'json',
  116. // 此参数无需处理,因为5+和支付宝小程序不支持,默认为text即可
  117. responseType: 'text',
  118. showLoading: true, // 是否显示请求中的loading
  119. loadingText: '请求中...',
  120. loadingTime: 800, // 在此时间内,请求还没回来的话,就显示加载中动画,单位ms
  121. timer: null, // 定时器
  122. loadingShow: false, // 是否已真正弹出过 loading(用于避免 hideLoading 误关 toast)
  123. originalData: false, // 是否在拦截器中返回服务端的原始数据,见文档说明
  124. loadingMask: true, // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透
  125. }
  126. // 拦截器
  127. this.interceptor = {
  128. // 请求前的拦截
  129. request: null,
  130. // 请求后的拦截
  131. response: null
  132. }
  133. // get请求
  134. this.get = (url, data = {}, header = {}) => {
  135. return this.request({
  136. method: 'GET',
  137. url,
  138. header,
  139. data
  140. })
  141. }
  142. // post请求
  143. this.post = (url, data = {}, header = {}) => {
  144. return this.request({
  145. url,
  146. method: 'POST',
  147. header,
  148. data
  149. })
  150. }
  151. // put请求,不支持支付宝小程序(HX2.6.15)
  152. this.put = (url, data = {}, header = {}) => {
  153. return this.request({
  154. url,
  155. method: 'PUT',
  156. header,
  157. data
  158. })
  159. }
  160. // delete请求,不支持支付宝和头条小程序(HX2.6.15)
  161. this.delete = (url, data = {}, header = {}) => {
  162. return this.request({
  163. url,
  164. method: 'DELETE',
  165. header,
  166. data
  167. })
  168. }
  169. }
  170. }
  171. export default new Request