123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- "use strict";
- const common_vendor = require("../common/vendor.js");
- const API_CONFIG = {
- // 开发环境API地址
- dev: {
- baseUrl: "http://localhost:8080"
- },
- // 生产环境API地址
- prod: {
- baseUrl: "http://43.143.204.137:8080"
- }
- };
- const BASE_URL = API_CONFIG.dev.baseUrl;
- const request = (options) => {
- return new Promise((resolve, reject) => {
- const token = common_vendor.index.getStorageSync("token") || "";
- common_vendor.index.__f__("log", "at util/api.js:28", `发起请求: ${options.method || "GET"} ${options.url}`, options.data);
- if (options.showLoading !== false) {
- common_vendor.index.showLoading({
- title: options.loadingText || "加载中...",
- mask: true
- });
- }
- const startTime = Date.now();
- const requestTask = common_vendor.index.request({
- url: BASE_URL + options.url,
- method: options.method || "GET",
- data: options.data || {},
- header: {
- "Content-Type": "application/json",
- "Authorization": token ? `Bearer ${token}` : "",
- ...options.header
- },
- timeout: options.timeout || 3e4,
- // 请求超时时间,默认30秒
- success: (res) => {
- const endTime = Date.now();
- const duration = endTime - startTime;
- common_vendor.index.__f__("log", "at util/api.js:55", `请求响应: ${options.url}, 耗时: ${duration}ms`, res);
- if (options.url.indexOf("/api/qrcode/generate") !== -1) {
- if (res.data && res.data.qrCodeUrl) {
- common_vendor.index.__f__("log", "at util/api.js:61", `二维码接口请求成功: ${options.url}`, res.data);
- resolve(res.data);
- return;
- }
- }
- if (res.statusCode === 200) {
- if (res.data && (res.data.code === 0 || res.data.code === 200 || res.data.success === true)) {
- common_vendor.index.__f__("log", "at util/api.js:71", `请求成功: ${options.url}`, res.data);
- resolve(res.data);
- } else {
- common_vendor.index.__f__("error", "at util/api.js:75", `业务错误: ${options.url}`, res.data);
- common_vendor.index.showToast({
- icon: "none",
- title: res.data.message || res.data.msg || "请求失败"
- });
- reject(res.data);
- }
- } else if (res.statusCode === 401) {
- common_vendor.index.__f__("error", "at util/api.js:84", `未授权: ${options.url}`, res);
- common_vendor.index.showToast({
- icon: "none",
- title: "请先登录"
- });
- reject(res);
- } else {
- common_vendor.index.__f__("error", "at util/api.js:93", `HTTP错误: ${options.url} (${res.statusCode})`, res);
- common_vendor.index.showToast({
- icon: "none",
- title: `请求失败(${res.statusCode})`
- });
- reject(res);
- }
- },
- fail: (err) => {
- var _a;
- common_vendor.index.__f__("error", "at util/api.js:102", `请求失败: ${options.url}`, err);
- common_vendor.index.showToast({
- icon: "none",
- title: ((_a = err.errMsg) == null ? void 0 : _a.includes("timeout")) ? "网络请求超时" : "网络请求失败"
- });
- reject(err);
- },
- complete: () => {
- if (options.showLoading !== false) {
- common_vendor.index.hideLoading();
- }
- if (options.retry && options.retryCount > 0 && options.retryCondition)
- ;
- }
- });
- if (options.requestTaskCallback) {
- options.requestTaskCallback(requestTask);
- }
- });
- };
- const API = {
- // 行程相关接口
- trip: {
- create: (data) => request({ url: "/api/trip/create", method: "POST", data }),
- update: (data) => request({ url: "/api/trip/update", method: "POST", data }),
- delete: (id) => request({ url: `/api/trip/delete/${id}`, method: "POST" }),
- detail: (id) => request({ url: `/api/trip/detail/${id}`, method: "GET" }),
- list: () => request({ url: "/api/trip/list", method: "GET" }),
- sync: (data) => request({ url: "/api/trip/sync", method: "POST", data }),
- generateQrCode: (data) => request({ url: "/api/qrcode/generate", method: "POST", data })
- }
- };
- exports.API = API;
- exports.BASE_URL = BASE_URL;
- //# sourceMappingURL=../../.sourcemap/mp-weixin/util/api.js.map
|