c4c0c2b7cd
## 你问的「养了半年会不会自动切换」
原来的答案是「都不会,而且比这更糟」:
age「2个月」 建档时算好存进库,养半年后首页还显示 2个月
stage「幼年期」同上,一年后还是幼年期
养护方案 按 stage 挑的,所以永远给幼猫那套
NormalizeStage 只在建档那一次调用(handler/pet.go:51),之后就是两个死字符串。
现在:
age 改成计算字段(gorm:"-"),读取时现算。它没有手改需求,存下来只会过期。
填在 ownedPet 这个收口里,不是逐个函数补——那样漏一个就返回空年龄。
stage 保留存储(有正当的手改需求:领养的猫生日是估的,主人按体型判断更准),
但加了漂移检测 GET /pets/:id/stage-drift。
**系统负责发现,用户负责决定。** 不自动改的三个理由:
1. 用户可能手改过
2. 已经排好的这个月计划不该被推翻(套用是「往里加」不是「替换」)
3. 偷偷换会让用户莫名发现计划变了,找不到原因
## 猫狗不能共用阈值 —— 这是这次最实质的发现
原来一刀切「<12月幼年,>=7岁老年」,对猫勉强,对狗两头都错:
吉娃娃 8 个月已性成熟、可换成犬粮;大丹犬 8 个月还在长骨头,
这时喂成犬粮(钙磷比和热量密度都不同)会加重关节负担。
吉娃娃 7 岁还在成年期(8 岁才进中年,能活 13-16 年);
大丹犬 7 岁已是老年(平均寿命 7-10 年)。
所以狗的阶段判定要看体型。体型不能靠当前体重(幼犬体重说明不了成年后多大),
只能靠品种 —— breeds 表加 size_class,45 个犬种逐个标了。
实测:同样 10 个月大 → 吉娃娃青年期、柴犬幼年期、金毛幼年期、大丹犬幼年期;
同样 7 岁 → 吉娃娃成年期、大丹犬老年期。
## 阶段 3 段 → 5 段
幼年期 / 青年期 / 成年期 / 中年期 / 老年期。
标签做成物种中立(不分幼猫期/幼犬期)——物种本来是另一个字段,
分开会让 (species,stage) 的组合和方案配置都翻倍。
「刚到家 0-30 天」不进这五段:它和年龄正交(刚接回家的成年猫两套都需要),
代码里本来就是叠加处理的。
猫按 AAFP/AAHA 猫生命阶段指南那套,在成年里细分出中年。
狗五段的阈值按体型走四组,越大的犬种生长期越长、老得越早。
## 划分标准和判定用同一张表
StageRules 那张表既驱动判定,也直接吐给前端当依据展示
(GET /api/life-stages?species=dog&size=giant)。
不是「代码里判定 + 文档里写标准」——后者一定会漂:改了阈值忘了改文档,
用户看到的说明和实际行为对不上,比没有说明更糟。
建档第三步的阶段选择器现在会显示「0-18 月 · 巨型犬生长期最长,18 个月还在长;
这阶段最忌过度补钙」,还会标出「巨型犬(成年 >40kg)——阶段阈值按体型算」。
前端和后台的 STAGES 硬编码列表都删了,改成读后端。
## 10 套方案,内容按各阶段真实重点配
不是把同一份换个名字:
幼年期 疫苗序列 + 驱虫 + 社会化,大型犬还要控制生长速度
青年期 绝育窗口 + 换成年粮
成年期 维持期,把基线数据记下来
中年期 年度血检开始有意义,体重和牙结石重点看
老年期 体检半年一次;猫记饮水量(多饮多尿是慢性肾病最典型的早期表现)、
狗记精神食欲,都设成「重要」
老的 8 套(按 3 段配的)改名停用不删:万一新内容有问题还能翻回去看;
而且已套用出去的计划早就和模板断开了,停用不影响任何用户。
## 验证(预生产库实跑)
体型判定 10 个月 / 7 岁两组对照,四个体型都落在预期阶段
age '13个月' 现算,不入库
漂移检测 手改成幼年期后 drifted=true,连带给出新阶段的依据和重点
划分标准 猫 5 段、狗 4 个体型各 5 段,老年起点 7/8/10/11 岁
全链路 3 个月大丹犬建档 → 幼年期 → 0 任务 + 4 条必要提醒 →
挑「幼犬标准照护」→ 套用下月 104 条节点,
周四那天正确出现每周项(检查爪垫和耳朵)
迁移 45 个犬种体型回填;现有 5 只宠物阶段重算后无变化(原本都对)
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
159 lines
7.8 KiB
JavaScript
159 lines
7.8 KiB
JavaScript
const { request, uploadFile, setToken, setRefreshToken } = require('./request.js');
|
||
|
||
// 登录成功后统一落盘 access + refresh 两个令牌
|
||
function saveTokens(data) {
|
||
setToken(data.token);
|
||
setRefreshToken(data.refresh_token);
|
||
return data;
|
||
}
|
||
|
||
// wx.login 取 code
|
||
function wxLogin() {
|
||
return new Promise((resolve, reject) => {
|
||
wx.login({
|
||
success: (r) => (r.code ? resolve(r.code) : reject(new Error('wx.login 未返回 code'))),
|
||
fail: reject,
|
||
});
|
||
});
|
||
}
|
||
|
||
// 登录:优先微信 code2session,失败回退开发态 Mock 登录(后端 dev_login=true)
|
||
async function login() {
|
||
try {
|
||
const code = await wxLogin();
|
||
// noAuthRetry:登录接口失败时也会返回 40100,不能再触发「续期→重新登录」,否则会自己套自己
|
||
const data = await request({ url: '/api/auth/wechat', method: 'POST', data: { code }, noAuthRetry: true });
|
||
return saveTokens(data);
|
||
} catch (e) {
|
||
console.warn('[api] 微信登录失败,回退 Mock 登录:', e && e.message);
|
||
const data = await request({
|
||
url: '/api/auth/login',
|
||
method: 'POST',
|
||
data: { nickname: '开发用户' },
|
||
noAuthRetry: true,
|
||
});
|
||
return saveTokens(data);
|
||
}
|
||
}
|
||
|
||
const api = {
|
||
login,
|
||
uploadFile,
|
||
|
||
// 用户
|
||
getProfile: () => request({ url: '/api/user/profile' }),
|
||
updateProfile: (body) => request({ url: '/api/user/profile', method: 'PUT', data: body }),
|
||
|
||
// 用户
|
||
userSummary: () => request({ url: '/api/user/summary' }),
|
||
// 记录类型(后台可配)。分组好的,前端直接渲染
|
||
recordTypes: () => request({ url: '/api/record-types' }),
|
||
updateDecoration: (body) => request({ url: '/api/user/decoration', method: 'PUT', data: body }),
|
||
|
||
// 宠物
|
||
getPets: () => request({ url: '/api/pets' }),
|
||
homeSummary: (id) => request({ url: `/api/pets/${id}/home-summary` }),
|
||
getPet: (id) => request({ url: `/api/pets/${id}` }),
|
||
// 生命阶段的划分标准。判定和展示用同一份数据 —— 不另写一份说明文案,
|
||
// 否则改了阈值忘了改文案,用户看到的依据和实际行为对不上
|
||
lifeStages: (species, size) =>
|
||
request({ url: `/api/life-stages?species=${species}&size=${size || ''}` }),
|
||
petStageDrift: (petId) => request({ url: `/api/pets/${petId}/stage-drift` }),
|
||
|
||
// 品种(后台可配),按首字母分组
|
||
breeds: (species) => request({ url: '/api/breeds?species=' + species }),
|
||
createPet: (body) => request({ url: '/api/pets', method: 'POST', data: body }),
|
||
updatePet: (id, body) => request({ url: `/api/pets/${id}`, method: 'PUT', data: body }),
|
||
deletePet: (id) => request({ url: `/api/pets/${id}`, method: 'DELETE' }),
|
||
onboarding: (body) => request({ url: '/api/onboarding', method: 'POST', data: body }),
|
||
|
||
// 记录
|
||
getRecords: (id, { type, page, pageSize } = {}) => {
|
||
const qs = [];
|
||
if (type) qs.push(`type=${type}`);
|
||
if (page) qs.push(`page=${page}`);
|
||
if (pageSize) qs.push(`page_size=${pageSize}`);
|
||
return request({ url: `/api/pets/${id}/records${qs.length ? '?' + qs.join('&') : ''}` });
|
||
},
|
||
createRecord: (id, body) => request({ url: `/api/pets/${id}/records`, method: 'POST', data: body }),
|
||
deleteRecord: (recordId) => request({ url: `/api/records/${recordId}`, method: 'DELETE' }),
|
||
petInsights: (id) => request({ url: `/api/pets/${id}/insights` }),
|
||
weightTrend: (id) => request({ url: `/api/pets/${id}/records/weight-trend` }),
|
||
|
||
// 任务
|
||
getTasks: (id, date) => request({ url: `/api/pets/${id}/tasks${date ? `?date=${date}` : ''}` }),
|
||
createTask: (id, body) => request({ url: `/api/pets/${id}/tasks`, method: 'POST', data: body }),
|
||
updateTask: (taskId, body) => request({ url: `/api/tasks/${taskId}`, method: 'PUT', data: body }),
|
||
deleteTask: (taskId) => request({ url: `/api/tasks/${taskId}`, method: 'DELETE' }),
|
||
toggleTask: (taskId) => request({ url: `/api/tasks/${taskId}/toggle`, method: 'POST' }),
|
||
completeAllTasks: (id) => request({ url: `/api/pets/${id}/tasks/complete-all`, method: 'POST' }),
|
||
|
||
// 计划
|
||
getPlan: (id) => request({ url: `/api/pets/${id}/plan` }),
|
||
planCalendar: (id, month) =>
|
||
request({ url: `/api/pets/${id}/plan/calendar${month ? `?month=${month}` : ''}` }),
|
||
dayPlan: (id, date) => request({ url: `/api/pets/${id}/day-plan?date=${date}` }),
|
||
// AI 相关:用户端入口已全部下掉,后端 10 个路由和配额配置都还在。
|
||
// 想开回来把这几行解注释、再把入口接上就行,不用重写。
|
||
// createAIPlan: (id, input) => request({ url: `/api/pets/${id}/ai-plan`, method: 'POST', data: { input } }),
|
||
// applyAIPlan: (planId) => request({ url: `/api/ai-plan/${planId}/apply`, method: 'POST' }),
|
||
// 用户自己管理计划节点
|
||
planMonthStatus: (petId) => request({ url: `/api/pets/${petId}/plan/month-status` }),
|
||
addPlanTask: (petId, body) => request({ url: `/api/pets/${petId}/plan-tasks`, method: 'POST', data: body }),
|
||
updatePlanTask: (taskId, body) => request({ url: `/api/plan-tasks/${taskId}`, method: 'PUT', data: body }),
|
||
deletePlanTask: (taskId) => request({ url: `/api/plan-tasks/${taskId}`, method: 'DELETE' }),
|
||
togglePlanTask: (taskId) => request({ url: `/api/plan-tasks/${taskId}/toggle`, method: 'POST' }),
|
||
|
||
// 提醒
|
||
getReminders: (id) => request({ url: `/api/pets/${id}/reminders` }),
|
||
createReminder: (id, body) => request({ url: `/api/pets/${id}/reminders`, method: 'POST', data: body }),
|
||
updateReminder: (remId, body) => request({ url: `/api/reminders/${remId}`, method: 'PUT', data: body }),
|
||
deleteReminder: (remId) => request({ url: `/api/reminders/${remId}`, method: 'DELETE' }),
|
||
|
||
// 报告
|
||
weeklyReport: (id) => request({ url: `/api/pets/${id}/report/weekly` }),
|
||
getBill: (id, period) => request({ url: `/api/pets/${id}/bill?period=${period || 'month'}` }),
|
||
healthSummary: (id) => request({ url: `/api/pets/${id}/health-summary` }),
|
||
getPoster: (id) => request({ url: `/api/pets/${id}/poster` }),
|
||
|
||
// 社区
|
||
listPosts: (tab, page) =>
|
||
request({ url: `/api/posts?tab=${encodeURIComponent(tab || '')}&page=${page || 1}&page_size=10` }),
|
||
createPost: (body) => request({ url: '/api/posts', method: 'POST', data: body }),
|
||
likePost: (id) => request({ url: `/api/posts/${id}/like`, method: 'POST' }),
|
||
userCard: (userId) => request({ url: `/api/users/${userId}/profile` }),
|
||
userPosts: (userId, page) => request({ url: `/api/users/${userId}/posts?page=${page || 1}&page_size=10` }),
|
||
relations: (userId, kind, page) =>
|
||
request({ url: `/api/users/${userId}/relations?kind=${kind}&page=${page || 1}&page_size=20` }),
|
||
followUser: (userId) => request({ url: `/api/users/${userId}/follow`, method: 'POST' }),
|
||
unfollowUser: (userId) => request({ url: `/api/users/${userId}/follow`, method: 'DELETE' }),
|
||
deletePost: (id) => request({ url: `/api/posts/${id}`, method: 'DELETE' }),
|
||
deleteComment: (id) => request({ url: `/api/comments/${id}`, method: 'DELETE' }),
|
||
listComments: (id) => request({ url: `/api/posts/${id}/comments` }),
|
||
createComment: (id, content, images, parentId) =>
|
||
request({
|
||
url: `/api/posts/${id}/comments`,
|
||
method: 'POST',
|
||
data: { content, images: images || [], parent_id: parentId || '' },
|
||
}),
|
||
listReplies: (commentId) => request({ url: `/api/comments/${commentId}/replies` }),
|
||
|
||
// 文章
|
||
listArticles: () => request({ url: '/api/articles' }),
|
||
submitFeedback: (body) => request({ url: '/api/feedback', method: 'POST', data: body }),
|
||
getArticle: (id) => request({ url: `/api/articles/${id}` }),
|
||
|
||
// Pro
|
||
getPro: () => request({ url: '/api/pro' }),
|
||
activatePro: () => request({ url: '/api/pro/activate', method: 'POST' }),
|
||
|
||
// AI
|
||
// aiMessages: (session, limit) =>
|
||
// request({ url: `/api/ai/messages?session=${session || ''}&limit=${limit || 30}` }),
|
||
// aiChat: (body) => request({ url: '/api/ai/chat', method: 'POST', data: body }),
|
||
// assessSymptom: (id, body) =>
|
||
// request({ url: `/api/pets/${id}/ai/assess-symptom`, method: 'POST', data: body }),
|
||
};
|
||
|
||
module.exports = api;
|