d9d7e128ff
## 引导页三个逻辑漏洞 1. **阶段和生日脱节**:用户填了 3 个月的生日,却能手选「成年期」, 而阶段决定疫苗针次、驱虫周期和每天要做的事,选错整套计划都是偏的。 现在按生日自动推断(猫狗同:12 月龄成年、7 岁进老年),仍可手改, 但改成矛盾值时会弹框说明代价;服务端 NormalizeStage 兜底。 2. **`goals` 是假选择**:6 个勾选框收上来只存进 pets.goals,一处没用过。 现在真正生效:体重提醒、月度报告按勾选生成,勾了「想控制养宠开销」 会多一条记账任务。疫苗和驱虫不给关——狂犬是法定要求,漏驱虫真会出事。 3. **「刚到家 0-30 天」和年龄阶段互斥**:挤在一个四选一里,刚接回家的 成年猫只能二选一。现在拆开:stage 只存年龄阶段,新增 pets.arrived_at, 到家不满 30 天时把安置期那套任务和计划叠加上去(同标题去重)。 顺带:生日/到家日期不能选未来(前后端各拦一道)、体重范围校验。 ## 疫苗提醒改为按月龄算 原来不管多大都排「14 天后首针」,一只 8 月龄才接回家的猫会被安排去打首免。 现在 dueDaysFor 按真实月龄推:<2 月龄等到 8 周龄、2-5 月龄间隔 21 天、 5-12 月龄两周内补齐、成年走年度加强。标题也跟着改写,否则用户照着 「年度疫苗加强」去打错针。 年度疫苗不再编一个假的到期日(原来写 30 天后),改用频率描述。 ## 8 套模板按临床共识重配 依据 WSAVA 疫苗指南、ESCCAP/CAPC 驱虫建议、AAHA 生命阶段指南整理: 猫三联 3 针间隔 3-4 周末针不早于 16 周龄、犬联苗 2-3 针 + 3 月龄狂犬、 体内驱虫 6 月龄前每月一次之后每 3 个月、幼崽 3-6 月龄每天 3-4 餐、 成年犬每天 2 次共 30-60 分钟遛狗、老年建议半年一次体检。 每套从 11 条扩到 10-14 条,描述写成能直接照做的具体动作。 后台新增「恢复默认」:启动 seed 改为只补缺失、不覆盖已有(管理员可能 改过),内置模板更新后必须从后台显式推一次。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
175 lines
5.8 KiB
JavaScript
175 lines
5.8 KiB
JavaScript
const store = require('../../utils/store.js');
|
|
const api = require('../../utils/api.js');
|
|
|
|
const GENDERS = ['男孩', '女孩', '不确定'];
|
|
|
|
// 与后端 service.AgeStages 一致:「刚到家」不是年龄阶段,它由到家日期单独决定
|
|
const STAGE_OPTIONS = [
|
|
{ key: '幼年期', desc: '疫苗针次、驱虫、体重增长、社会化训练' },
|
|
{ key: '成年期', desc: '体重控制、口腔、日常健康管理' },
|
|
{ key: '老年期', desc: '慢病监测、半年体检、用药与护理' },
|
|
];
|
|
|
|
function today() {
|
|
const d = new Date();
|
|
const p = (n) => (n < 10 ? '0' + n : '' + n);
|
|
return d.getFullYear() + '-' + p(d.getMonth() + 1) + '-' + p(d.getDate());
|
|
}
|
|
|
|
function monthsSince(dateStr) {
|
|
const b = new Date(dateStr);
|
|
const now = new Date();
|
|
let m = (now.getFullYear() - b.getFullYear()) * 12 + (now.getMonth() - b.getMonth());
|
|
if (now.getDate() < b.getDate()) m--;
|
|
return m < 0 ? 0 : m;
|
|
}
|
|
|
|
// 与后端 StageFromBirthday 同一套规则:12 月龄成年、7 岁进入老年
|
|
function stageFromBirthday(birthday) {
|
|
if (!birthday) return '';
|
|
const m = monthsSince(birthday);
|
|
if (m < 12) return '幼年期';
|
|
if (m >= 84) return '老年期';
|
|
return '成年期';
|
|
}
|
|
|
|
function ageFromBirthday(bdayStr) {
|
|
if (!bdayStr) return '';
|
|
const b = new Date(bdayStr);
|
|
const now = new Date();
|
|
let months = (now.getFullYear() - b.getFullYear()) * 12 + (now.getMonth() - b.getMonth());
|
|
if (months < 0) months = 0;
|
|
return months < 24 ? months + '个月' : Math.floor(months / 12) + '岁';
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
step: 0,
|
|
petType: '猫猫',
|
|
petEmoji: '🐱',
|
|
petName: '',
|
|
birthday: '',
|
|
genderIdx: 0,
|
|
weight: '',
|
|
arrivedAt: '',
|
|
today: '',
|
|
stage: '',
|
|
stageAuto: '',
|
|
stageOptions: STAGE_OPTIONS,
|
|
goals: [
|
|
{ label: '不知道每天该做什么', on: true },
|
|
{ label: '疫苗驱虫怕忘记', on: true },
|
|
{ label: '想记录体重和健康', on: false },
|
|
{ label: '担心生病不会判断', on: false },
|
|
{ label: '想控制养宠开销', on: false },
|
|
{ label: '想生成成长报告', on: false },
|
|
],
|
|
submitting: false,
|
|
},
|
|
onLoad() {
|
|
this.setData({ today: today(), arrivedAt: today() });
|
|
// 已有宠物则直接进首页
|
|
store
|
|
.ready()
|
|
.then(() => {
|
|
if (store.getPets().length > 0) {
|
|
wx.switchTab({ url: '/pages/home/home' });
|
|
}
|
|
})
|
|
.catch(() => {});
|
|
},
|
|
nextStep() {
|
|
this.setData({ step: Math.min(this.data.step + 1, 5) });
|
|
},
|
|
choosePet(e) {
|
|
const { type, emoji } = e.currentTarget.dataset;
|
|
this.setData({ petType: type, petEmoji: emoji });
|
|
},
|
|
onName(e) { this.setData({ petName: e.detail.value }); },
|
|
onWeight(e) { this.setData({ weight: e.detail.value }); },
|
|
onBirthday(e) {
|
|
const birthday = e.detail.value;
|
|
const auto = stageFromBirthday(birthday);
|
|
const patch = { birthday, stageAuto: auto };
|
|
// 用户手动选过就尊重他的选择,否则跟着生日走
|
|
if (!this._stageTouched) patch.stage = auto;
|
|
this.setData(patch);
|
|
},
|
|
onArrived(e) { this.setData({ arrivedAt: e.detail.value }); },
|
|
selectGender(e) { this.setData({ genderIdx: Number(e.currentTarget.dataset.index) }); },
|
|
// 先随便看看:不建数据,直接进首页浏览(无宠物时首页显示空态)
|
|
skipBrowse() { wx.switchTab({ url: '/pages/home/home' }); },
|
|
savePetNext() {
|
|
if (!(this.data.petName || '').trim()) {
|
|
wx.showToast({ title: '给它起个名字吧', icon: 'none' });
|
|
return;
|
|
}
|
|
const w = parseFloat(this.data.weight);
|
|
// 体重会进趋势图和用药剂量参考,乱填不如不填
|
|
if (this.data.weight && !(w > 0 && w < 120)) {
|
|
wx.showToast({ title: '体重看起来不太对', icon: 'none' });
|
|
return;
|
|
}
|
|
if (!this.data.birthday) {
|
|
wx.showToast({ title: '生日决定疫苗和驱虫节奏,估个大概也行', icon: 'none' });
|
|
return;
|
|
}
|
|
this.nextStep();
|
|
},
|
|
chooseStage(e) {
|
|
const stage = e.currentTarget.dataset.stage;
|
|
const auto = this.data.stageAuto;
|
|
// 改成和生日矛盾的阶段是允许的(领养估的生日可能不准,大型犬发育也慢),
|
|
// 但要先说清楚代价:阶段决定疫苗针次怎么排,选错会漏针
|
|
if (auto && stage !== auto) {
|
|
wx.showModal({
|
|
title: '和生日对不上',
|
|
content: `按生日算它应该在「${auto}」。改成「${stage}」的话,疫苗针次和驱虫周期会按${stage}安排。确定要改吗?`,
|
|
confirmText: '确定改',
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
this._stageTouched = true;
|
|
this.setData({ stage });
|
|
}
|
|
},
|
|
});
|
|
return;
|
|
}
|
|
this._stageTouched = true;
|
|
this.setData({ stage });
|
|
},
|
|
toggleGoal(e) {
|
|
const i = e.currentTarget.dataset.index;
|
|
this.setData({ [`goals[${i}].on`]: !this.data.goals[i].on });
|
|
},
|
|
async submit() {
|
|
if (this.data.submitting) return;
|
|
this.setData({ submitting: true });
|
|
wx.showLoading({ title: '生成计划中...' });
|
|
const body = {
|
|
name: (this.data.petName || '').trim(),
|
|
emoji: this.data.petEmoji,
|
|
type: this.data.petType,
|
|
gender: GENDERS[this.data.genderIdx],
|
|
birthday: this.data.birthday,
|
|
arrived_at: this.data.arrivedAt,
|
|
weight: this.data.weight,
|
|
stage: this.data.stage || this.data.stageAuto,
|
|
age: ageFromBirthday(this.data.birthday),
|
|
goals: this.data.goals.filter((g) => g.on).map((g) => g.label),
|
|
};
|
|
try {
|
|
await store.ready();
|
|
await api.onboarding(body);
|
|
await store.loadPets();
|
|
wx.hideLoading();
|
|
wx.switchTab({ url: '/pages/home/home' });
|
|
} catch (e) {
|
|
wx.hideLoading();
|
|
wx.showToast({ title: e.message || '创建失败', icon: 'none' });
|
|
this.setData({ submitting: false });
|
|
}
|
|
},
|
|
finishOnboard() { this.submit(); },
|
|
});
|