request.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * 2019年4月5日12:44:58
  3. * 简单封装uni-app请求,下载,上传。
  4. */
  5. let _baseuUrl = '';
  6. let _isUpOpenDown = false; //是否在上传js中引入下载的js
  7. let _defaultReq = {
  8. isreq: true, //是否已经打开ajax,默认为true
  9. url: '', //独立的url ajax
  10. baseData: {}, //ajax基本参数
  11. header: {
  12. 'content-type': 'application/x-www-form-urlencoded'
  13. },
  14. type: 'GET',
  15. dataType: 'json',
  16. responseType: 'text',
  17. beforeSend:r=>r,
  18. beforeFinsh: r=> r
  19. }
  20. let _defaultUp = {
  21. url: '', //独立的url
  22. baseData: {},
  23. header: {
  24. 'content-type': 'multipart/form-data;'
  25. },
  26. }
  27. /**
  28. * 代理控制 2019年4月6日16:06:05
  29. */
  30. let ProxyControll = (obj, callback = (key, val, oldval) => {}) => {
  31. for (let key in obj) {
  32. let itemval = obj[key];
  33. Object.defineProperty(obj, key, {
  34. enumerable: true,
  35. get: function() {
  36. return this[`HHYANG_${key}`]
  37. },
  38. set: function(newvalue) {
  39. callback(key, newvalue, this[`HHYANG_${key}`]);
  40. this[`HHYANG_${key}`]= newvalue;
  41. }
  42. })
  43. obj[key] = itemval;
  44. }
  45. }
  46. ProxyControll(_defaultReq);
  47. ProxyControll(_defaultUp);
  48. class Request {
  49. constructor(arg) {
  50. this.platform = this.platformChunk();
  51. this.defaultReq = _defaultReq;
  52. this.defaultUp = _defaultUp;
  53. }
  54. set baseuUrl(value) {
  55. _baseuUrl = value;
  56. _defaultReq.url = value;
  57. _defaultUp.url = value;
  58. }
  59. get baseuUrl() {
  60. return _baseuUrl;
  61. }
  62. set isUpOpenDown(value) {
  63. _isUpOpenDown = value;
  64. }
  65. get isUpOpenDown() {
  66. return _isUpOpenDown;
  67. }
  68. /**
  69. * 基本ajax请求
  70. */
  71. ajax({
  72. path = '', //请求路径
  73. title = false, //请求头部 默认为false不显示, 传入字符串则显示 推荐7个字内
  74. header = this.defaultReq.header, //请求header 默认为"application/x-www-form-urlencoded"
  75. data = {}, //请求数据,默认为空对象
  76. type = this.defaultReq.type, //请求类型 默认为'GET'
  77. dataType = this.defaultReq.dataType, //返回数据类型,默认为json。会对返回数据做一个JSON.parse
  78. responseType = this.defaultReq.responseType, //设置响应的数据类型默认为'text'
  79. abortFun = _bt => {}
  80. } = {}, ...extra) {
  81. return new Promise(async (resolve, reject) => {
  82. if (!this.defaultReq.isreq) {
  83. return reject('要想使用ajax,请开放isreq 设置为true');
  84. }
  85. Object.assign(data, this.defaultReq.baseData); //合并参数
  86. if (typeof header === 'string') { //如果用户只想设置content-type
  87. header = {
  88. 'content-type': header
  89. };
  90. }
  91. let beforeInfo={
  92. url: this.defaultReq.url + path,
  93. method: type,
  94. dataType,
  95. responseType,
  96. data,
  97. header,
  98. }
  99. let verifyBeforeInfo =await this.defaultReq.beforeSend(beforeInfo); //用户自定义后的请求参数
  100. if(!verifyBeforeInfo){
  101. return reject( Object.assign(beforeInfo,{beforeClose:true}));
  102. }
  103. if (title) { //显示请求提示
  104. uni.showLoading({
  105. title,
  106. mask: true,
  107. });
  108. }
  109. const requestTask = uni.request({
  110. ...beforeInfo,
  111. complete:async ({
  112. statusCode,
  113. ...finsh
  114. }={}) => {
  115. let callData = Object.assign({
  116. extra
  117. }, finsh,{statusCode});
  118. if (statusCode == 200) {
  119. try {
  120. let verifyRes = await this.defaultReq.beforeFinsh(callData);
  121. resolve(verifyRes);
  122. } catch (error) {
  123. reject(error)
  124. }
  125. // let verifyRes =await this.defaultReq.beforeFinsh(callData);
  126. // if(verifyRes){
  127. // resolve(verifyRes);
  128. // }
  129. }else{
  130. reject(callData)
  131. }
  132. if (title) {
  133. uni.hideLoading();
  134. }
  135. }
  136. });
  137. abortFun(beforeInfo,requestTask);
  138. })
  139. }
  140. /**
  141. * 2019年4月6日12:05:55
  142. * 封装上传文件功能
  143. */
  144. ajaxFile({
  145. path = '',
  146. title = false,
  147. header = this.defaultUp.header,
  148. filePath = '',
  149. fileName = '',
  150. extra = {},
  151. abort = bt => {},
  152. _isFirst = true,
  153. _autoClose = true,
  154. ...args
  155. } = {}) {
  156. Object.assign(extra,this.defaultUp.baseData);
  157. return new Promise((resolve, reject) => {
  158. if (title && _isFirst) { //显示请求提示
  159. uni.showLoading({
  160. title,
  161. mask: true,
  162. });
  163. }
  164. const url=this.defaultUp.url + path;
  165. let beforeInfo=Object.assign({},{path:url,header,filePath,fileName,extra,args})
  166. const uploadTask = uni.uploadFile({
  167. url,
  168. filePath,
  169. name: fileName,
  170. header,
  171. formData: extra,
  172. complete: ({
  173. statusCode = 0,
  174. ...finsh
  175. } = {}) => {
  176. if (title && _autoClose) {
  177. uni.hideLoading();
  178. }
  179. if (statusCode == 200) {
  180. return resolve(finsh);
  181. }
  182. return reject(finsh);
  183. }
  184. });
  185. abort(beforeInfo,uploadTask);
  186. })
  187. }
  188. /**
  189. * 内部下载文件,仅内部调用
  190. */
  191. downFiles({
  192. abort = () => {},
  193. path = '',
  194. title = false,
  195. index=0, //所属下载索引
  196. ...extra
  197. } = {}) {
  198. return new Promise((resolve, reject) => {
  199. if (!path) {
  200. reject('请选设置请求路径');
  201. }
  202. if (title) {
  203. uni.showLoading({
  204. title,
  205. mask: true,
  206. });
  207. }
  208. const downloadTask = uni.downloadFile({
  209. url: path,
  210. ...extra,
  211. complete: ({
  212. statusCode = 0,
  213. ...finsh
  214. } = {}) => {
  215. if (title) {
  216. uni.hideLoading();
  217. }
  218. if (statusCode === 200) {
  219. return resolve(Object.assign({}, {
  220. statusCode,
  221. params: extra,
  222. ...finsh
  223. }));
  224. }
  225. return reject(finsh)
  226. },
  227. })
  228. abort({
  229. abort,
  230. path,
  231. title,
  232. index,
  233. ...extra
  234. },downloadTask);
  235. })
  236. }
  237. /**
  238. * 设置代理
  239. */
  240. proxy(obj, callback) {
  241. ProxyControll(obj, callback);
  242. }
  243. /**
  244. * 运行环境判断
  245. */
  246. platformChunk() {
  247. if (typeof plus == 'undefined') {
  248. return 1;
  249. }
  250. return 0;
  251. }
  252. }
  253. export const req = new Request();
  254. export const RQ = Request;