修复红包领取三重问题:①getOnlineUserIds 兼容旧版用户(fallback数据库查询) ②聊天领取按钮用全局Map替代内嵌JSON避免HTML属性破坏 ③doClaim改判 data.ok 而非不存在的 data.status

This commit is contained in:
2026-03-01 19:36:44 +08:00
parent 23fca927d5
commit a37b04aca0
2 changed files with 31 additions and 8 deletions

View File

@@ -364,6 +364,7 @@ class WeddingService
/**
* Redis room:{roomId}:users Hash 获取当前在线用户 ID 列表。
* Hash value 中无 user_id旧版登录用户则用 username 批量查库补齐。
*
* @param int $roomId 房间 ID
* @return array<int>
@@ -373,16 +374,33 @@ class WeddingService
try {
$key = "room:{$roomId}:users";
$users = Redis::hgetall($key);
if (empty($users)) {
return [];
}
$ids = [];
$fallbacks = []; // 需要 fallback 查库的用户名
foreach ($users as $username => $jsonInfo) {
$info = json_decode($jsonInfo, true);
// user_id 由 ChatController::join() 写入 userData
if (isset($info['user_id'])) {
// 新版登录user_id 直接存在 Redis
$ids[] = (int) $info['user_id'];
} else {
// 旧版登录修复前user_id 缺失,记录 username 待批量查库
$fallbacks[] = $username;
}
}
// 对旧用户批量查库补齐 user_id
if (! empty($fallbacks)) {
$dbIds = User::whereIn('username', $fallbacks)
->pluck('id')
->map(fn ($id) => (int) $id)
->all();
$ids = array_merge($ids, $dbIds);
}
return array_values(array_unique($ids));
} catch (\Throwable) {
return [];

View File

@@ -1225,7 +1225,7 @@
})
});
const data = await res.json();
if (data.status === 'success') {
if (data.ok) {
this.claimed = true;
this.claimedAmount = data.amount || 0;
} else {
@@ -1327,14 +1327,18 @@
const amount = detail.total_amount ? Number(detail.total_amount).toLocaleString() : '?';
const ceremonyId = detail.ceremony_id;
// 公屏追加带按钮的系统消息
// 将 detail 存入全局 Map避免 onclick 属性内嵌 JSON 被双引号破坏
if (!window._weddingEnvelopes) window._weddingEnvelopes = {};
window._weddingEnvelopes[ceremonyId] = detail;
// 公屏追加带按钮的系统消息(按钮通过 ceremonyId 引用全局 Map
if (typeof appendSystemMessage === 'function') {
const claimBtn = `<button onclick="(function(){const el=document.getElementById('wedding-envelope-modal');if(el)Alpine.$data(el).open(${JSON.stringify(detail)});})()"
style="display:inline-block; margin-left:10px; padding:3px 12px; border-radius:20px;
background:linear-gradient(135deg,#fcd34d,#f59e0b); color:#92400e;
const claimBtn = `<button onclick="(function(){var d=window._weddingEnvelopes[${ceremonyId}];var el=document.getElementById('wedding-envelope-modal');if(el&&d)Alpine.$data(el).open(d);})()"
style="display:inline-block; margin-left:10px; padding:4px 14px; border-radius:20px;
background:#d97706; color:#fff;
border:none; font-size:12px; font-weight:bold; cursor:pointer;
vertical-align:middle; line-height:1.8;"
title="点击领取婚礼红包">🎁 点击领取红包</button>`;
vertical-align:middle; line-height:1.8; box-shadow:0 2px 8px rgba(0,0,0,.3);"
title="点击领取婚礼红包">🧧 点击领取红包</button>`;
appendSystemMessage(
`${tierIcon} ${groomName} 与 ${brideName} 举办了【${tierName}】!总金额 🪙${amount} 金币,快来抢红包!${claimBtn}`
);
@@ -1343,6 +1347,7 @@
// 同时弹出全屏红包弹窗
const el = document.getElementById('wedding-envelope-modal');
if (el) Alpine.$data(el).open(detail);
});