重构:将聊天室所有原生弹窗替换为全局弹窗,公告增加发送者与时间

- 将设公告、公屏讲话、全员清屏按钮弹窗改为使用 window.chatDialog 全局弹窗
- 所有弹窗改用 .then() 回调注册事件,避免 async/await 行为不一致问题
- 公告内容末尾追加「——发送者 MM-dd HH:mm」,无需新增数据库字段
- 前端编辑公告时自动剥离末尾元信息,用户仅编辑纯内容
- 修复 red-packet-panel.blade.php 中 3 处原生 alert() 残留
- 修复 shop-panel.blade.php 中购买确认 confirm() 原生弹窗残留
This commit is contained in:
2026-03-12 07:33:32 +08:00
parent f1062b34d2
commit b9c703b755
4 changed files with 93 additions and 79 deletions
+44 -41
View File
@@ -986,13 +986,15 @@
// ── 设置房间公告 ─────────────────────────────────────
async function promptAnnouncement() {
const currentText = document.getElementById('announcement-text')?.textContent?.trim() || '';
const newText = prompt('请输入新的房间公告/祝福语:', currentText);
if (newText === null || newText.trim() === '') return;
function promptAnnouncement() {
// 从 marquee 读取当前公告全文,剥离末尾的「——发送者 日期」元信息,仅预填纯内容
const fullText = document.getElementById('announcement-text')?.textContent?.trim() || '';
const pureText = fullText.replace(/ ——\S+ \d{2}-\d{2} \d{2}:\d{2}$/, '').trim();
// 使用全局弹窗替代原生 prompt(),通过 .then() 注册回调确保事件正确触发
window.chatDialog.prompt('请输入新的房间公告/祝福语:', pureText, '设置公告', '#336699').then(newText => {
if (newText === null || newText.trim() === '') return;
try {
const res = await fetch(`/room/${window.chatContext.roomId}/announcement`, {
fetch(`/room/${window.chatContext.roomId}/announcement`, {
method: 'POST',
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute(
@@ -1003,27 +1005,28 @@
body: JSON.stringify({
announcement: newText.trim()
})
}).then(res => res.json()).then(data => {
if (data.status === 'success') {
// 用后端返回的含发送者+时间的完整公告文本更新滚动条
const marquee = document.getElementById('announcement-text');
if (marquee) marquee.textContent = data.announcement;
window.chatDialog.alert('公告已更新!', '提示', '#16a34a');
} else {
window.chatDialog.alert(data.message || '更新失败', '操作失败', '#cc4444');
}
}).catch(e => {
window.chatDialog.alert('设置公告失败:' + e.message, '操作失败', '#cc4444');
});
const data = await res.json();
if (data.status === 'success') {
const marquee = document.getElementById('announcement-text');
if (marquee) marquee.textContent = newText.trim();
window.chatDialog.alert('公告已更新!', '提示', '#16a34a');
} else {
window.chatDialog.alert(data.message || '更新失败', '操作失败', '#cc4444');
}
} catch (e) {
window.chatDialog.alert('设置公告失败:' + e.message, '操作失败', '#cc4444');
}
});
}
// ── 站长公屏讲话 ─────────────────────────────────────
async function promptAnnounceMessage() {
const content = prompt('请输入公屏讲话内容:');
if (!content || !content.trim()) return;
function promptAnnounceMessage() {
// 使用全局弹窗替代原生 prompt(),通过 .then() 注册回调确保事件正确触发
window.chatDialog.prompt('请输入公屏讲话内容:', '', '📢 公屏讲话', '#7c3aed').then(content => {
if (!content || !content.trim()) return;
try {
const res = await fetch('/command/announce', {
fetch('/command/announce', {
method: 'POST',
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute(
@@ -1035,22 +1038,23 @@
content: content.trim(),
room_id: window.chatContext.roomId,
})
}).then(res => res.json()).then(data => {
if (data.status !== 'success') {
window.chatDialog.alert(data.message || '发送失败', '操作失败', '#cc4444');
}
}).catch(e => {
window.chatDialog.alert('发送失败:' + e.message, '操作失败', '#cc4444');
});
const data = await res.json();
if (!res.ok || data.status !== 'success') {
window.chatDialog.alert(data.message || '发送失败', '操作失败', '#cc4444');
}
} catch (e) {
window.chatDialog.alert('发送失败:' + e.message, '操作失败', '#cc4444');
}
});
}
// ── 管理员全员清屏 ─────────────────────────────────────
async function adminClearScreen() {
if (!confirm('确定要清除所有人的聊天记录吗?(悄悄话将保留)')) return;
function adminClearScreen() {
// 使用全局弹窗替代原生 confirm(),通过 .then() 注册回调确保事件正确触发
window.chatDialog.confirm('确定要清除所有人的聊天记录吗?(悄悄话将保留)', '全员清屏', '#dc2626').then(ok => {
if (!ok) return;
try {
const res = await fetch('/command/clear-screen', {
fetch('/command/clear-screen', {
method: 'POST',
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute(
@@ -1061,14 +1065,14 @@
body: JSON.stringify({
room_id: window.chatContext.roomId,
})
}).then(res => res.json()).then(data => {
if (data.status !== 'success') {
window.chatDialog.alert(data.message || '清屏失败', '操作失败', '#cc4444');
}
}).catch(e => {
window.chatDialog.alert('清屏失败:' + e.message, '操作失败', '#cc4444');
});
const data = await res.json();
if (!res.ok || data.status !== 'success') {
window.chatDialog.alert(data.message || '清屏失败', '操作失败', '#cc4444');
}
} catch (e) {
window.chatDialog.alert('清屏失败:' + e.message, '操作失败', '#cc4444');
}
});
}
// ── 本地清屏(仅限自己的屏幕)───────────────────────────
@@ -1273,5 +1277,4 @@
div.textContent = text;
return div.innerHTML;
}
</script>