request.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. console.log(callData);
  119. if (statusCode == 200) {
  120. try {
  121. let verifyRes = await this.defaultReq.beforeFinsh(callData);
  122. resolve(verifyRes);
  123. } catch (error) {
  124. reject(error)
  125. }
  126. // let verifyRes =await this.defaultReq.beforeFinsh(callData);
  127. // if(verifyRes){
  128. // resolve(verifyRes);
  129. // }
  130. }else{
  131. reject(callData)
  132. }
  133. if (title) {
  134. uni.hideLoading();
  135. }
  136. }
  137. });
  138. abortFun(beforeInfo,requestTask);
  139. })
  140. }
  141. /**
  142. * 2019年4月6日12:05:55
  143. * 封装上传文件功能
  144. */
  145. ajaxFile({
  146. path = '',
  147. title = false,
  148. header = this.defaultUp.header,
  149. filePath = '',
  150. fileName = '',
  151. extra = {},
  152. abort = bt => {},
  153. _isFirst = true,
  154. _autoClose = true,
  155. ...args
  156. } = {}) {
  157. Object.assign(extra,this.defaultUp.baseData);
  158. return new Promise((resolve, reject) => {
  159. if (title && _isFirst) { //显示请求提示
  160. uni.showLoading({
  161. title,
  162. mask: true,
  163. });
  164. }
  165. const url=this.defaultUp.url + path;
  166. let beforeInfo=Object.assign({},{path:url,header,filePath,fileName,extra,args})
  167. const uploadTask = uni.uploadFile({
  168. url,
  169. filePath,
  170. name: fileName,
  171. header,
  172. formData: extra,
  173. complete: ({
  174. statusCode = 0,
  175. ...finsh
  176. } = {}) => {
  177. if (title && _autoClose) {
  178. uni.hideLoading();
  179. }
  180. if (statusCode == 200) {
  181. return resolve(finsh);
  182. }
  183. return reject(finsh);
  184. }
  185. });
  186. abort(beforeInfo,uploadTask);
  187. })
  188. }
  189. /**
  190. * 内部下载文件,仅内部调用
  191. */
  192. downFiles({
  193. abort = () => {},
  194. path = '',
  195. title = false,
  196. index=0, //所属下载索引
  197. ...extra
  198. } = {}) {
  199. return new Promise((resolve, reject) => {
  200. if (!path) {
  201. reject('请选设置请求路径');
  202. }
  203. if (title) {
  204. uni.showLoading({
  205. title,
  206. mask: true,
  207. });
  208. }
  209. const downloadTask = uni.downloadFile({
  210. url: path,
  211. ...extra,
  212. complete: ({
  213. statusCode = 0,
  214. ...finsh
  215. } = {}) => {
  216. if (title) {
  217. uni.hideLoading();
  218. }
  219. if (statusCode === 200) {
  220. return resolve(Object.assign({}, {
  221. statusCode,
  222. params: extra,
  223. ...finsh
  224. }));
  225. }
  226. return reject(finsh)
  227. },
  228. })
  229. abort({
  230. abort,
  231. path,
  232. title,
  233. index,
  234. ...extra
  235. },downloadTask);
  236. })
  237. }
  238. /**
  239. * 设置代理
  240. */
  241. proxy(obj, callback) {
  242. ProxyControll(obj, callback);
  243. }
  244. /**
  245. * 运行环境判断
  246. */
  247. platformChunk() {
  248. if (typeof plus == 'undefined') {
  249. return 1;
  250. }
  251. return 0;
  252. }
  253. }
  254. export const req = new Request();
  255. export const RQ = Request;