Files
sundynix-pets/pets-fe/components/pet-id-card/index.js
T
Blizzard b1439b0fe5
ci-cd / verify (push) Successful in 1m39s
ci-cd / deploy (push) Skipped
Move pet card actions into menu and use circular brand logos
2026-10-05 11:38:15 +08:00

56 lines
1.7 KiB
JavaScript

const { profile, renderCard, saveToAlbum } = require("../../utils/pet-card");
const { toast } = require("../../utils/api");
Component({
properties: { pet: { type: Object, value: {} } },
data: { card: {}, saving: false },
observers: {
pet(value) {
this.setData({ card: profile(value || {}) });
},
},
methods: {
async more() {
if (this.data.saving) return;
try {
const result = await wx.showActionSheet({
itemList: ["编辑档案", "保存身份卡到相册"],
itemColor: "#795a4b",
});
if (result.tapIndex === 0) this.edit();
else if (result.tapIndex === 1) await this.save();
} catch (error) {
if (!/cancel/.test(error.errMsg || "")) toast(error);
}
},
edit() {
this.triggerEvent("edit", { id: this.data.pet.id });
},
async generate() {
const canvas = await new Promise((resolve, reject) =>
this.createSelectorQuery()
.select("#card-canvas")
.fields({ node: true, size: true })
.exec((result) =>
result[0] && result[0].node
? resolve(result[0].node)
: reject(new Error("卡片还未准备好,请重试")),
),
);
return renderCard(canvas, this.data.pet);
},
async save() {
if (this.data.saving) return;
this.setData({ saving: true });
try {
const path = await this.generate();
if (await saveToAlbum(path))
wx.showToast({ title: "身份卡已保存到相册", icon: "success" });
} catch (error) {
toast(error);
} finally {
this.setData({ saving: false });
}
},
},
});