const store = require('../../utils/store.js'); const api = require('../../utils/api.js'); const { toastErr } = require('../../utils/ui.js'); Page({ data: { user: {}, pets: [], summary: {}, initial: '', sheetShow: false, sheetType: '' }, onLoad() { this._unsub = store.subscribe(() => this.applyUser()); }, onUnload() { if (this._unsub) this._unsub(); }, onShow() { store .ready() .then(() => { this.applyUser(); this.loadSummary(); }) .catch((e) => toastErr(e)); }, applyUser() { const user = store.getUser() || {}; // WXML 取不了字符串下标,首字在这里算 this.setData({ pets: store.getPets(), user, initial: (user.nickname || '?').slice(0, 1) }); }, loadSummary() { api .userSummary() .then((summary) => this.setData({ summary })) .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.currentTarget.dataset.kind}` }); }, goPlan() { wx.navigateTo({ url: '/pages/plan/plan' }); }, goRecord() { wx.switchTab({ url: '/pages/record/record' }); }, onPickPet(e) { store.switchPet(e.currentTarget.dataset.id); wx.switchTab({ url: '/pages/home/home' }); }, openSheet(e) { this.setData({ sheetType: e.currentTarget.dataset.type, sheetShow: true }); }, closeSheet() { this.setData({ sheetShow: false }); }, onAddPet() { this.setData({ sheetType: 'addPet', sheetShow: true }); }, onFab() { wx.navigateTo({ url: '/pages/ai/ai' }); }, onShareAppMessage() { return { title: '肉垫计划 · 每天照顾好毛孩子', path: '/pages/home/home' }; }, });