feat: add multi-source breed encyclopedia and two-hour refresh sessions
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
const { secure } = require("../../utils/api");
|
||||
const providers = {
|
||||
cku: "CKU",
|
||||
fci: "FCI",
|
||||
akc: "AKC",
|
||||
rkc: "英国皇家犬业俱乐部",
|
||||
editorial: "编辑整理",
|
||||
};
|
||||
Page({
|
||||
data: {
|
||||
breed: null,
|
||||
loading: true,
|
||||
error: "",
|
||||
traits: [],
|
||||
facts: [],
|
||||
references: [],
|
||||
traitSource: "",
|
||||
},
|
||||
onLoad(options) {
|
||||
this.id = options.id;
|
||||
this.load();
|
||||
},
|
||||
async load() {
|
||||
if (this.pending) return;
|
||||
this.pending = true;
|
||||
this.setData({ loading: true, error: "" });
|
||||
try {
|
||||
const breed = await secure("/breeds/" + this.id);
|
||||
let scores = {};
|
||||
try {
|
||||
scores = JSON.parse(breed.traits || "{}") || {};
|
||||
} catch (_) {}
|
||||
const fieldSources = breed.field_sources || {};
|
||||
const facts = [
|
||||
["origin", "起源地"],
|
||||
["fci_number", "FCI 编号"],
|
||||
["height", "参考肩高"],
|
||||
["weight", "参考体重"],
|
||||
["life_span", "寿命参考"],
|
||||
["breed_group", "犬种分组"],
|
||||
].map(([key, label]) => ({
|
||||
key,
|
||||
label,
|
||||
value: breed[key] || "待补充",
|
||||
source: breed[key] ? providers[fieldSources[key]] || "" : "",
|
||||
}));
|
||||
const references = (breed.references || []).map((ref) => ({
|
||||
...ref,
|
||||
name: providers[ref.provider] || ref.provider,
|
||||
}));
|
||||
this.setData({
|
||||
breed,
|
||||
facts,
|
||||
references,
|
||||
traits: Object.keys(scores)
|
||||
.filter(
|
||||
(name) =>
|
||||
Number.isInteger(scores[name]) &&
|
||||
scores[name] >= 1 &&
|
||||
scores[name] <= 5,
|
||||
)
|
||||
.map((name) => ({ name, value: scores[name] })),
|
||||
traitSource: providers[fieldSources.traits] || "来源资料",
|
||||
exerciseSource: providers[fieldSources.exercise] || "",
|
||||
groomingSource: providers[fieldSources.grooming] || "",
|
||||
});
|
||||
} catch (error) {
|
||||
this.setData({ error: error.message });
|
||||
} finally {
|
||||
this.pending = false;
|
||||
this.setData({ loading: false });
|
||||
}
|
||||
},
|
||||
source(e) {
|
||||
const ref = this.data.references.find(
|
||||
(item) => item.id === e.currentTarget.dataset.id,
|
||||
);
|
||||
const url = ref ? ref.url : this.data.breed?.source_url;
|
||||
if (!url) return;
|
||||
wx.setClipboardData({
|
||||
data: url,
|
||||
success: () => wx.showToast({ title: "资料链接已复制", icon: "none" }),
|
||||
});
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user