7093e6517d
token 过期后小程序静默白屏(影响所有用户) - JWT 有效期 7 天,过期后 40100 只 clearToken 不重新登录,而 store.ready 的 promise 已缓存不会重跑,加上 23 处静默 catch, 用户看到的是「什么都没有、也没有任何提示」,只能重启小程序 - request 层加 token 失效钩子:自动重新登录并重试一次(只重试一次, 防止登录接口本身故障时死循环);store 注册钩子重置引导缓存 - 补 15s 请求超时、60s 上传超时,超时给明确文案(原先默认 60s 卡住无反馈) 请求翻倍 - 每个 tab 页同时用 store.subscribe 和 onShow→ready().then() 加载, 而 ready() 内部 loadPets 会 notify 触发 subscriber,首次进入请求翻倍 - subscriber 改为只处理「切换宠物 / 数据变更」,首次加载交给 onShow - 首页 bind:save 与组件实际触发的 saved 事件名不一致,onSheetSave 从未生效;一并修正,并去掉关闭弹层时的全量重载 失败伪装成空态 - 社区请求失败时显示「还没有帖子,来发第一条吧」,把故障说成没数据。 改为区分「加载失败(可点击重试)」「关注 tab 为空」「确实没有帖子」 - 新增 utils/ui.js 统一错误提示(同文案 3 秒内去重,避免并发失败刷屏), 首页与记录页主数据加载失败不再静默 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
176 lines
4.6 KiB
JavaScript
176 lines
4.6 KiB
JavaScript
const store = require('../../utils/store.js');
|
|
const api = require('../../utils/api.js');
|
|
const { toastErr } = require('../../utils/ui.js');
|
|
|
|
function todayStr() {
|
|
const d = new Date();
|
|
const p = (n) => (n < 10 ? '0' + n : '' + n);
|
|
return d.getFullYear() + '-' + p(d.getMonth() + 1) + '-' + p(d.getDate());
|
|
}
|
|
|
|
function mapTask(t) {
|
|
return {
|
|
id: t.id,
|
|
title: t.title,
|
|
sub: t.description,
|
|
priority: t.priority,
|
|
done: t.done,
|
|
sheet: t.sheet_type,
|
|
};
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
pet: {},
|
|
summary: { greeting: '', insights: [], week: [], advice: '', health_pct: 0, health_status: '正常' },
|
|
tasks: [],
|
|
firstArticle: null,
|
|
selectedDate: '',
|
|
taskLabel: '今日任务',
|
|
todayDate: '',
|
|
sheetShow: false,
|
|
sheetType: '',
|
|
},
|
|
onLoad() {
|
|
this.setData({ todayDate: todayStr() });
|
|
this._unsub = store.subscribe((pet) => {
|
|
this.setData({ pet });
|
|
// 首次由 onShow 统一加载,这里只处理之后的「切换宠物 / 数据变更」,避免重复请求
|
|
if (!this._inited) return;
|
|
this.loadAll();
|
|
});
|
|
},
|
|
onUnload() {
|
|
if (this._unsub) this._unsub();
|
|
},
|
|
onShow() {
|
|
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
|
|
this.getTabBar().setData({ selected: 0 });
|
|
}
|
|
store
|
|
.ready()
|
|
.then(() => {
|
|
this._inited = true;
|
|
this.setData({ pet: store.getPet() });
|
|
this.loadAll();
|
|
})
|
|
.catch((e) => wx.showToast({ title: e.message || '加载失败', icon: 'none' }));
|
|
},
|
|
loadAll() {
|
|
this.loadSummary();
|
|
this.loadTasks();
|
|
if (!this.data.firstArticle) {
|
|
api
|
|
.listArticles()
|
|
.then((list) => {
|
|
if (list && list[0]) {
|
|
this.setData({ firstArticle: { id: list[0].id, icon: list[0].icon, title: list[0].title, desc: list[0].description } });
|
|
}
|
|
})
|
|
.catch(() => {});
|
|
}
|
|
},
|
|
loadSummary() {
|
|
const id = store.currentPetId();
|
|
if (!id) return;
|
|
api
|
|
.homeSummary(id)
|
|
.then((summary) => this.setData({ summary }))
|
|
.catch((e) => toastErr(e));
|
|
},
|
|
loadTasks() {
|
|
const id = store.currentPetId();
|
|
if (!id) return;
|
|
const date = this.data.selectedDate || this.data.todayDate;
|
|
api
|
|
.getTasks(id, date)
|
|
.then((tasks) => this.setData({ tasks: (tasks || []).map(mapTask) }))
|
|
.catch((e) => toastErr(e));
|
|
},
|
|
onTapDay(e) {
|
|
const { date, active } = e.currentTarget.dataset;
|
|
if (!date) return;
|
|
const parts = date.split('-');
|
|
this.setData({
|
|
selectedDate: date,
|
|
taskLabel: active ? '今日任务' : +parts[1] + '月' + +parts[2] + '日',
|
|
});
|
|
this.loadTasks();
|
|
},
|
|
backToToday() {
|
|
this.setData({ selectedDate: this.data.todayDate, taskLabel: '今日任务' });
|
|
this.loadTasks();
|
|
},
|
|
onTapTask(e) {
|
|
const i = e.currentTarget.dataset.index;
|
|
const t = this.data.tasks[i];
|
|
if (t.sheet) {
|
|
this.openSheetType(t.sheet);
|
|
return;
|
|
}
|
|
api
|
|
.toggleTask(t.id)
|
|
.then((res) => {
|
|
this.setData({ [`tasks[${i}].done`]: res.done });
|
|
this.loadSummary();
|
|
})
|
|
.catch(() => {});
|
|
},
|
|
completeTasks() {
|
|
const id = store.currentPetId();
|
|
if (!id) return;
|
|
api
|
|
.completeAllTasks(id)
|
|
.then((tasks) => {
|
|
this.setData({ tasks: (tasks || []).map(mapTask) });
|
|
this.loadSummary();
|
|
})
|
|
.catch(() => {});
|
|
},
|
|
openSheet(e) {
|
|
this.openSheetType(e.currentTarget.dataset.type);
|
|
},
|
|
openSheetType(type) {
|
|
this.setData({ sheetType: type, sheetShow: true });
|
|
},
|
|
closeSheet() {
|
|
// 只关闭。数据变更由 bind:saved 或 store 的宠物变更通知触发刷新,
|
|
// 不必每次关弹层(哪怕只是看了眼 AI)都全量重拉一遍
|
|
this.setData({ sheetShow: false });
|
|
},
|
|
onSheetSave() {
|
|
this.loadTasks();
|
|
this.loadSummary();
|
|
},
|
|
onAddPet() {
|
|
this.openSheetType('addPet');
|
|
},
|
|
onEditPet() {
|
|
this.openSheetType('editPet');
|
|
},
|
|
onFab() {
|
|
this.openSheetType('ai');
|
|
},
|
|
goProfile() {
|
|
wx.navigateTo({ url: '/pages/profile/profile' });
|
|
},
|
|
goLearn() {
|
|
wx.navigateTo({ url: '/pages/learn/learn' });
|
|
},
|
|
// 首页那张卡片直接打开这篇文章正文
|
|
openFirstArticle() {
|
|
const a = this.data.firstArticle;
|
|
if (a && a.id) wx.navigateTo({ url: `/pages/article/article?id=${a.id}` });
|
|
else this.goLearn();
|
|
},
|
|
goRecord() {
|
|
wx.switchTab({ url: '/pages/record/record' });
|
|
},
|
|
onShareAppMessage() {
|
|
return { title: '肉垫计划 · 每天照顾好毛孩子', path: '/pages/home/home' };
|
|
},
|
|
onShareTimeline() {
|
|
return { title: '肉垫计划 · 每天照顾好毛孩子' };
|
|
},
|
|
});
|