9adafb1354
后台登录框原来预填了 sundynix/sundynix,等于把凭证写在页面上,去掉。 令牌改为 access(2h) + refresh 两段式: - 新表 sundynix_refresh_tokens,只存 sha256,明文只在签发时返回一次 - 有效期:小程序 30 天、后台 7 天 - 新接口 /api/auth/refresh|logout、/api/admin/refresh|logout 安全约定: - 每次续期都轮换刷新令牌,旧的立即作废 - 作废后 60 秒内再到达算并发重试放行,超过则判定泄露、吊销该账号全部会话 - 主动退出与被连坐吊销的令牌不吃宽限期,否则「吊销全部」形同虚设 - 禁用用户时一并吊销刷新令牌,最多 2 小时彻底失去访问 两端请求层都做了单飞续期:并发请求同时 401 只发一次 refresh, 否则刷新令牌会被并发轮换掉互相打架。小程序续期失败回退 wx.login, 登录/续期请求标 noAuthRetry,避免登录失败(同样返回 40100)触发自我套娃。 顺带修掉 vite 代理仍指向 8080 的遗留(端口早已改 9090)。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
131 lines
5.9 KiB
JavaScript
131 lines
5.9 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' }),
|
||
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;
|