utils.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. return new Promise((resolve, reject) => {
  24. let requestAbort = true;
  25. let requestData = {
  26. url: requestInfo.url,
  27. header: requestInfo.header, //加入请求头
  28. success: (res) => {
  29. requestAbort = false;
  30. resolve(res);
  31. },
  32. fail: (err) => {
  33. requestAbort = false;
  34. if (err.errMsg == "request:fail abort") {
  35. reject({
  36. errMsg: "请求超时,请重新尝试",
  37. statusCode: 0,
  38. });
  39. } else {
  40. reject(err);
  41. }
  42. }
  43. };
  44. //请求类型
  45. if (requestInfo.method) {
  46. requestData.method = requestInfo.method;
  47. }
  48. if (requestInfo.data) {
  49. requestData.data = requestInfo.data;
  50. }
  51. // #ifdef MP-WEIXIN || MP-ALIPAY
  52. if (requestInfo.timeout) {
  53. requestData.timeout = requestInfo.timeout;
  54. }
  55. // #endif
  56. if (requestInfo.dataType) {
  57. requestData.dataType = requestInfo.dataType;
  58. }
  59. // #ifndef APP-PLUS || MP-ALIPAY
  60. if (requestInfo.responseType) {
  61. requestData.responseType = requestInfo.responseType;
  62. }
  63. // #endif
  64. // #ifdef H5
  65. if (requestInfo.withCredentials) {
  66. requestData.withCredentials = requestInfo.withCredentials;
  67. }
  68. // #endif
  69. let requestTask = uni.request(requestData);
  70. setTimeout(() => {
  71. if (requestAbort) {
  72. requestTask.abort();
  73. }
  74. }, requestInfo.timeout)
  75. })
  76. }
  77. // jsonp请求
  78. export const jsonpRequest = (requestInfo) => {
  79. return new Promise((resolve, reject) => {
  80. let dataStr = '';
  81. Object.keys(requestInfo.data).forEach(key => {
  82. dataStr += key + '=' + requestInfo.data[key] + '&';
  83. });
  84. //匹配最后一个&并去除
  85. if (dataStr !== '') {
  86. dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
  87. }
  88. requestInfo.url = requestInfo.url + '?' + dataStr;
  89. let callbackName = "callback" + Math.ceil(Math.random() * 1000000);
  90. // #ifdef H5
  91. window[callbackName] = (data) => {
  92. resolve(data);
  93. }
  94. let script = document.createElement("script");
  95. script.src = requestInfo.url + "&callback=" + callbackName;
  96. document.head.appendChild(script);
  97. // 及时删除,防止加载过多的JS
  98. document.head.removeChild(script);
  99. // #endif
  100. });
  101. }