cbfcaada55
竞品那张「居民身份卡」,完整做了。 ## 后端 service/wxacode.go —— 小程序码。两步:先拿 access_token(2h 有效,进程内缓存、 提前 5 分钟过期),再调 getwxacodeunlimit 换一张 PNG 存到 MinIO 返回 URL。 同一只宠物的码 object 名按 pet id 定死,存在就直接返回不重新生成 —— 微信侧有日调用配额,不能每次点都烧一次。 ⚠️ getwxacodeunlimit 要求 page 在**已发布**的小程序版本里。开发/未发布时返回 errcode 41030,实测确认。这时身份卡照常出、只是没码(qr_ready=false), 发布后自动就有,不用改代码。best-effort,取不到不算错。 service/idcard.go —— 组装卡片内容。身份 ID 用「宠物雪花 id 的数字 + 生日」 拼成 18 位、每 4 位空一下,纯装饰不入库不校验,同一只每次都一样。 介绍按物种给默认(汪星人/喵星人来地球啦~)。有效期用生日起算。 GET /api/pets/:id/id-card storage 加了 Exists(StatObject),生成前先查。 ## 前端 pages/idcard 离屏 canvas 2d 画卡,导出成图展示 + 存相册 + 分享(open-type=share, imageUrl 用画好的卡当封面)。 两张远程图(头像 + 小程序码)都用 node.createImage() 异步加载, **Promise.all 等两张都就绪再开画** —— 否则小程序码会画不上或画一半 (规划里专门点过名的坑)。任一张加载失败 resolve(null),缺图也照画: 头像缺→画物种 emoji,码缺→画「小程序码 待发布」占位框。 存相册的权限被拒过一次后 wx.authorize 不再弹,走 openSetting 引导(同海报那套)。 绘制复用了 bottom-sheet 里 poster 的 canvas 模式(dpr 放大、roundRect、 canvasToTempFilePath),但 poster 只画文字不画远程图,这里的双图加载是新写的。 入口:「我的」→ 身份卡,用当前选中的宠物。 ## 验证(预生产库实跑) id-card 接口返回:姓名/性别/品种/生日/介绍/身份ID/有效期 全对 身份 ID = 3410 4333 1420 2503 17(宠物id数字+生日,稳定) qr_ready = false,日志确认 41030 invalid page(小程序未发布,预期) canvas 版式用 HTML 复刻确认过:未发布=占位、发布后=真码 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
88 lines
2.8 KiB
JavaScript
88 lines
2.8 KiB
JavaScript
const store = require('../../utils/store.js');
|
|
const api = require('../../utils/api.js');
|
|
const { toastErr } = require('../../utils/ui.js');
|
|
const { syncTabBar } = require('../../utils/tabbar.js');
|
|
|
|
Page({
|
|
data: { user: {}, pets: [], card: null, sheetShow: false, sheetType: '' },
|
|
onLoad() {
|
|
this._unsub = store.subscribe(() => this.applyUser());
|
|
},
|
|
onUnload() {
|
|
if (this._unsub) this._unsub();
|
|
},
|
|
onShow() {
|
|
syncTabBar(this);
|
|
store
|
|
.ready()
|
|
.then(() => {
|
|
this.applyUser();
|
|
this.loadCard();
|
|
})
|
|
.catch((e) => toastErr(e));
|
|
},
|
|
applyUser() {
|
|
this.setData({ pets: store.getPets(), user: store.getUser() || {} });
|
|
},
|
|
// 头部数据走 /users/:id/profile,和别人看我时是同一个接口——
|
|
// 换成 /user/summary 的话两边字段会对不上,装扮效果也预览不到
|
|
loadCard() {
|
|
const uid = (this.data.user && this.data.user.id) || '';
|
|
if (!uid) return;
|
|
api
|
|
.userCard(uid)
|
|
.then((card) => this.setData({ card }))
|
|
.catch(() => {});
|
|
},
|
|
// 看自己的公开主页,和别人看到的是同一个页面、同一套渲染
|
|
previewPublic() {
|
|
const uid = this.data.user && this.data.user.id;
|
|
if (uid) wx.navigateTo({ url: `/pages/user/user?id=${uid}` });
|
|
},
|
|
goSettings() {
|
|
wx.navigateTo({ url: '/pages/settings/settings' });
|
|
},
|
|
goRelations(e) {
|
|
const uid = (this.data.user && this.data.user.id) || '';
|
|
if (!uid) return wx.showToast({ title: '登录信息还没就绪', icon: 'none' });
|
|
wx.navigateTo({ url: `/pages/relations/relations?id=${uid}&kind=${e.detail.kind}` });
|
|
},
|
|
// 宠物档案:当前这只的编辑页
|
|
goPetForm() {
|
|
const id = store.currentPetId();
|
|
if (!id) return wx.navigateTo({ url: '/pages/petform/petform' });
|
|
wx.navigateTo({ url: '/pages/petform/petform?id=' + id });
|
|
},
|
|
goDecorate() {
|
|
wx.navigateTo({ url: '/pages/decorate/decorate' });
|
|
},
|
|
// 身份卡:当前这只的
|
|
goIDCard() {
|
|
const id = store.currentPetId();
|
|
if (!id) return wx.showToast({ title: '先选一只毛孩子', icon: 'none' });
|
|
wx.navigateTo({ url: '/pages/idcard/idcard?id=' + id });
|
|
},
|
|
goPlan() {
|
|
wx.navigateTo({ url: '/pages/plan/plan' });
|
|
},
|
|
// 列表行传 dataset,宠物名片墙传 detail,两边都进这里
|
|
onPickPet(e) {
|
|
const id = (e.detail && e.detail.id) || e.currentTarget.dataset.id;
|
|
if (!id) return;
|
|
store.switchPet(id);
|
|
wx.switchTab({ url: '/pages/home/home' });
|
|
},
|
|
openSheet(e) {
|
|
this.setData({ sheetType: e.currentTarget.dataset.type, sheetShow: true });
|
|
},
|
|
closeSheet() {
|
|
this.setData({ sheetShow: false });
|
|
},
|
|
onAddPet() {
|
|
wx.navigateTo({ url: '/pages/petform/petform' });
|
|
},
|
|
onShareAppMessage() {
|
|
return { title: '肉垫计划 · 每天照顾好毛孩子', path: '/pages/home/home' };
|
|
},
|
|
});
|