Files
Pet3/miniprogram/pages/home/home.js
2026-06-25 18:24:29 +08:00

59 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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 : ''}` });
},
});