request.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const config_env = require("../config/env.js");
  4. const loadingManager = {
  5. loadingCount: 0,
  6. loadingTimer: null,
  7. // 显示 loading
  8. show(options = {}) {
  9. const config = {
  10. title: "加载中...",
  11. mask: true,
  12. delay: 300,
  13. ...options
  14. };
  15. if (this.loadingCount > 0) {
  16. this.loadingCount++;
  17. return;
  18. }
  19. this.loadingTimer = setTimeout(() => {
  20. if (this.loadingCount > 0) {
  21. common_vendor.index.showLoading({
  22. title: config.title,
  23. mask: config.mask
  24. });
  25. }
  26. }, config.delay);
  27. this.loadingCount++;
  28. },
  29. // 隐藏 loading
  30. hide() {
  31. this.loadingCount = Math.max(0, this.loadingCount - 1);
  32. if (this.loadingCount === 0) {
  33. if (this.loadingTimer) {
  34. clearTimeout(this.loadingTimer);
  35. this.loadingTimer = null;
  36. }
  37. common_vendor.index.hideLoading();
  38. }
  39. },
  40. // 强制隐藏所有 loading
  41. forceHide() {
  42. this.loadingCount = 0;
  43. if (this.loadingTimer) {
  44. clearTimeout(this.loadingTimer);
  45. this.loadingTimer = null;
  46. }
  47. common_vendor.index.hideLoading();
  48. }
  49. };
  50. const request = {
  51. async get(url, params = {}, options = {}) {
  52. return this.request(url, "GET", params, options);
  53. },
  54. async post(url, data = {}, options = {}) {
  55. return this.request(url, "POST", data, options);
  56. },
  57. async put(url, data = {}, options = {}) {
  58. return this.request(url, "PUT", data, options);
  59. },
  60. async delete(url, data = {}, options = {}) {
  61. return this.request(url, "DELETE", data, options);
  62. },
  63. async request(url, method, data, options = {}) {
  64. let loadingConfig;
  65. if (options.loading === false) {
  66. loadingConfig = false;
  67. } else {
  68. loadingConfig = {
  69. show: true,
  70. title: "加载中...",
  71. mask: true,
  72. delay: 300,
  73. timeout: 1e4,
  74. ...typeof options.loading === "object" ? options.loading : {}
  75. };
  76. }
  77. const requestConfig = {
  78. timeout: 15e3,
  79. // 默认网络超时 15 秒
  80. ...options.request
  81. };
  82. const shouldShowLoading = loadingConfig !== false && loadingConfig.show !== false;
  83. if (shouldShowLoading) {
  84. loadingManager.show(loadingConfig);
  85. }
  86. const deviceInfo = common_vendor.index.getStorageSync("deviceInfo") || {};
  87. const sbbs = deviceInfo.deviceId || "";
  88. const sbmc = deviceInfo.model || "";
  89. const separator = url.includes("?") ? "&" : "?";
  90. const finalUrl = `${url}${separator}sbbs=${sbbs}&sbmc=${sbmc}`;
  91. let timeoutTimer = null;
  92. if (shouldShowLoading && loadingConfig.timeout) {
  93. timeoutTimer = setTimeout(() => {
  94. loadingManager.hide();
  95. common_vendor.index.showToast({
  96. title: "请求超时",
  97. icon: "none"
  98. });
  99. }, loadingConfig.timeout);
  100. }
  101. let requestData = data;
  102. if (options.formData && data && typeof data === "object") {
  103. requestData = Object.keys(data).map((key) => {
  104. const value = data[key];
  105. if (Array.isArray(value)) {
  106. return value.map((item) => `${encodeURIComponent(key)}=${encodeURIComponent(item)}`).join("&");
  107. } else {
  108. return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
  109. }
  110. }).join("&");
  111. }
  112. return new Promise((resolve, reject) => {
  113. common_vendor.index.request({
  114. url: `${config_env.env.baseUrl}${finalUrl}`,
  115. method,
  116. data: requestData,
  117. timeout: requestConfig.timeout,
  118. // 设置网络超时
  119. header: (() => {
  120. const headers = {};
  121. if (options.formData) {
  122. headers["content-type"] = "application/x-www-form-urlencoded";
  123. } else {
  124. headers["content-type"] = "application/json";
  125. }
  126. const jsessionId = common_vendor.index.getStorageSync("JSESSIONID");
  127. if (jsessionId) {
  128. headers["Cookie"] = `JSESSIONID=${jsessionId}`;
  129. }
  130. return headers;
  131. })(),
  132. success: (res) => {
  133. if (timeoutTimer) {
  134. clearTimeout(timeoutTimer);
  135. }
  136. const headers = res.header;
  137. const setCookie = headers["set-cookie"] || headers["Set-Cookie"];
  138. if (setCookie) {
  139. const match = setCookie.match(/JSESSIONID=([^;]+)/);
  140. if (match && match[1]) {
  141. common_vendor.index.setStorageSync("JSESSIONID", match[1]);
  142. }
  143. }
  144. if (res.statusCode === 200) {
  145. const responseData = {
  146. data: res.data
  147. };
  148. resolve(responseData);
  149. } else {
  150. reject(res);
  151. }
  152. },
  153. fail: (err) => {
  154. if (timeoutTimer) {
  155. clearTimeout(timeoutTimer);
  156. }
  157. common_vendor.index.showToast({
  158. title: "网络请求失败",
  159. icon: "none"
  160. });
  161. reject(err);
  162. },
  163. complete: () => {
  164. if (shouldShowLoading) {
  165. loadingManager.hide();
  166. }
  167. }
  168. });
  169. });
  170. }
  171. };
  172. request.loadingManager = loadingManager;
  173. request.silent = {
  174. get: (url, params = {}) => request.get(url, params, { loading: false }),
  175. post: (url, data = {}) => request.post(url, data, { loading: false }),
  176. put: (url, data = {}) => request.put(url, data, { loading: false }),
  177. delete: (url, data = {}) => request.delete(url, data, { loading: false })
  178. };
  179. request.withLoading = (title) => ({
  180. get: (url, params = {}) => request.get(url, params, { loading: { title } }),
  181. post: (url, data = {}) => request.post(url, data, { loading: { title } }),
  182. put: (url, data = {}) => request.put(url, data, { loading: { title } }),
  183. delete: (url, data = {}) => request.delete(url, data, { loading: { title } })
  184. });
  185. exports.request = request;
  186. //# sourceMappingURL=../../.sourcemap/mp-weixin/utils/request.js.map