222 lines
6.7 KiB
JavaScript
222 lines
6.7 KiB
JavaScript
"use strict";
|
||
const common_vendor = require("../common/vendor.js");
|
||
const config_dev = require("../config/dev.js");
|
||
const utils_loadingManager = require("./loading-manager.js");
|
||
const ENV_CONFIG = {
|
||
develop: {
|
||
// 开发环境
|
||
// baseUrl: 'http://192.168.2.136:4501',
|
||
baseUrl: "https://testlu.chuangtewl.com/prod-api",
|
||
appId: 1
|
||
// TODO: 根据实际后端配置调整
|
||
},
|
||
trial: {
|
||
// 体验版
|
||
baseUrl: "https://testlu.chuangtewl.com/prod-api",
|
||
appId: 1
|
||
// TODO: 根据实际后端配置调整
|
||
},
|
||
release: {
|
||
// 正式版
|
||
baseUrl: "https://testlu.chuangtewl.com/prod-api",
|
||
appId: 1
|
||
// TODO: 根据实际后端配置调整
|
||
}
|
||
};
|
||
const getCurrentConfig = () => {
|
||
try {
|
||
const { envVersion } = common_vendor.wx$1.getAccountInfoSync().miniProgram;
|
||
common_vendor.index.__f__("log", "at utils/request.js:41", "当前环境:", envVersion);
|
||
const envConfig = ENV_CONFIG[envVersion] || ENV_CONFIG.release;
|
||
return {
|
||
baseUrl: envConfig.baseUrl,
|
||
appId: envConfig.appId || 1
|
||
// 确保appId有默认值
|
||
};
|
||
} catch (error) {
|
||
common_vendor.index.__f__("warn", "at utils/request.js:49", "获取环境失败,默认使用正式环境:", error);
|
||
const fallbackConfig = ENV_CONFIG.release;
|
||
return {
|
||
baseUrl: fallbackConfig.baseUrl,
|
||
appId: fallbackConfig.appId || 1
|
||
// 确保appId有默认值
|
||
};
|
||
}
|
||
};
|
||
const config = getCurrentConfig();
|
||
const BASE_URL = config.baseUrl;
|
||
common_vendor.index.__f__("log", "at utils/request.js:62", "HTTP配置:", {
|
||
baseUrl: BASE_URL,
|
||
config
|
||
});
|
||
function getRequestHeaders(customHeader = {}) {
|
||
let token = common_vendor.index.getStorageSync("token");
|
||
if (!token) {
|
||
token = config_dev.getTempToken();
|
||
}
|
||
let authorization = token;
|
||
return {
|
||
"Content-Type": "application/json;charset=UTF-8",
|
||
Authorization: authorization,
|
||
...customHeader
|
||
};
|
||
}
|
||
function handleResponseError(res, reject, options = {}) {
|
||
var _a;
|
||
if (options.showLoading !== false) {
|
||
utils_loadingManager.hideLoading();
|
||
}
|
||
const errorMap = {
|
||
401: {
|
||
title: "登录已过期,请重新登录",
|
||
action: () => {
|
||
setTimeout(() => {
|
||
common_vendor.index.reLaunch({
|
||
url: "/pages/login/login"
|
||
});
|
||
}, 1500);
|
||
}
|
||
},
|
||
403: {
|
||
title: "权限不足",
|
||
action: () => {
|
||
}
|
||
},
|
||
404: {
|
||
title: "请求的资源不存在",
|
||
action: () => {
|
||
}
|
||
},
|
||
500: {
|
||
title: "服务器错误",
|
||
action: () => {
|
||
}
|
||
}
|
||
};
|
||
const error = errorMap[res.statusCode] || {
|
||
title: ((_a = res.data) == null ? void 0 : _a.msg) || "请求失败",
|
||
action: () => {
|
||
}
|
||
};
|
||
common_vendor.index.showToast({
|
||
title: error.title,
|
||
icon: "none",
|
||
duration: 2e3
|
||
});
|
||
error.action();
|
||
reject(new Error(error.title));
|
||
}
|
||
function request(options = {}) {
|
||
return new Promise((resolve, reject) => {
|
||
const localToken = common_vendor.index.getStorageSync("token");
|
||
let token = localToken;
|
||
if (!token && config_dev.shouldUseTempToken() && !options.noToken) {
|
||
token = config_dev.getTempToken();
|
||
common_vendor.index.__f__("log", "at utils/request.js:173", "使用临时token进行开发测试");
|
||
}
|
||
if (!options.url || typeof options.url !== "string") {
|
||
reject(new Error("无效的URL"));
|
||
return;
|
||
}
|
||
const url = options.url.startsWith("/") ? options.url : "/" + options.url;
|
||
const requestOptions = {
|
||
url: BASE_URL + url,
|
||
method: options.method || "GET",
|
||
header: getRequestHeaders(options.header),
|
||
timeout: options.timeout || 6e4,
|
||
// 默认60秒超时
|
||
success: (res) => {
|
||
if (options.showLoading !== false) {
|
||
utils_loadingManager.hideLoading();
|
||
}
|
||
if (res.statusCode === 200) {
|
||
resolve(res.data);
|
||
} else {
|
||
handleResponseError(res, reject, options);
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
if (options.showLoading !== false) {
|
||
utils_loadingManager.hideLoading();
|
||
}
|
||
common_vendor.index.__f__("error", "at utils/request.js:212", "请求失败:", {
|
||
error: err,
|
||
url: requestOptions.url,
|
||
method: requestOptions.method,
|
||
baseUrl: BASE_URL,
|
||
originalUrl: options.url
|
||
});
|
||
let errorMessage = "网络错误";
|
||
if (err.errMsg) {
|
||
if (err.errMsg.includes("timeout")) {
|
||
errorMessage = "请求超时";
|
||
} else if (err.errMsg.includes("fail")) {
|
||
errorMessage = "网络连接失败";
|
||
}
|
||
}
|
||
common_vendor.index.showToast({
|
||
title: errorMessage,
|
||
icon: "none",
|
||
duration: 2e3
|
||
});
|
||
reject(err);
|
||
}
|
||
};
|
||
const noTokenUrls = ["/wxLogin", "/user/login"];
|
||
if (noTokenUrls.includes(url) || options.noToken) {
|
||
delete requestOptions.header.Authorization;
|
||
common_vendor.index.__f__("log", "at utils/request.js:244", "跳过token验证的接口:", url);
|
||
}
|
||
if (options.params && Object.keys(options.params).length > 0) {
|
||
requestOptions.data = { ...options.params };
|
||
} else if (options.data && Object.keys(options.data).length > 0) {
|
||
requestOptions.data = { ...options.data };
|
||
} else {
|
||
requestOptions.data = {};
|
||
}
|
||
try {
|
||
if (!options.noAppId && requestOptions.data && !requestOptions.data.appId) {
|
||
const appId = getCurrentAppId();
|
||
requestOptions.data.appId = appId;
|
||
common_vendor.index.__f__("log", "at utils/request.js:262", "自动添加appId:", appId, "到请求:", requestOptions.url);
|
||
}
|
||
} catch (error) {
|
||
common_vendor.index.__f__("error", "at utils/request.js:265", "添加appId时出错:", error);
|
||
if (requestOptions.data) {
|
||
requestOptions.data.appId = 1;
|
||
}
|
||
}
|
||
common_vendor.index.__f__("log", "at utils/request.js:273", "发起请求:", {
|
||
url: requestOptions.url,
|
||
method: requestOptions.method,
|
||
header: requestOptions.header,
|
||
data: requestOptions.data,
|
||
timeout: requestOptions.timeout,
|
||
baseUrl: BASE_URL
|
||
});
|
||
if (options.showLoading !== false) {
|
||
utils_loadingManager.showLoading(options.loadingText || utils_loadingManager.config.loadingText);
|
||
}
|
||
common_vendor.index.request(requestOptions);
|
||
});
|
||
}
|
||
function post(url, data = {}, options = {}) {
|
||
return request({
|
||
url,
|
||
method: "POST",
|
||
data,
|
||
...options
|
||
});
|
||
}
|
||
function getCurrentAppId() {
|
||
try {
|
||
return config.appId || config_dev.getAppId() || 1;
|
||
} catch (error) {
|
||
common_vendor.index.__f__("error", "at utils/request.js:361", "获取appId失败,使用默认值:", error);
|
||
return 1;
|
||
}
|
||
}
|
||
exports.post = post;
|
||
exports.request = request;
|
||
//# sourceMappingURL=../../.sourcemap/mp-weixin/utils/request.js.map
|