195 lines
6.8 KiB
JavaScript
195 lines
6.8 KiB
JavaScript
"use strict";
|
||
const common_vendor = require("./vendor.js");
|
||
const uniStarter_config = require("../uni-starter.config.js");
|
||
const db = common_vendor._r.database();
|
||
function isLoginApi(url) {
|
||
if (!url)
|
||
return false;
|
||
return url.includes("/api/sys/auth/login");
|
||
}
|
||
function setupRequestInterceptor() {
|
||
const originalRequest = common_vendor.index.request;
|
||
common_vendor.index.request = function(options = {}) {
|
||
const { url, header = {}, method = "GET", ...restOptions } = options;
|
||
common_vendor.index.__f__("log", "at common/appInit.js:59", "[Request Interceptor] 开始拦截请求:", {
|
||
url,
|
||
method: method.toUpperCase(),
|
||
isLoginApi: isLoginApi(url),
|
||
originalOptions: options
|
||
});
|
||
let tenantId = "";
|
||
try {
|
||
tenantId = common_vendor.index.getStorageSync("backend-tenant-id") || "";
|
||
} catch (e) {
|
||
common_vendor.index.__f__("error", "at common/appInit.js:71", "[Request Interceptor] 获取 tenantId 失败:", e);
|
||
}
|
||
common_vendor.index.__f__("log", "at common/appInit.js:74", "[Request Interceptor] tenantId状态:", {
|
||
tenantId,
|
||
hasTenantId: !!tenantId
|
||
});
|
||
if (!isLoginApi(url) && !tenantId) {
|
||
common_vendor.index.__f__("warn", "at common/appInit.js:83", "[Request Interceptor] 租户ID为空,但允许请求继续执行,由后端处理认证");
|
||
} else if (isLoginApi(url)) {
|
||
common_vendor.index.__f__("log", "at common/appInit.js:86", "[Request Interceptor] 这是登录接口,跳过租户ID检查");
|
||
} else {
|
||
common_vendor.index.__f__("log", "at common/appInit.js:88", "[Request Interceptor] 租户ID存在,正常处理请求");
|
||
}
|
||
const newHeader = {
|
||
"Content-Type": "application/json",
|
||
...header
|
||
};
|
||
if (tenantId) {
|
||
newHeader["X-Tenant-Id"] = tenantId;
|
||
}
|
||
if (!isLoginApi(url)) {
|
||
try {
|
||
const token = common_vendor.index.getStorageSync("backend-token") || "";
|
||
if (token) {
|
||
newHeader["Authorization"] = `Bearer ${token}`;
|
||
} else {
|
||
delete newHeader["Authorization"];
|
||
}
|
||
} catch (e) {
|
||
common_vendor.index.__f__("error", "at common/appInit.js:115", "获取 token 失败:", e);
|
||
}
|
||
}
|
||
try {
|
||
const roleName = common_vendor.index.getStorageSync("backend-role-name") || "";
|
||
if (roleName) {
|
||
newHeader["X-Role-Name"] = roleName;
|
||
}
|
||
const scenario = common_vendor.index.getStorageSync("backend-scenario") || "";
|
||
if (scenario) {
|
||
newHeader["X-Scenario"] = scenario;
|
||
}
|
||
} catch (e) {
|
||
common_vendor.index.__f__("error", "at common/appInit.js:130", "获取 roleName 或 scenario 失败:", e);
|
||
}
|
||
const finalRequestOptions = {
|
||
...restOptions,
|
||
url,
|
||
method,
|
||
header: newHeader
|
||
};
|
||
common_vendor.index.__f__("log", "at common/appInit.js:141", "[Request Interceptor] 准备发送请求:", {
|
||
url,
|
||
method: method.toUpperCase(),
|
||
headers: newHeader,
|
||
hasData: !!restOptions.data
|
||
});
|
||
return new Promise((resolve, reject) => {
|
||
common_vendor.index.__f__("log", "at common/appInit.js:151", "[Request Interceptor] 开始调用原始 uni.request");
|
||
originalRequest.call(common_vendor.index, {
|
||
...finalRequestOptions,
|
||
success: (res) => {
|
||
common_vendor.index.__f__("log", "at common/appInit.js:155", "[Request Interceptor] 请求成功:", {
|
||
url,
|
||
method: method.toUpperCase(),
|
||
statusCode: res.statusCode,
|
||
hasData: !!res.data
|
||
});
|
||
if (options.success) {
|
||
options.success(res);
|
||
}
|
||
resolve(res);
|
||
},
|
||
fail: (err) => {
|
||
common_vendor.index.__f__("error", "at common/appInit.js:170", "[Request Interceptor] 请求失败:", {
|
||
url,
|
||
method: method.toUpperCase(),
|
||
error: err,
|
||
errorMessage: err.errMsg || "未知错误"
|
||
});
|
||
if (options.fail) {
|
||
options.fail(err);
|
||
}
|
||
reject(err);
|
||
},
|
||
complete: (res) => {
|
||
common_vendor.index.__f__("log", "at common/appInit.js:184", "[Request Interceptor] 请求完成:", {
|
||
url,
|
||
method: method.toUpperCase(),
|
||
hasResponse: !!res
|
||
});
|
||
if (options.complete) {
|
||
options.complete(res);
|
||
}
|
||
}
|
||
});
|
||
});
|
||
};
|
||
}
|
||
async function initApp() {
|
||
uniStarter_config.config.debug;
|
||
setupRequestInterceptor();
|
||
setTimeout(() => {
|
||
try {
|
||
const app = getApp({
|
||
allowDefault: true
|
||
});
|
||
if (app && app.globalData) {
|
||
app.globalData.config = uniStarter_config.config;
|
||
}
|
||
} catch (e) {
|
||
common_vendor.index.__f__("warn", "at common/appInit.js:217", "[appInit] 挂载配置失败:", e);
|
||
}
|
||
}, 1);
|
||
function onDBError({
|
||
code,
|
||
// 错误码详见https://uniapp.dcloud.net.cn/uniCloud/clientdb?id=returnvalue
|
||
message
|
||
}) {
|
||
common_vendor.index.__f__("log", "at common/appInit.js:230", "onDBError", {
|
||
code,
|
||
message
|
||
});
|
||
common_vendor.index.__f__("error", "at common/appInit.js:235", code, message);
|
||
}
|
||
db.on("error", onDBError);
|
||
common_vendor._r.interceptObject({
|
||
async invoke({
|
||
objectName,
|
||
// 云对象名称
|
||
methodName,
|
||
// 云对象的方法名称
|
||
params
|
||
// 参数列表
|
||
}) {
|
||
if (objectName == "uni-id-co" && (methodName.includes("loginBy") || ["login", "registerUser"].includes(methodName))) {
|
||
common_vendor.index.__f__("log", "at common/appInit.js:254", "执行登录相关云对象");
|
||
params[0].inviteCode = await new Promise((callBack) => {
|
||
common_vendor.index.getClipboardData({
|
||
success: function(res) {
|
||
common_vendor.index.__f__("log", "at common/appInit.js:258", "剪切板内容:" + res.data);
|
||
if (res.data.slice(0, 18) == "uniInvitationCode:") {
|
||
let uniInvitationCode = res.data.slice(18, 38);
|
||
common_vendor.index.__f__("log", "at common/appInit.js:261", "当前用户是其他用户推荐下载的,推荐者的code是:" + uniInvitationCode);
|
||
callBack(uniInvitationCode);
|
||
} else {
|
||
callBack();
|
||
}
|
||
},
|
||
fail() {
|
||
common_vendor.index.__f__("log", "at common/appInit.js:273", "error--");
|
||
callBack();
|
||
},
|
||
complete() {
|
||
common_vendor.index.hideToast();
|
||
}
|
||
});
|
||
});
|
||
}
|
||
},
|
||
success(e) {
|
||
common_vendor.index.__f__("log", "at common/appInit.js:288", e);
|
||
},
|
||
complete() {
|
||
},
|
||
fail(e) {
|
||
common_vendor.index.__f__("error", "at common/appInit.js:294", e);
|
||
}
|
||
});
|
||
}
|
||
exports.initApp = initApp;
|
||
//# sourceMappingURL=../../.sourcemap/mp-weixin/common/appInit.js.map
|