feat: 建立小程序品牌运行时上下文
This commit is contained in:
1 parent
dcb40f629b
commit
f7fe6d6b20
6 files changed
+51
-1
No files matched your search
@@ -1,9 +1,11 @@
|
||||
const DEFAULT_API_BASE_URL = "http://172.16.6.123:3001";
|
||||
const DEFAULT_BRAND_SLUG = "default-brand";
|
||||
|
||||
App({
|
||||
onLaunch() {
|
||||
this.globalData = {
|
||||
apiBaseUrl: wx.getStorageSync("apiBaseUrl") || DEFAULT_API_BASE_URL,
|
||||
brandSlug: wx.getStorageSync("brandSlug") || DEFAULT_BRAND_SLUG,
|
||||
brand: {},
|
||||
};
|
||||
},
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const root = path.resolve(__dirname, "..");
|
||||
const requestSource = fs.readFileSync(path.join(root, "utils/request.js"), "utf8");
|
||||
const configSource = fs.readFileSync(path.join(root, "utils/config.js"), "utf8");
|
||||
if (
|
||||
!requestSource.includes('path.startsWith("/public/")') ||
|
||||
!requestSource.includes("brandSlug") ||
|
||||
!configSource.includes("brandSlug")
|
||||
) {
|
||||
console.error("miniapp: public requests must carry the configured brand slug");
|
||||
process.exit(1);
|
||||
}
|
||||
const fixtureFiles = ["linjing-door.json", "minimal-neutral.json", "doors-industry.json"];
|
||||
const fixtures = fixtureFiles.map((file) =>
|
||||
JSON.parse(fs.readFileSync(path.join(__dirname, "fixtures", file), "utf8")),
|
||||
|
||||
@@ -7,4 +7,7 @@ module.exports = {
|
||||
get brand() {
|
||||
return app.globalData.brand || {};
|
||||
},
|
||||
get brandSlug() {
|
||||
return app.globalData.brandSlug || "default-brand";
|
||||
},
|
||||
};
|
||||
@@ -1,9 +1,16 @@
|
||||
const config = require("./config");
|
||||
|
||||
function withBrandContext(path, options) {
|
||||
if (options.includeBrand === false || !path.startsWith("/public/")) return path;
|
||||
const separator = path.includes("?") ? "&" : "?";
|
||||
return path + separator + "brandSlug=" + encodeURIComponent(config.brandSlug);
|
||||
}
|
||||
|
||||
function request(path, options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const requestPath = withBrandContext(path, options);
|
||||
wx.request({
|
||||
url: config.apiBaseUrl + path,
|
||||
url: config.apiBaseUrl + requestPath,
|
||||
method: options.method || "GET",
|
||||
header: { accept: "application/json", ...(options.header || {}) },
|
||||
...(options.data === undefined ? {} : { data: options.data }),
|
||||
|
||||
Reference in new issue
Block a user