fix: 补全小程序媒体地址并支持构建注入 AppID

This commit is contained in:
que01 committed 2026-10-13 09:51:38 +08:00
1 parent 5c6ad02f46
commit 07629a4596
4 files changed
+18 -10

No files matched your search

+1
View File
@@ -3,3 +3,4 @@ TEMPLATE_KEY=acheng-campus-food
BRAND_SLUG=default-brand
API_BASE_URL=http://localhost:3001
PREVIEW_PORT=3002
WEAPP_APP_ID=touristappid
+1 -1
View File
@@ -31,7 +31,7 @@ TEMPLATE_KEY=linjing-editorial pnpm --filter @personal-brand/taro build:h5
TEMPLATE_KEY=linjing-editorial pnpm --filter @personal-brand/taro build:weapp
```
输出 `apps/taro/dist/<templateKey>/h5` 和 `weapp`。微信开发者工具导入对应 weapp 目录;正式登录需在项目配置里设置与 API 相匹配的 AppID,配置请求合法域名。默认 touristappid 仅用于浏览,不含密钥。
输出 `apps/taro/dist/<templateKey>/h5` 和 `weapp`。微信开发者工具导入对应 weapp 目录;构建时通过 `API_BASE_URL` 设置 API 地址,通过 `WEAPP_APP_ID` 或 `TARO_APP_ID` 设置小程序 AppID,并在微信公众平台配置请求合法域名。默认 touristappid 仅用于浏览,不含密钥。
页面结构和样式以 React 原型为参考,当前原生小程序用于校对字段、结构化来源、认证与提交行为。模板实现位于各 package 的 `taro/`;公共请求、表单、记录和状态位于此目录。新模板须提供 manifest、Taro 实现和验收用例,不新增全局 seed。
+1 -8
View File
@@ -17,6 +17,7 @@ const templates = fs
const key = process.env.TEMPLATE_KEY || templates[0];
if (!key) throw new Error("No Taro template package found");
if (!templates.includes(key)) throw new Error(`Unsupported Taro template: ${key}`);
process.env.TARO_APP_ID ||= process.env.WEAPP_APP_ID;
const template = path.join(root, "packages", `template-${key}`);
const manifest = JSON.parse(fs.readFileSync(path.join(template, "template.json"), "utf8")) as {
packageKey: string;
@@ -60,14 +61,6 @@ export default defineConfig({
to: `dist/template-preview/h5/assets/${templateKey}`,
}))
: []),
...(process.env.TARO_ENV === "weapp"
? [
{
from: path.resolve(__dirname, "../project.config.json"),
to: `dist/${key}/weapp/project.config.json`,
},
]
: []),
...(!previewAllTemplates
? [
{
+15 -1
View File
@@ -47,7 +47,7 @@ export async function loadWorkspace() {
const data = await request<RuntimeWorkspace>("/public/workspace");
const editorPreview = isEditorPreview();
if (!editorPreview && data.publication?.status !== "published") throw new Error("品牌尚未发布");
return {
return resolveMediaUrls({
...data,
brand: data.brand ?? {},
templateData: data.templateData ?? {},
@@ -58,7 +58,21 @@ export async function loadWorkspace() {
capabilities: data.capabilities ?? [],
theme: data.theme ?? { colors: {} },
plugins: data.plugins ?? [],
});
}
function resolveMediaUrls(workspace: RuntimeWorkspace): RuntimeWorkspace {
const resolve = (value: unknown): unknown => {
if (typeof value === "string") {
return value.startsWith("/media/") ? `${API_BASE_URL.replace(/\/$/, "")}${value}` : value;
}
if (Array.isArray(value)) return value.map(resolve);
if (value && typeof value === "object") {
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, resolve(item)]));
}
return value;
};
return resolve(workspace) as RuntimeWorkspace;
}
export async function ensureSession() {
if (hasSession()) return;