609f7d06cf
- pets-fe: 微信原生小程序(首页/计划/记录/报告/社区/引导), 服务端驱动、无假数据;弹层改用 scroll-view,打开时隐藏自定义 tabBar - pets-be: Gin + GORM(MySQL, sundynix_ 前缀) + MinIO,统一响应/分页, 微信 code2session 登录,provider-neutral AI(DeepSeek),go:embed React 后台 - 修复:分段选择类型不匹配(字符串 vs 数字)导致选不中 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
131 lines
3.0 KiB
JavaScript
131 lines
3.0 KiB
JavaScript
const store = require('../../utils/store.js');
|
|
const api = require('../../utils/api.js');
|
|
|
|
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,
|
|
sheetShow: false,
|
|
sheetType: '',
|
|
},
|
|
onLoad() {
|
|
this._unsub = store.subscribe((pet) => {
|
|
this.setData({ pet });
|
|
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.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: { 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(() => {});
|
|
},
|
|
loadTasks() {
|
|
const id = store.currentPetId();
|
|
if (!id) return;
|
|
api
|
|
.getTasks(id)
|
|
.then((tasks) => this.setData({ tasks: (tasks || []).map(mapTask) }))
|
|
.catch(() => {});
|
|
},
|
|
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() {
|
|
this.setData({ sheetShow: false });
|
|
this.loadAll();
|
|
},
|
|
onSheetSave() {},
|
|
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' });
|
|
},
|
|
goRecord() {
|
|
wx.switchTab({ url: '/pages/record/record' });
|
|
},
|
|
});
|