// pages/home/home — 1 我的(Tab,启动默认页) const { call } = require('../../utils/cloud.js'); const store = require('../../utils/store.js'); Page({ data: { userInfo: null, joinDays: 0, dogs: [], }, onShow() { if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setData({ selected: 1, walkState: (store.getWalkSession() || {}).status || 'idle' }); } this.ensureUser(); this.loadDogs(); }, // 兜底:若直接进首页(未经登录页)补一次 login 拿 userInfo async ensureUser() { let userInfo = store.getState().userInfo; if (!userInfo) { const res = await call('login'); if (res.ok) { userInfo = res.data.userInfo; store.set({ userInfo }); } } if (userInfo) { const joinDays = Math.max(0, Math.floor((Date.now() - userInfo.joinedAt) / 86400000)); this.setData({ userInfo, joinDays }); } }, async loadDogs() { const res = await call('dogManage', { action: 'list' }); if (res.ok) { store.set({ dogs: res.data }); this.setData({ dogs: res.data }); } }, goDogDetail(e) { const id = e.currentTarget.dataset.id; wx.navigateTo({ url: `/pages/dog-detail/dog-detail?id=${id}` }); }, goAddDog() { wx.navigateTo({ url: '/pages/dog-edit/dog-edit' }); }, goHistory() { wx.navigateTo({ url: '/pages/history/history' }); }, goAchievements() { const id = this.data.dogs[0] && this.data.dogs[0]._id; wx.navigateTo({ url: `/pages/achievements/achievements${id ? '?dogId=' + id : ''}` }); }, });