Files
sundynix-pets/pets-fe/pages/home/home.js
T
Blizzard 28b95a6468 feat: 每日任务顺延与增删改 + 计划到期续期 + 靠谱养护模板 + 图片URL修复
后端
- 每日任务惰性生成:某天为空则从最近一天顺延复制(done 重置),首天用模板
- 任务增删改 API(POST /pets/:id/tasks、PUT/DELETE /tasks/:id),改动自动顺延
- 30 天计划到期自动归档并按当前阶段生成新一轮
- 内置养护模板重写为兽医常识向:狗每阶段含遛狗/牵引/狂犬,猫含猫砂/梳毛/饮水
- 文件 URL 改为按 object_name + 当前配置动态拼接,换 IP/域名不再有旧地址

小程序
- 首页今日任务加「管理」入口:增删改任务弹层
- 记录时间轴显示照片缩略图(可全屏预览)+ 拍照成功提示
- 社区发布条精简为单个「发布图文」按钮,去掉头像/输入框误触

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 10:06:13 +08:00

159 lines
3.8 KiB
JavaScript

const store = require('../../utils/store.js');
const api = require('../../utils/api.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 });
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;
const date = this.data.selectedDate || this.data.todayDate;
api
.getTasks(id, date)
.then((tasks) => this.setData({ tasks: (tasks || []).map(mapTask) }))
.catch(() => {});
},
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() {
this.setData({ sheetShow: false });
this.loadAll();
},
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' });
},
goRecord() {
wx.switchTab({ url: '/pages/record/record' });
},
});