75765b3ee8
之前用户记几十条数据只换回一条折线,数据没有回到他身上,所以不记也不亏。 新增 GET /api/pets/:id/insights,只看最近 90 天(更早的对「现在怎么样」 没参考价值,还会让关联分析找出一堆巧合),产出最多 6 条按严重度排序的结论: 1. 体重趋势 —— 单次数字没意义,跨度 ≥14 天的连续同向变化才算。 跌 10% 报警建议血检,涨 15% 提醒控制,跌 5% 持续观察 2. 换粮 → 软便的时间关联 —— 找异常排便前 72 小时内的饮食记录。 这是用户最看不出来的一类:两条记录隔两三天、分属不同类型,翻时间轴翻不出来 3. 异常扎堆 —— 两周内 ≥2 次异常。偶发一次和反复出现完全不是一回事 4. 逾期提醒 —— 设了不看等于没设 5. 同龄体重对比 —— 同物种、月龄差 2 个月内其它宠物的中位数。 样本 <8 只直接不显示:拿 3 只算出来的「中位数」比不给还糟 6. 记录习惯 —— 数据不足时negative空手而归,给一句「再记几条就能看出规律」 每条都带 evidence(支撑它的具体数据)和 action(点击直接跳到对应记录入口), 结论不能只是断言。文案沿用 AI 助手同一套护栏:不做诊断,异常一律写清 什么时候必须就医。 前端落在记录页体重趋势卡上方,三档配色区分严重度。 实测(造 4 条体重下滑 + 换粮 + 软便 + 两次异常): 🔴 体重掉了 13%,建议就医检查(39 天内 4.50→3.90kg) 🔴 两周内记了 2 次异常 🟠 这次「软便」之前 2 天有过饮食变化(7月20日换粮 → 7月22日软便) Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
132 lines
6.0 KiB
JavaScript
132 lines
6.0 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' }),
|
||
|
||
// 宠物
|
||
getPets: () => request({ url: '/api/pets' }),
|
||
homeSummary: (id) => request({ url: `/api/pets/${id}/home-summary` }),
|
||
getPet: (id) => request({ url: `/api/pets/${id}` }),
|
||
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}` }),
|
||
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' }),
|
||
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' }),
|
||
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) =>
|
||
request({ url: `/api/posts/${id}/comments`, method: 'POST', data: { content } }),
|
||
|
||
// 文章
|
||
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;
|