适配小程序代码结构

This commit is contained in:
zhonghua1
2026-01-20 08:46:36 +08:00
parent 3b169467ac
commit 9c00db8ff5
615 changed files with 62726 additions and 8 deletions

View File

@@ -0,0 +1,169 @@
"use strict";
const common_vendor = require("./vendor.js");
const uniStarter_config = require("../uni-starter.config.js");
const db = common_vendor.tr.database();
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/appInit.js:32", "跳转登录页失败:", err);
}
});
}
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;
let tenantId = "";
try {
tenantId = common_vendor.index.getStorageSync("backend-tenant-id") || "";
} catch (e) {
common_vendor.index.__f__("error", "at common/appInit.js:63", "获取 tenantId 失败:", e);
}
if (!isLoginApi(url) && !tenantId) {
common_vendor.index.__f__("warn", "at common/appInit.js:68", "[Request Interceptor] 租户ID为空跳转到登录页面");
redirectToLogin();
return Promise.reject(new Error("未登录,请先登录"));
}
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:98", "获取 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:113", "获取 roleName 或 scenario 失败:", e);
}
return new Promise((resolve, reject) => {
originalRequest.call(common_vendor.index, {
...restOptions,
url,
method,
header: newHeader,
success: (res) => {
if (options.success) {
options.success(res);
}
resolve(res);
},
fail: (err) => {
common_vendor.index.__f__("error", "at common/appInit.js:133", "[Request Interceptor] 请求失败:", {
url,
method: method.toUpperCase(),
error: err
});
if (options.fail) {
options.fail(err);
}
reject(err);
},
complete: (res) => {
if (options.complete) {
options.complete(res);
}
}
});
});
};
}
async function initApp() {
uniStarter_config.config.debug;
setupRequestInterceptor();
setTimeout(() => {
getApp({
allowDefault: true
}).globalData.config = uniStarter_config.config;
}, 1);
function onDBError({
code,
// 错误码详见https://uniapp.dcloud.net.cn/uniCloud/clientdb?id=returnvalue
message
}) {
common_vendor.index.__f__("log", "at common/appInit.js:180", "onDBError", {
code,
message
});
common_vendor.index.__f__("error", "at common/appInit.js:185", code, message);
}
db.on("error", onDBError);
common_vendor.tr.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:204", "执行登录相关云对象");
params[0].inviteCode = await new Promise((callBack) => {
common_vendor.index.getClipboardData({
success: function(res) {
common_vendor.index.__f__("log", "at common/appInit.js:208", "剪切板内容:" + 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:211", "当前用户是其他用户推荐下载的,推荐者的code是" + uniInvitationCode);
callBack(uniInvitationCode);
} else {
callBack();
}
},
fail() {
common_vendor.index.__f__("log", "at common/appInit.js:223", "error--");
callBack();
},
complete() {
common_vendor.index.hideToast();
}
});
});
}
},
success(e) {
common_vendor.index.__f__("log", "at common/appInit.js:238", e);
},
complete() {
},
fail(e) {
common_vendor.index.__f__("error", "at common/appInit.js:244", e);
}
});
}
exports.initApp = initApp;
//# sourceMappingURL=../../.sourcemap/mp-weixin/common/appInit.js.map