Files
MiniProgram/NamingAssistant/pages/result/index.js

82 lines
2.5 KiB
JavaScript
Raw Normal View History

2025-11-05 17:26:52 +08:00
const namingStore = require("../../store/namingStore");
2025-11-05 00:22:09 +08:00
function showToast(title) {
if (typeof tt === "undefined" || !tt.showToast) {
console.warn("Toast:", title);
return;
}
tt.showToast({
title,
icon: "none",
duration: 2000
});
}
Page({
data: {
2025-11-05 17:26:52 +08:00
results: [],
2025-11-06 19:23:37 +08:00
matchSummary: "",
elementsChart: []
2025-11-05 00:22:09 +08:00
},
onLoad() {
2025-11-05 17:26:52 +08:00
const { results, analysis } = namingStore.getState();
2025-11-05 00:22:09 +08:00
if (!results || !results.length) {
2025-11-05 17:26:52 +08:00
showToast("暂无生成结果,请返回重新生成");
2025-11-05 00:22:09 +08:00
setTimeout(() => {
if (typeof tt !== "undefined" && tt.navigateBack) {
tt.navigateBack();
}
}, 1000);
return;
}
2025-11-06 19:23:37 +08:00
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}%`
};
});
2025-11-05 17:26:52 +08:00
const normalized = results.map((item) => ({
name: item.name,
2025-11-06 19:23:37 +08:00
meaning: item.meaning || item.Meaning || "寓意待补充",
elementReason: item.elementReason || item.ElementReason || "五行流转相济"
2025-11-05 17:26:52 +08:00
}));
this.setData({
results: normalized,
2025-11-06 19:23:37 +08:00
matchSummary: summary,
elementsChart: chart
2025-11-05 17:26:52 +08:00
});
2025-11-05 00:22:09 +08:00
},
handleFavorite(event) {
const { name, meaning } = event.currentTarget.dataset;
2025-11-05 17:26:52 +08:00
const saved = namingStore.addFavorite({ name, meaning });
showToast(saved ? "已收藏" : "收藏失败");
2025-11-05 00:22:09 +08:00
},
handleBack() {
if (typeof tt !== "undefined" && tt.navigateBack) {
tt.navigateBack();
return;
}
if (typeof tt !== "undefined" && tt.redirectTo) {
tt.redirectTo({ url: "/pages/home/index" });
}
}
});