fix: 修复关注按钮相关问题

- 食谱详情页: 修复 applyDefaultData 中未定义变量 id 的问题
- 帖子详情页: 优化 toggleFollow 方法,提前校验 author.id,兼容多种后端字段
- 为帖子详情页已关注状态添加灰色样式
This commit is contained in:
msh-agent
2026-03-09 18:56:53 +08:00
parent b516089c4f
commit c1857ce852
14 changed files with 3590 additions and 102 deletions

View File

@@ -244,28 +244,16 @@ export default {
const { setSignIntegral, getFrontUserInfo, getUserInfo } = await import('@/api/user.js');
const { getUserPoints } = await import('@/api/tool.js');
// 子问题 A不得在 API 返回成功前修改 currentPoints避免打卡前积分提前跳变
// 打卡接口GET /api/front/user/sign/integralsetSignIntegral等同于 checkin 签到
// 子问题 A在 API 返回成功前不得修改 currentPoints避免打卡前积分提前跳变(禁止前端本地 +30 等)
// 打卡接口GET /api/front/user/sign/integralsetSignIntegral即本项目的签到/打卡接口
await setSignIntegral();
// 子问题 B仅在打卡成功后用服务端数据更新积分。 GET /api/front/user/info 刷新用户积分,禁止硬编码 +30
let userRes = null;
try {
userRes = await getFrontUserInfo(); // GET /api/front/user/info
} catch (_) {
userRes = await getUserInfo().catch(() => null);
}
if (userRes && userRes.data && (userRes.data.integral != null || userRes.data.points != null)) {
this.currentPoints = userRes.data.integral ?? userRes.data.points ?? 0;
} else {
const pointsRes = await getUserPoints();
if (pointsRes && pointsRes.data) {
const serverPoints = pointsRes.data.totalPoints ?? pointsRes.data.points ?? pointsRes.data.availablePoints ?? 0;
this.currentPoints = serverPoints;
}
// 子问题 B打卡成功后必须用服务端数据更新积分。 GET /api/front/user/info 刷新用户积分,禁止硬编码 +30
const serverPoints = await this._fetchServerPoints(getFrontUserInfo, getUserInfo, getUserPoints);
if (serverPoints != null) {
this.currentPoints = Number(serverPoints);
}
// 积分已从服务端更新后再更新打卡状态并跳转,避免“已打卡”与积分不同步
this.todaySigned = true;
} catch (e) {
const msg = (typeof e === 'string' ? e : (e && (e.message || e.msg))) || '打卡失败';
@@ -279,6 +267,37 @@ export default {
url: '/pages/tool/checkin-publish'
});
},
/**
* 从服务端拉取积分(用于打卡成功后刷新,积分值必须来自服务端,不可前端累加)
* 优先 GET /api/front/user/info失败时回退到 user 或 tool/points/info
*/
async _fetchServerPoints(getFrontUserInfo, getUserInfo, getUserPoints) {
try {
const userRes = await getFrontUserInfo();
if (userRes && userRes.data) {
const d = userRes.data;
const p = d.integral ?? d.points ?? d.totalPoints ?? (d.user && (d.user.integral ?? d.user.points ?? d.user.totalPoints));
if (p != null) return p;
}
} catch (_) {}
try {
const userRes = await getUserInfo();
if (userRes && userRes.data) {
const d = userRes.data;
const p = d.integral ?? d.points ?? d.totalPoints ?? (d.user && (d.user.integral ?? d.user.points ?? d.user.totalPoints));
if (p != null) return p;
}
} catch (_) {}
try {
const pointsRes = await getUserPoints();
if (pointsRes && pointsRes.data) {
const d = pointsRes.data;
const p = d.totalPoints ?? d.points ?? d.availablePoints;
if (p != null) return p;
}
} catch (_) {}
return null;
},
getTodayIndex() {
// 根据连续打卡数据计算当前是第几天
// streakDays 中已有 completed 状态,找第一个未完成的位置即为今天