const namingStore = require("../../store/namingStore"); function showToast(title) { if (typeof tt === "undefined" || !tt.showToast) { console.warn("Toast:", title); return; } tt.showToast({ title, icon: "none", duration: 2000 }); } Page({ data: { results: [], matchSummary: "", elementsChart: [] }, onLoad() { const { results, analysis } = namingStore.getState(); if (!results || !results.length) { showToast("暂无生成结果,请返回重新生成"); setTimeout(() => { if (typeof tt !== "undefined" && tt.navigateBack) { tt.navigateBack(); } }, 1000); return; } const summary = analysis && (analysis.matchSummary || analysis.MatchSummary) ? analysis.matchSummary || analysis.MatchSummary : ""; const elements = Array.isArray(analysis && (analysis.elementDistribution || analysis.ElementDistribution)) ? analysis.elementDistribution || analysis.ElementDistribution : []; const total = elements.reduce((acc, current) => { const value = Number(current.count ?? current.Count ?? 0); return acc + (Number.isNaN(value) ? 0 : value); }, 0); const chart = elements.map((item) => { const elementName = item.element || item.Element || ""; const countValue = Number(item.count ?? item.Count ?? 0); const count = Number.isNaN(countValue) ? 0 : countValue; const percent = total > 0 ? Math.round((count / total) * 100) : 0; const widthPercent = percent > 0 ? Math.max(percent, 6) : 0; return { label: elementName || "未知", count, percent, percentText: `${percent}%`, percentWidth: `${widthPercent}%` }; }); const normalized = results.map((item) => ({ name: item.name, meaning: item.meaning || item.Meaning || "寓意待补充", elementReason: item.elementReason || item.ElementReason || "五行流转相济" })); this.setData({ results: normalized, matchSummary: summary, elementsChart: chart }); }, handleFavorite(event) { const { name, meaning } = event.currentTarget.dataset; const saved = namingStore.addFavorite({ name, meaning }); showToast(saved ? "已收藏" : "收藏失败"); }, handleBack() { if (typeof tt !== "undefined" && tt.navigateBack) { tt.navigateBack(); return; } if (typeof tt !== "undefined" && tt.redirectTo) { tt.redirectTo({ url: "/pages/home/index" }); } } });