功能:奖励发放聊天室公告 + 右下角 Toast 通知卡片

后端(AdminCommandController::reward):
- 新增聊天室公开公告消息(系统公告,所有在场用户可见)
- 接收者私信附带 toast_notification 字段触发前端小卡片
- 公告文案:「🪙 [职务人] 向 [目标] 发放了 [N] 枚奖励金币!」

前端:
- 新建 chat/partials/toast-notification.blade.php:
  全局右下角 Toast 组件,window.chatToast.show() API
  支持 title/message/icon/color/duration/action 配置
  多条 Toast 从右下角向上堆叠,独立计时、独立关闭
- chat:message 事件监听中检测 toast_notification 字段,
  自动弹出右下角通知卡片(仅接收方可见)
- showFriendToast 迁移至 window.chatToast.show(),
  删除 80 行旧实现,代码量净减
- frame.blade.php 引入新 partial

DEVELOPMENT.md:
- 新增 §7.9 chatToast 完整文档(API、使用场景、迁移说明)
- 原 chatBanner 章节编号改为 §7.10
This commit is contained in:
2026-03-01 12:15:18 +08:00
parent 2ae3d83349
commit 3d7b86f06d
5 changed files with 275 additions and 96 deletions
+44 -92
View File
@@ -494,6 +494,17 @@
return;
}
appendMessage(msg);
// 若消息携带 toast_notification 字段且当前用户是接收者,弹右下角小卡片
if (msg.toast_notification && msg.to_user === window.chatContext.username) {
const t = msg.toast_notification;
window.chatToast.show({
title: t.title || '通知',
message: t.message || '',
icon: t.icon || '💬',
color: t.color || '#336699',
duration: t.duration ?? 8000,
});
}
});
window.addEventListener('chat:kicked', (e) => {
@@ -673,20 +684,39 @@
.listen('.FriendRemoved', (e) => {
if (e.had_added_back) {
// 之前是互相好友,现在对方删除了我 → 提示可以同步删除
showFriendToast(
` <b>${e.from_username}</b> 已将你从好友列表移除。<br>
<span style="color:#6b7280; font-size:12px;">你的好友列表中仍保留对方,可点击同步移除。</span>`,
'#6b7280', {
window.chatToast.show({
title: '好友通知',
message: `<b>${e.from_username}</b> 已将你从好友列表移除。<br><span style="color:#6b7280; font-size:12px;">你的好友列表中仍保留对方,可点击同步移除。</span>`,
icon: '👥',
color: '#6b7280',
duration: 10000,
action: {
label: `🗑️ 同步移除 ${e.from_username}`,
username: e.from_username,
action: 'remove'
}
);
onClick: async () => {
const url = `/friend/${encodeURIComponent(e.from_username)}/remove`;
const csrf = document.querySelector('meta[name="csrf-token"]')
?.content ?? '';
await fetch(url, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': csrf,
'Accept': 'application/json'
},
body: JSON.stringify({
room_id: window.chatContext?.roomId
}),
});
}
},
});
} else {
showFriendToast(
` <b>${e.from_username}</b> 已将你从他的好友列表移除。`,
'#9ca3af'
);
window.chatToast.show({
title: '好友通知',
message: `<b>${e.from_username}</b> 已将你从他的好友列表移除。`,
icon: '👥',
color: '#9ca3af',
});
}
});
}
@@ -1002,86 +1032,8 @@
}
};
function showFriendToast(html, color = '#16a34a', action = null) {
const toast = document.createElement('div');
toast.style.cssText = `
position: fixed; bottom: 24px; right: 24px; z-index: 999999;
background: #fff; border-left: 4px solid ${color};
border-radius: 8px; padding: 14px 18px; min-width: 260px; max-width: 320px;
box-shadow: 0 8px 32px rgba(0,0,0,.18);
font-size: 13px; color: #374151; line-height: 1.6;
animation: fdSlideIn .3s ease;
`;
// 操作按钮 HTML
let actionHtml = '';
if (action) {
actionHtml = `
<div style="margin-top:10px;">
<button id="friend-toast-btn-${Date.now()}"
style="background:${color}; color:#fff; border:none; border-radius:5px;
padding:5px 12px; font-size:12px; font-weight:bold; cursor:pointer;">
${action.label}
</button>
</div>`;
}
toast.innerHTML = `
<div style="display:flex; justify-content:space-between; align-items:flex-start;">
<div>
<div style="font-weight:bold; margin-bottom:4px; color:${color};">💬 好友通知</div>
<div>${html}</div>
${actionHtml}
</div>
<button onclick="this.closest('div[style]').remove()"
style="background:none; border:none; color:#9ca3af; font-size:18px;
cursor:pointer; line-height:1; margin-left:8px; flex-shrink:0;">×</button>
</div>
`;
document.body.appendChild(toast);
// 绑定操作按钮事件
if (action) {
const btn = toast.querySelector('button[id^="friend-toast-btn"]');
if (btn) {
btn.addEventListener('click', async () => {
btn.disabled = true;
btn.textContent = '处理中…';
try {
const method = action.action === 'add' ? 'POST' : 'DELETE';
const url =
`/friend/${encodeURIComponent(action.username)}/${action.action === 'add' ? 'add' : 'remove'}`;
const csrf = document.querySelector('meta[name="csrf-token"]')?.content ?? '';
const res = await fetch(url, {
method,
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': csrf,
'Accept': 'application/json'
},
body: JSON.stringify({
room_id: window.chatContext?.roomId
}),
});
const data = await res.json();
btn.textContent = data.status === 'success' ? '✅ 已完成' : '❌ 失败';
btn.style.background = data.status === 'success' ? '#16a34a' : '#cc4444';
setTimeout(() => toast.remove(), 2000);
} catch (e) {
btn.textContent = '❌ 网络错误';
}
});
}
}
// 5秒后自动消失
setTimeout(() => {
toast.style.transition = 'opacity .5s';
toast.style.opacity = '0';
setTimeout(() => toast.remove(), 500);
}, 8000);
}
// showFriendToast 已迁移至 window.chatToasttoast-notification.blade.php
// 保留空函数作向后兼容(移除时搜索 showFriendToast 确认无残余调用)
// ── 全屏特效事件监听(烟花/下雨/雷电/下雪)─────────
window.addEventListener('chat:effect', (e) => {