Files
smartDriveEEUniApp/unpackage/dist/dev/mp-weixin/common/request.js
2026-01-20 08:46:36 +08:00

153 lines
4.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
const common_vendor = require("./vendor.js");
const common_config = require("./config.js");
function getTenantId() {
try {
return common_vendor.index.getStorageSync("backend-tenant-id") || "";
} catch (e) {
common_vendor.index.__f__("error", "at common/request.js:15", "获取 tenantId 失败:", e);
return "";
}
}
function redirectToLogin() {
const loginPage = "/uni_modules/uni-id-pages/pages/login/login-withpwd";
const pages = getCurrentPages();
if (pages.length > 0) {
const currentPage = pages[pages.length - 1];
if (currentPage.route && currentPage.route.includes("login")) {
return;
}
}
common_vendor.index.reLaunch({
url: loginPage,
fail: (err) => {
common_vendor.index.__f__("error", "at common/request.js:38", "跳转登录页失败:", err);
}
});
}
function isLoginApi(url) {
if (!url)
return false;
return url.includes("/api/sys/auth/login");
}
function getToken() {
try {
return common_vendor.index.getStorageSync("backend-token") || "";
} catch (e) {
common_vendor.index.__f__("error", "at common/request.js:59", "获取 token 失败:", e);
return "";
}
}
function getRoleName() {
try {
return common_vendor.index.getStorageSync("backend-role-name") || "";
} catch (e) {
common_vendor.index.__f__("error", "at common/request.js:71", "获取 roleName 失败:", e);
return "";
}
}
function getScenario() {
try {
return common_vendor.index.getStorageSync("backend-scenario") || "";
} catch (e) {
common_vendor.index.__f__("error", "at common/request.js:83", "获取 scenario 失败:", e);
return "";
}
}
function request(options = {}) {
const {
url,
method = "GET",
data = {},
header = {},
timeout = 1e4,
needTenantId = true,
needToken = true,
...restOptions
} = options;
const fullUrl = url.startsWith("http") ? url : common_config.getApiUrl(url);
const apiEnv = typeof common_config.getApiEnv === "function" ? common_config.getApiEnv() : "";
common_vendor.index.__f__("log", "at common/request.js:118", `H5环境(${apiEnv})直接使用完整URL: ${fullUrl}`);
const requestHeader = {
"Content-Type": "application/json",
...header
};
if (needToken && !isLoginApi(fullUrl)) {
const token = getToken();
if (token) {
requestHeader["Authorization"] = `Bearer ${token}`;
} else {
delete requestHeader["Authorization"];
}
}
const roleName = getRoleName();
if (roleName) {
requestHeader["X-Role-Name"] = roleName;
}
const scenario = getScenario();
if (scenario) {
requestHeader["X-Scenario"] = scenario;
}
let requestData = { ...data };
if (needTenantId) {
const tenantId = getTenantId();
if (!isLoginApi(fullUrl) && !tenantId) {
common_vendor.index.__f__("warn", "at common/request.js:159", "[Request] 租户ID为空跳转到登录页面");
redirectToLogin();
return Promise.reject(new Error("未登录,请先登录"));
}
if (tenantId) {
requestHeader["X-Tenant-Id"] = tenantId;
}
}
return new Promise((resolve, reject) => {
common_vendor.index.request({
url: fullUrl,
method: method.toUpperCase(),
data: requestData,
header: requestHeader,
timeout,
...restOptions,
success: (res) => {
resolve(res);
},
fail: (err) => {
common_vendor.index.__f__("error", "at common/request.js:184", "[Request] 请求失败:", {
url: fullUrl,
method: method.toUpperCase(),
error: err
});
reject(err);
}
});
});
}
function get(url, data = {}, options = {}) {
return request({
url,
method: "GET",
data,
...options
});
}
function post(url, data = {}, options = {}) {
return request({
url,
method: "POST",
data,
...options
});
}
function put(url, data = {}, options = {}) {
return request({
url,
method: "PUT",
data,
...options
});
}
exports.get = get;
exports.post = post;
exports.put = put;
//# sourceMappingURL=../../.sourcemap/mp-weixin/common/request.js.map