api.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const API_CONFIG = {
  4. // 开发环境API地址
  5. dev: {
  6. baseUrl: "http://localhost:8080"
  7. },
  8. // 生产环境API地址
  9. prod: {
  10. baseUrl: "http://43.143.204.137:8080"
  11. }
  12. };
  13. const BASE_URL = API_CONFIG.dev.baseUrl;
  14. const request = (options) => {
  15. return new Promise((resolve, reject) => {
  16. const token = common_vendor.index.getStorageSync("token") || "";
  17. common_vendor.index.__f__("log", "at util/api.js:28", `发起请求: ${options.method || "GET"} ${options.url}`, options.data);
  18. if (options.showLoading !== false) {
  19. common_vendor.index.showLoading({
  20. title: options.loadingText || "加载中...",
  21. mask: true
  22. });
  23. }
  24. const startTime = Date.now();
  25. const requestTask = common_vendor.index.request({
  26. url: BASE_URL + options.url,
  27. method: options.method || "GET",
  28. data: options.data || {},
  29. header: {
  30. "Content-Type": "application/json",
  31. "Authorization": token ? `Bearer ${token}` : "",
  32. ...options.header
  33. },
  34. timeout: options.timeout || 3e4,
  35. // 请求超时时间,默认30秒
  36. success: (res) => {
  37. const endTime = Date.now();
  38. const duration = endTime - startTime;
  39. common_vendor.index.__f__("log", "at util/api.js:55", `请求响应: ${options.url}, 耗时: ${duration}ms`, res);
  40. if (options.url.indexOf("/api/qrcode/generate") !== -1) {
  41. if (res.data && res.data.qrCodeUrl) {
  42. common_vendor.index.__f__("log", "at util/api.js:61", `二维码接口请求成功: ${options.url}`, res.data);
  43. resolve(res.data);
  44. return;
  45. }
  46. }
  47. if (res.statusCode === 200) {
  48. if (res.data && (res.data.code === 0 || res.data.code === 200 || res.data.success === true)) {
  49. common_vendor.index.__f__("log", "at util/api.js:71", `请求成功: ${options.url}`, res.data);
  50. resolve(res.data);
  51. } else {
  52. common_vendor.index.__f__("error", "at util/api.js:75", `业务错误: ${options.url}`, res.data);
  53. common_vendor.index.showToast({
  54. icon: "none",
  55. title: res.data.message || res.data.msg || "请求失败"
  56. });
  57. reject(res.data);
  58. }
  59. } else if (res.statusCode === 401) {
  60. common_vendor.index.__f__("error", "at util/api.js:84", `未授权: ${options.url}`, res);
  61. common_vendor.index.showToast({
  62. icon: "none",
  63. title: "请先登录"
  64. });
  65. reject(res);
  66. } else {
  67. common_vendor.index.__f__("error", "at util/api.js:93", `HTTP错误: ${options.url} (${res.statusCode})`, res);
  68. common_vendor.index.showToast({
  69. icon: "none",
  70. title: `请求失败(${res.statusCode})`
  71. });
  72. reject(res);
  73. }
  74. },
  75. fail: (err) => {
  76. var _a;
  77. common_vendor.index.__f__("error", "at util/api.js:102", `请求失败: ${options.url}`, err);
  78. common_vendor.index.showToast({
  79. icon: "none",
  80. title: ((_a = err.errMsg) == null ? void 0 : _a.includes("timeout")) ? "网络请求超时" : "网络请求失败"
  81. });
  82. reject(err);
  83. },
  84. complete: () => {
  85. if (options.showLoading !== false) {
  86. common_vendor.index.hideLoading();
  87. }
  88. if (options.retry && options.retryCount > 0 && options.retryCondition)
  89. ;
  90. }
  91. });
  92. if (options.requestTaskCallback) {
  93. options.requestTaskCallback(requestTask);
  94. }
  95. });
  96. };
  97. const API = {
  98. // 行程相关接口
  99. trip: {
  100. create: (data) => request({ url: "/api/trip/create", method: "POST", data }),
  101. update: (data) => request({ url: "/api/trip/update", method: "POST", data }),
  102. delete: (id) => request({ url: `/api/trip/delete/${id}`, method: "POST" }),
  103. detail: (id) => request({ url: `/api/trip/detail/${id}`, method: "GET" }),
  104. list: () => request({ url: "/api/trip/list", method: "GET" }),
  105. sync: (data) => request({ url: "/api/trip/sync", method: "POST", data }),
  106. generateQrCode: (data) => request({ url: "/api/qrcode/generate", method: "POST", data })
  107. }
  108. };
  109. exports.API = API;
  110. exports.BASE_URL = BASE_URL;
  111. //# sourceMappingURL=../../.sourcemap/mp-weixin/util/api.js.map