utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // 获取合并的数据
  2. export const mergeConfig = (_this, options) => {
  3. //判断url是不是链接
  4. let urlType = /^(http|https):\/\//.test(options.url);
  5. let config = Object.assign({
  6. timeout: _this.timeout
  7. }, _this.config, options);
  8. if (options.method == "FILE") {
  9. config.url = urlType ? options.url : _this.fileUrl + options.url;
  10. } else {
  11. config.url = urlType ? options.url : _this.baseUrl + options.url;
  12. }
  13. //请求头
  14. if (options.header) {
  15. config.header = Object.assign({}, _this.header, options.header);
  16. } else {
  17. config.header = Object.assign({}, _this.header);
  18. }
  19. return config;
  20. }
  21. // 请求
  22. export const dispatchRequest = (requestInfo) => {
  23. // console.log(requestInfo, 'requestInfo.header')
  24. return new Promise((resolve, reject) => {
  25. let requestAbort = true;
  26. let requestData = {
  27. url: requestInfo.url,
  28. header: requestInfo.header, //加入请求头
  29. responseType: requestInfo.responseType || '',
  30. success: (res) => {
  31. requestAbort = false;
  32. resolve(res);
  33. },
  34. fail: (err) => {
  35. requestAbort = false;
  36. if (err.errMsg == "request:fail abort") {
  37. reject({
  38. errMsg: "请求超时,请重新尝试",
  39. statusCode: 0,
  40. });
  41. } else {
  42. reject(err);
  43. }
  44. }
  45. };
  46. //请求类型
  47. if (requestInfo.method) {
  48. requestData.method = requestInfo.method;
  49. }
  50. if (requestInfo.data) {
  51. // 对数据进行编码
  52. // if (requestInfo.method === 'GET') {
  53. // console.log(requestInfo.method, requestInfo.url, 'requestInfo.method')
  54. // const encodedData = Object.keys(requestInfo.data).map((key, index) => {
  55. // return encodeURIComponent(key) + '=' + encodeURIComponent(requestInfo.data[key]);
  56. // }).join('&');
  57. // requestData.url = requestInfo.url + '?' + encodedData;
  58. // console.log(requestInfo.url, 'requestInfo.url')
  59. // } else {
  60. // requestData.data = requestInfo.data;
  61. // }
  62. requestData.data = requestInfo.data;
  63. }
  64. // #ifdef MP-WEIXIN || MP-ALIPAY
  65. if (requestInfo.timeout) {
  66. requestData.timeout = requestInfo.timeout;
  67. }
  68. // #endif
  69. if (requestInfo.dataType) {
  70. requestData.dataType = requestInfo.dataType;
  71. }
  72. // #ifndef APP-PLUS || MP-ALIPAY
  73. // #endif
  74. // #ifdef H5
  75. if (requestInfo.withCredentials) {
  76. requestData.withCredentials = requestInfo.withCredentials;
  77. }
  78. // #endif
  79. let requestTask = uni.request(requestData);
  80. setTimeout(() => {
  81. if (requestAbort) {
  82. requestTask.abort();
  83. }
  84. }, requestInfo.timeout)
  85. })
  86. }
  87. // jsonp请求
  88. export const jsonpRequest = (requestInfo) => {
  89. return new Promise((resolve, reject) => {
  90. let dataStr = '';
  91. Object.keys(requestInfo.data).forEach(key => {
  92. dataStr += key + '=' + requestInfo.data[key] + '&';
  93. });
  94. //匹配最后一个&并去除
  95. if (dataStr !== '') {
  96. dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
  97. }
  98. requestInfo.url = requestInfo.url + '?' + dataStr;
  99. let callbackName = "callback" + Math.ceil(Math.random() * 1000000);
  100. // #ifdef H5
  101. window[callbackName] = (data) => {
  102. resolve(data);
  103. }
  104. let script = document.createElement("script");
  105. script.src = requestInfo.url + "&callback=" + callbackName;
  106. document.head.appendChild(script);
  107. // 及时删除,防止加载过多的JS
  108. document.head.removeChild(script);
  109. // #endif
  110. });
  111. }