迁移快捷好友操作事件

This commit is contained in:
2026-04-25 10:20:21 +08:00
parent a847dad00a
commit 95484681c5
4 changed files with 79 additions and 59 deletions
+4 -2
View File
@@ -6,7 +6,7 @@ export { bindGlobalDialogControls } from "./chat-room/dialog.js";
export { bindDailySignInControls } from "./chat-room/daily-sign-in.js";
export { applyFontSize, bindChatFontSizeControl, CHAT_FONT_SIZE_STORAGE_KEY, restoreChatFontSize } from "./chat-room/font-size.js";
export { bindChatImageUploadControl } from "./chat-room/image-upload.js";
export { bindFriendPanelControls, closeFriendPanel, friendSearch, loadFriends, openFriendPanel } from "./chat-room/friend-panel.js";
export { bindFriendPanelControls, closeFriendPanel, friendSearch, loadFriends, openFriendPanel, quickFriendAction } from "./chat-room/friend-panel.js";
export { closeChatImageLightbox, initChatImageLightboxEvents, openChatImageLightbox } from "./chat-room/lightbox.js";
export {
bindMobileDrawerControls,
@@ -75,7 +75,7 @@ import { bindGlobalDialogControls } from "./chat-room/dialog.js";
import { bindDailySignInControls } from "./chat-room/daily-sign-in.js";
import { applyFontSize, bindChatFontSizeControl, CHAT_FONT_SIZE_STORAGE_KEY, restoreChatFontSize } from "./chat-room/font-size.js";
import { bindChatImageUploadControl } from "./chat-room/image-upload.js";
import { bindFriendPanelControls, closeFriendPanel, friendSearch, loadFriends, openFriendPanel } from "./chat-room/friend-panel.js";
import { bindFriendPanelControls, closeFriendPanel, friendSearch, loadFriends, openFriendPanel, quickFriendAction } from "./chat-room/friend-panel.js";
import { closeChatImageLightbox, initChatImageLightboxEvents, openChatImageLightbox } from "./chat-room/lightbox.js";
import {
bindMobileDrawerControls,
@@ -154,6 +154,7 @@ if (typeof window !== "undefined") {
friendSearch,
loadFriends,
openFriendPanel,
quickFriendAction,
bindMobileDrawerControls,
closeMobileDrawer,
loadMobileRoomList,
@@ -220,6 +221,7 @@ if (typeof window !== "undefined") {
window.closeFriendPanel = closeFriendPanel;
window.friendSearch = friendSearch;
window.openFriendPanel = openFriendPanel;
window.quickFriendAction = quickFriendAction;
window.closeMobileDrawer = closeMobileDrawer;
window.loadMobileRoomList = loadMobileRoomList;
window.openMobileDrawer = openMobileDrawer;
+65
View File
@@ -286,6 +286,59 @@ async function friendAction(action, username, button) {
}
}
/**
* 聊天消息和横幅内的快捷好友操作。
*
* @param {"add"|"remove"|string} action 操作类型
* @param {string} username 目标用户名
* @param {HTMLElement} element 触发元素
* @returns {Promise<void>}
*/
export async function quickFriendAction(action, username, element) {
if (!["add", "remove"].includes(action) || !username || !(element instanceof HTMLElement)) {
return;
}
if (element.dataset.done) {
return;
}
element.dataset.done = "1";
element.textContent = "处理中…";
element.style.pointerEvents = "none";
try {
// 消息内链接来自后端 HTML,用户名进入 path 前仍必须编码。
const response = await fetch(`/friend/${encodeURIComponent(username)}/${action}`, {
method: action === "add" ? "POST" : "DELETE",
headers: {
"Content-Type": "application/json",
"X-CSRF-TOKEN": csrf(),
Accept: "application/json",
},
body: JSON.stringify({
room_id: roomId(),
}),
});
const data = await response.json();
if (data.status === "success") {
element.textContent = action === "add" ? "✅ 已回加" : "✅ 已移除";
element.style.color = "#16a34a";
element.style.textDecoration = "none";
return;
}
element.textContent = `${data.message || "操作失败"}`;
element.style.color = "#cc4444";
} catch (error) {
element.textContent = "❌ 网络错误";
element.style.color = "#cc4444";
delete element.dataset.done;
element.style.pointerEvents = "";
}
}
/**
* 通过搜索框按用户名添加好友,具体校验仍交给后端。
*
@@ -370,6 +423,18 @@ export function bindFriendPanelControls() {
return;
}
const quickAction = event.target.closest("[data-quick-friend-action]");
if (quickAction) {
event.preventDefault();
// 后端系统消息只输出 data 属性,具体请求仍统一走模块方法。
void quickFriendAction(
quickAction.getAttribute("data-quick-friend-action") || "",
quickAction.getAttribute("data-quick-friend-username") || "",
quickAction,
);
return;
}
const panel = event.target.closest("#friend-panel");
// 只在点击遮罩本身时关闭,避免点击内容区误关。
if (panel && event.target === panel) {