爱简收旧衣按件回收前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.6 KiB

  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const api_model_address = require("./model/address.js");
  4. const api_model_index = require("./model/index.js");
  5. const api_model_info = require("./model/info.js");
  6. const api_model_login = require("./model/login.js");
  7. const api_model_vip = require("./model/vip.js");
  8. const api_http = require("./http.js");
  9. const api_config = require("./config.js");
  10. const limit = /* @__PURE__ */ new Map();
  11. const debounce = /* @__PURE__ */ new Map();
  12. const modules = /* @__PURE__ */ Object.assign({ "./model/address.js": api_model_address.__vite_glob_0_0, "./model/index.js": api_model_index.__vite_glob_0_1, "./model/info.js": api_model_info.__vite_glob_0_2, "./model/login.js": api_model_login.__vite_glob_0_3, "./model/vip.js": api_model_vip.__vite_glob_0_4 });
  13. Object.entries(modules).forEach(([path, mod]) => {
  14. const model = mod.default || mod;
  15. const key = path.match(/\/([^/]+)\.js$/)[1];
  16. for (const k in model) {
  17. if (api_config.config[k]) {
  18. common_vendor.index.__f__("error", "at api/api.js:18", `重名api------model=${key},key=${k}`);
  19. continue;
  20. }
  21. api_config.config[k] = model[k];
  22. }
  23. });
  24. function api(key, data = {}, callback, loadingTitle) {
  25. const req = api_config.config[key];
  26. if (!req) {
  27. common_vendor.index.__f__("error", "at api/api.js:38", "无效key: " + key);
  28. return Promise.reject(new Error("无效key"));
  29. }
  30. if (typeof callback === "string") {
  31. loadingTitle = callback;
  32. callback = void 0;
  33. }
  34. if (typeof data === "function") {
  35. callback = data;
  36. data = {};
  37. }
  38. if (req.limit) {
  39. const last = limit.get(req.url);
  40. if (last && Date.now() - last < req.limit) {
  41. return Promise.reject(new Error("请求过于频繁"));
  42. }
  43. limit.set(req.url, Date.now());
  44. }
  45. if (req.auth && !common_vendor.index.getStorageSync("token")) {
  46. return Promise.reject(new Error("需要登录"));
  47. }
  48. if (req.debounce) {
  49. const prev = debounce.get(req.url);
  50. if (prev)
  51. clearTimeout(prev);
  52. debounce.set(
  53. req.url,
  54. setTimeout(() => {
  55. debounce.delete(req.url);
  56. api_http.http.http(
  57. req.url,
  58. data,
  59. callback,
  60. req.method,
  61. loadingTitle || req.showLoading,
  62. loadingTitle || req.loadingTitle
  63. );
  64. }, req.debounce)
  65. );
  66. return Promise.reject(new Error("请求防抖中"));
  67. }
  68. return api_http.http.http(
  69. req.url,
  70. data,
  71. callback,
  72. req.method,
  73. loadingTitle || req.showLoading,
  74. loadingTitle || req.loadingTitle
  75. );
  76. }
  77. exports.api = api;
  78. //# sourceMappingURL=../../.sourcemap/mp-weixin/api/api.js.map