const { loadWorkspaceWithFallback } = require("../../utils/runtime-workspace"); const { openPage } = require("../../utils/navigation"); const config = require("../../utils/config"); Page({ data: { error: "", offerings: [], filtered: [], brand: {}, presentation: {}, category: "all", // 分类来自后台为业务提供项设置的分类,不在模板里写死。 categories: [], loadedBrandSlug: "", }, onLoad() { this.loadWorkspace(); }, onShow() { if (this.data.loadedBrandSlug && this.data.loadedBrandSlug !== config.brandSlug) { this.loadWorkspace(); } }, loadWorkspace() { loadWorkspaceWithFallback() .then((workspace) => { const items = workspace.offerings || []; const labels = Array.from(new Set(items.map((item) => item.category).filter(Boolean))); this.setData({ offerings: items, filtered: items, category: "all", categories: [ { key: "all", label: "全部" }, ...labels.map((label) => ({ key: label, label })), ], brand: workspace.brand || {}, presentation: workspace.brand?.visualPresentation?.offerings || {}, loadedBrandSlug: config.brandSlug, }); }) .catch((error) => this.setData({ error: error.message })); }, switchCategory(e) { const category = e.detail.key; const items = this.data.offerings || []; this.setData({ category, filtered: category === "all" ? items : items.filter((item) => item.category === category), }); }, open(e) { openPage("/pages/offering/detail?id=" + e.detail.id); }, inquire() { openPage("/pages/inquiry/index"); }, });