index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. // this.$uni.getSystemInfoSync({
  12. // success:(res=>{
  13. // console.log(res)
  14. // })
  15. // })
  16. this.config.header = {
  17. Authorization: uni.getStorageSync('token') || '',
  18. appid: "wx3466c53d33b9dbdc"
  19. }
  20. // 检查请求拦截
  21. if (this.interceptor.request && typeof this.interceptor.request === 'function') {
  22. let tmpConfig = {};
  23. let interceptorRequest = this.interceptor.request(options);
  24. if (interceptorRequest === false) {
  25. // 返回一个处于pending状态中的Promise,来取消原promise,避免进入then()回调
  26. return new Promise(() => {});
  27. }
  28. this.options = interceptorRequest;
  29. }
  30. options.dataType = options.dataType || this.config.dataType;
  31. options.responseType = options.responseType || this.config.responseType;
  32. options.url = options.url || '';
  33. options.params = options.params || {};
  34. options.header = Object.assign({}, this.config.header, options.header);
  35. options.method = options.method || this.config.method;
  36. return new Promise((resolve, reject) => {
  37. options.complete = (response) => {
  38. // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
  39. uni.hideLoading();
  40. // 清除定时器,如果请求回来了,就无需loading
  41. clearTimeout(this.config.timer);
  42. this.config.timer = null;
  43. // 判断用户对拦截返回数据的要求,如果originalData为true,返回所有的数据(response)到拦截器,否则只返回response.data
  44. if (this.config.originalData) {
  45. // 判断是否存在拦截器
  46. if (this.interceptor.response && typeof this.interceptor.response === 'function') {
  47. let resInterceptors = this.interceptor.response(response);
  48. // 如果拦截器不返回false,就将拦截器返回的内容给this.$u.post的then回调
  49. if (resInterceptors !== false) {
  50. resolve(resInterceptors);
  51. } else {
  52. // 如果拦截器返回false,意味着拦截器定义者认为返回有问题,直接接入catch回调
  53. reject(response);
  54. }
  55. } else {
  56. // 如果要求返回原始数据,就算没有拦截器,也返回最原始的数据
  57. resolve(response);
  58. }
  59. } else {
  60. if (response.statusCode == 200) {
  61. if (this.interceptor.response && typeof this.interceptor.response ===
  62. 'function') {
  63. let resInterceptors = this.interceptor.response(response.data);
  64. if (resInterceptors !== false) {
  65. resolve(resInterceptors);
  66. } else {
  67. reject(response.data);
  68. }
  69. } else {
  70. if (response.data.code == 40000) {
  71. uni.navigateTo({
  72. url:'/pages/login/login'
  73. })
  74. return false
  75. }
  76. if (response.data.code != 200) {
  77. uni.showToast({
  78. title: response.data.message,
  79. duration: 2000,
  80. icon: "none"
  81. });
  82. return false
  83. }
  84. // 如果不是返回原始数据(originalData=false),且没有拦截器的情况下,返回纯数据给then回调
  85. resolve(response.data);
  86. }
  87. } else {
  88. // 不返回原始数据的情况下,服务器状态码不为200,modal弹框提示
  89. // if(response.errMsg) {
  90. // uni.showModal({
  91. // title: response.errMsg
  92. // });
  93. // }
  94. reject(response)
  95. }
  96. }
  97. }
  98. // 判断用户传递的URL是否/开头,如果不是,加上/,这里使用了uView的test.js验证库的url()方法
  99. options.url = validate.url(options.url) ? options.url : (this.config.baseUrl + (options.url
  100. .indexOf('/') == 0 ?
  101. options.url : '/' + options.url));
  102. // 是否显示loading
  103. // 加一个是否已有timer定时器的判断,否则有两个同时请求的时候,后者会清除前者的定时器id
  104. // 而没有清除前者的定时器,导致前者超时,一直显示loading
  105. if (this.config.showLoading && !this.config.timer) {
  106. this.config.timer = setTimeout(() => {
  107. uni.showLoading({
  108. title: this.config.loadingText,
  109. mask: this.config.loadingMask
  110. })
  111. this.config.timer = null;
  112. }, this.config.loadingTime);
  113. }
  114. uni.request(options);
  115. })
  116. // .catch(res => {
  117. // // 如果返回reject(),不让其进入this.$u.post().then().catch()后面的catct()
  118. // // 因为很多人都会忘了写后面的catch(),导致报错捕获不到catch
  119. // return new Promise(()=>{});
  120. // })
  121. }
  122. constructor() {
  123. this.config = {
  124. baseUrl: 'https://cardoa.platomix.net/api', // 请求的根域名
  125. // 默认的请求头
  126. header: {},
  127. method: 'POST',
  128. // 设置为json,返回后uni.request会对数据进行一次JSON.parse
  129. dataType: 'json',
  130. // 此参数无需处理,因为5+和支付宝小程序不支持,默认为text即可
  131. responseType: 'text',
  132. showLoading: true, // 是否显示请求中的loading
  133. loadingText: '请求中...',
  134. loadingTime: 800, // 在此时间内,请求还没回来的话,就显示加载中动画,单位ms
  135. timer: null, // 定时器
  136. originalData: false, // 是否在拦截器中返回服务端的原始数据,见文档说明
  137. loadingMask: true, // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透
  138. }
  139. // 拦截器
  140. this.interceptor = {
  141. // 请求前的拦截
  142. request: null,
  143. // 请求后的拦截
  144. response: null
  145. }
  146. // get请求
  147. this.get = (url, data = {}, header = {}) => {
  148. return this.request({
  149. method: 'GET',
  150. url,
  151. header,
  152. data
  153. })
  154. }
  155. // post请求
  156. this.post = (url, data = {}, header = {}) => {
  157. return this.request({
  158. url,
  159. method: 'POST',
  160. header,
  161. data
  162. })
  163. }
  164. // put请求,不支持支付宝小程序(HX2.6.15)
  165. this.put = (url, data = {}, header = {}) => {
  166. return this.request({
  167. url,
  168. method: 'PUT',
  169. header,
  170. data
  171. })
  172. }
  173. // delete请求,不支持支付宝和头条小程序(HX2.6.15)
  174. this.delete = (url, data = {}, header = {}) => {
  175. return this.request({
  176. url,
  177. method: 'DELETE',
  178. header,
  179. data
  180. })
  181. }
  182. }
  183. }
  184. export default new Request