const store = require('../../utils/store.js'); const api = require('../../utils/api.js'); const upload = require('../../utils/upload.js'); function completion(pet) { if (!pet) return 0; const fields = ['name', 'emoji', 'type', 'gender', 'birthday', 'weight', 'stage', 'age', 'color', 'breed']; const filled = fields.filter((f) => pet[f]).length; return Math.round((filled / fields.length) * 100); } Page({ data: { pet: {}, user: {}, summary: { pets: 0, records: 0, reminders: 0, pro_status: 'none' }, profilePct: 0, sheetShow: false, sheetType: '', }, onLoad() { this._unsub = store.subscribe((pet) => this.setData({ pet, profilePct: completion(pet) })); }, onUnload() { if (this._unsub) this._unsub(); }, onShow() { store .ready() .then(() => { const pet = store.getPet(); this.setData({ pet, user: store.getUser() || {}, profilePct: completion(pet) }); this.loadSummary(); }) .catch((e) => wx.showToast({ title: e.message || '加载失败', icon: 'none' })); }, loadSummary() { api.userSummary().then((summary) => this.setData({ summary })).catch(() => {}); }, openSheet(e) { this.setData({ sheetType: e.currentTarget.dataset.type, sheetShow: true }); }, onPickAvatar() { upload .chooseAndUploadImage() .then((f) => api.updateProfile({ avatar_file_id: f.id })) .then((user) => { this.setData({ user }); store.setUser(user); wx.showToast({ title: '头像已更新', icon: 'success' }); }) .catch((e) => { if (e && e.canceled) return; wx.showToast({ title: (e && e.message) || '上传失败', icon: 'none' }); }); }, onAddPet() { this.setData({ sheetType: 'addPet', sheetShow: true }); }, onEditPet() { this.setData({ sheetType: 'editPet', sheetShow: true }); }, closeSheet() { this.setData({ sheetShow: false }); this.loadSummary(); }, onFab() { this.setData({ sheetType: 'ai', sheetShow: true }); }, });