Files
sundynix-pets/pets-fe/components/pet-id-card/index.js
T
Blizzard cb4ffa13e9
ci-cd / verify (push) Successful in 1m31s
ci-cd / deploy (push) Skipped
Add one-way task completion and exportable pet identity cards
2026-10-05 11:32:32 +08:00

43 lines
1.2 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: {
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 });
}
},
},
});