feat: 建立小程序品牌运行时上下文
This commit is contained in:
1 parent
dcb40f629b
commit
f7fe6d6b20
6 files changed
+51
-1
No files matched your search
@@ -14,3 +14,4 @@
|
|||||||
- 下一步:运行全量门禁,记录根级检查结果并完成提交。
|
- 下一步:运行全量门禁,记录根级检查结果并完成提交。
|
||||||
- 全量门禁已通过:`format:check`、`lint`、`typecheck`、Admin build、Miniapp fixture/visual fixture、API integration `28/28`。
|
- 全量门禁已通过:`format:check`、`lint`、`typecheck`、Admin build、Miniapp fixture/visual fixture、API integration `28/28`。
|
||||||
- API lint 保留 6 个既有测试文件 `any` warning,无 error。
|
- API lint 保留 6 个既有测试文件 `any` warning,无 error。
|
||||||
|
- 小程序运行时已从唯一配置读取 `brandSlug`,公共请求层统一注入品牌上下文;客户和管理请求不会误带公共品牌参数。
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
const DEFAULT_API_BASE_URL = "http://172.16.6.123:3001";
|
const DEFAULT_API_BASE_URL = "http://172.16.6.123:3001";
|
||||||
|
const DEFAULT_BRAND_SLUG = "default-brand";
|
||||||
|
|
||||||
App({
|
App({
|
||||||
onLaunch() {
|
onLaunch() {
|
||||||
this.globalData = {
|
this.globalData = {
|
||||||
apiBaseUrl: wx.getStorageSync("apiBaseUrl") || DEFAULT_API_BASE_URL,
|
apiBaseUrl: wx.getStorageSync("apiBaseUrl") || DEFAULT_API_BASE_URL,
|
||||||
|
brandSlug: wx.getStorageSync("brandSlug") || DEFAULT_BRAND_SLUG,
|
||||||
brand: {},
|
brand: {},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,6 +1,16 @@
|
|||||||
const fs = require("node:fs");
|
const fs = require("node:fs");
|
||||||
const path = require("node:path");
|
const path = require("node:path");
|
||||||
const root = path.resolve(__dirname, "..");
|
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 fixtureFiles = ["linjing-door.json", "minimal-neutral.json", "doors-industry.json"];
|
||||||
const fixtures = fixtureFiles.map((file) =>
|
const fixtures = fixtureFiles.map((file) =>
|
||||||
JSON.parse(fs.readFileSync(path.join(__dirname, "fixtures", file), "utf8")),
|
JSON.parse(fs.readFileSync(path.join(__dirname, "fixtures", file), "utf8")),
|
||||||
|
|||||||
@@ -7,4 +7,7 @@ module.exports = {
|
|||||||
get brand() {
|
get brand() {
|
||||||
return app.globalData.brand || {};
|
return app.globalData.brand || {};
|
||||||
},
|
},
|
||||||
|
get brandSlug() {
|
||||||
|
return app.globalData.brandSlug || "default-brand";
|
||||||
|
},
|
||||||
};
|
};
|
||||||
@@ -1,9 +1,16 @@
|
|||||||
const config = require("./config");
|
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 = {}) {
|
function request(path, options = {}) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
const requestPath = withBrandContext(path, options);
|
||||||
wx.request({
|
wx.request({
|
||||||
url: config.apiBaseUrl + path,
|
url: config.apiBaseUrl + requestPath,
|
||||||
method: options.method || "GET",
|
method: options.method || "GET",
|
||||||
header: { accept: "application/json", ...(options.header || {}) },
|
header: { accept: "application/json", ...(options.header || {}) },
|
||||||
...(options.data === undefined ? {} : { data: options.data }),
|
...(options.data === undefined ? {} : { data: options.data }),
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
# 0007 小程序品牌运行时上下文
|
||||||
|
|
||||||
|
## Context
|
||||||
|
|
||||||
|
平台支持多个独立品牌,但小程序页面此前直接请求公共 API,默认依赖服务端 `DEFAULT_BRAND_ID`。同一套小程序代码切换品牌时,存在首页、业务列表和信任内容来自不同品牌的风险。
|
||||||
|
|
||||||
|
## Decision
|
||||||
|
|
||||||
|
小程序在 `app.js` 的 `globalData.brandSlug` 中维护唯一品牌身份,优先读取本地配置 `brandSlug`,没有配置时使用开发默认值 `default-brand`。`utils/request.js` 对 `/public/*` 请求统一追加 `brandSlug`,页面和模块不得自行拼接品牌参数。客户和管理接口不追加该参数。
|
||||||
|
|
||||||
|
API 通过 `PublicBrandContextService` 统一解析 `brandSlug`,没有 slug 时保留 `DEFAULT_BRAND_ID` 作为开发 fallback;未知 slug 返回 404。
|
||||||
|
|
||||||
|
## Alternatives
|
||||||
|
|
||||||
|
- 让每个页面手动拼接 `brandSlug`:容易遗漏,无法保证一致性。
|
||||||
|
- 只依赖 API 默认品牌:无法支持同一代码包服务多个品牌。
|
||||||
|
- 通过域名解析品牌:适合生产部署,但当前小程序开发和预览环境仍需要显式配置,后续可作为解析器的新增来源。
|
||||||
|
|
||||||
|
## Consequences
|
||||||
|
|
||||||
|
- 同一小程序代码可以通过运行时配置切换品牌内容和模板。
|
||||||
|
- 公共请求具有统一、可测试的品牌边界。
|
||||||
|
- 生产发布需要为每个小程序实例配置正确的 `brandSlug`,不能继续依赖默认值。
|
||||||
|
|
||||||
|
## Migration Plan
|
||||||
|
|
||||||
|
后续将把 `brandSlug` 从本地 storage 配置提升为构建环境或远程启动配置,并在发布检查中验证品牌 slug 存在且与目标品牌匹配。
|
||||||
Reference in new issue
Block a user