diff --git a/web/frontend/src/pages/AccountsPage.tsx b/web/frontend/src/pages/AccountsPage.tsx index 9241bb9..0a492e8 100644 --- a/web/frontend/src/pages/AccountsPage.tsx +++ b/web/frontend/src/pages/AccountsPage.tsx @@ -1,13 +1,13 @@ -import { useEffect, useState, useCallback } from 'react'; +import { useEffect, useState, useMemo, useCallback } from 'react'; import { useNavigate } from 'react-router-dom'; import { - Table, Button, Modal, Input, Select, Popconfirm, Typography, Tag, Space, - Row, Col, Card, Statistic, Tooltip, + Alert, Table, Button, Modal, Input, Select, Popconfirm, Typography, Tag, Space, + Row, Col, Card, Statistic, App, Tooltip, } from 'antd'; import { message } from '../utils/antdMessage'; import type { TableProps } from 'antd'; -import { ImportOutlined, DeleteOutlined, FilterOutlined } from '@ant-design/icons'; -import { accountApi, userApi, type AccountItem, type BasicSummary, type UserInfo } from '../api/modules'; +import { ImportOutlined, DeleteOutlined, TagOutlined, FilterOutlined, EyeOutlined, EyeInvisibleOutlined } from '@ant-design/icons'; +import { accountApi, type AccountItem, type BasicSummary } from '../api/modules'; import { usePermissions } from '../hooks/usePermissions'; import { formatTime } from '../utils/time'; import { getErrorMessage } from '../utils/error'; @@ -70,8 +70,9 @@ function BindChip({ scope, status }: { scope: string; status?: string }) { export default function AccountsPage() { const navigate = useNavigate(); + // App.useApp() 的 modal 实例继承主题,静态 Modal.confirm 在暗色模式下是白底 + const { modal } = App.useApp(); const [accounts, setAccounts] = useState([]); - const [users, setUsers] = useState([]); const [tags, setTags] = useState([]); const [loading, setLoading] = useState(false); const [importOpen, setImportOpen] = useState(false); @@ -83,6 +84,11 @@ export default function AccountsPage() { const [ckFilter, setCkFilter] = useState(''); const [bindFilter, setBindFilter] = useState(''); const [searchText, setSearchText] = useState(''); + const [selectedRowKeys, setSelectedRowKeys] = useState([]); + const [selectedAllMatching, setSelectedAllMatching] = useState(false); + const [batchTagInput, setBatchTagInput] = useState(''); + const [batchTagVisible, setBatchTagVisible] = useState(false); + const [sensitiveVisible, setSensitiveVisible] = useState(false); const [total, setTotal] = useState(0); const [summary, setSummary] = useState({ total: 0, assigned_count: 0, unassigned_count: 0, tag_count: 0 }); const [pageSize, setPageSize] = useState(() => { @@ -92,9 +98,11 @@ export default function AccountsPage() { const [currentPage, setCurrentPage] = useState(1); const { can } = usePermissions(); + const canViewFull = can('account:view_full'); const canImport = can('account:import'); - const canAssign = can('account:assign'); + const canTag = can('account:tag') || canImport; const canDelete = can('account:delete'); + const canSelectRows = canTag || canDelete; const loadAccounts = useCallback(async () => { setLoading(true); @@ -109,6 +117,7 @@ export default function AccountsPage() { page: currentPage, page_size: pageSize, search: searchText.trim() || undefined, + include_sensitive: canViewFull && sensitiveVisible, }); setAccounts(data.items); setTotal(data.total); @@ -117,7 +126,7 @@ export default function AccountsPage() { } finally { setLoading(false); } - }, [currentPage, pageSize, searchText, tagFilter, statusFilter, ckFilter, bindFilter]); + }, [canViewFull, currentPage, pageSize, searchText, sensitiveVisible, tagFilter, statusFilter, ckFilter, bindFilter]); const loadSummary = useCallback(async () => { try { @@ -128,15 +137,6 @@ export default function AccountsPage() { } }, []); - const loadUsers = useCallback(async () => { - try { - const data = await userApi.list(); - setUsers(data.filter((u) => u.role === 'support')); - } catch { - // 忽略客服列表加载失败,账号列表仍可继续使用。 - } - }, []); - const loadTags = useCallback(async () => { try { const data = await accountApi.listTags(); @@ -151,10 +151,26 @@ export default function AccountsPage() { }, [loadAccounts]); useEffect(() => { - if (canAssign) loadUsers(); loadTags(); loadSummary(); - }, [canAssign, loadUsers, loadTags, loadSummary]); + }, [loadTags, loadSummary]); + + const selectedCount = selectedAllMatching ? total : selectedRowKeys.length; + const selectionPayload = useMemo(() => ({ + account_ids: selectedAllMatching ? [] : selectedRowKeys.map((key) => Number(key)), + all_matching: selectedAllMatching, + search: searchText.trim(), + tag: tagFilter, + }), [searchText, selectedAllMatching, selectedRowKeys, tagFilter]); + + const clearSelection = () => { + setSelectedRowKeys([]); + setSelectedAllMatching(false); + }; + + useEffect(() => { + clearSelection(); + }, [searchText, tagFilter]); const handleImport = async () => { if (!importText.trim()) { @@ -178,11 +194,20 @@ export default function AccountsPage() { } }; - const handleAssign = async (accountId: number, assignedTo: number | null) => { + const handleBatchTag = async () => { + if (selectedCount === 0) { + message.warning('请先选择账号'); + return; + } try { - await accountApi.assign(accountId, assignedTo); - message.success('已分配'); + const result = await accountApi.batchTagSelection({ ...selectionPayload, tag_value: batchTagInput }); + message.success(result.message); + setBatchTagVisible(false); + setBatchTagInput(''); + clearSelection(); loadAccounts(); + loadTags(); + loadSummary(); } catch (e: unknown) { message.error(getErrorMessage(e)); } @@ -192,6 +217,8 @@ export default function AccountsPage() { try { await accountApi.delete(id); message.success('已删除'); + setSelectedRowKeys((prev) => prev.filter((k) => k !== id)); + setSelectedAllMatching(false); loadAccounts(); loadTags(); loadSummary(); @@ -200,6 +227,44 @@ export default function AccountsPage() { } }; + const handleBatchDelete = async () => { + if (selectedCount === 0) { + message.warning('请先选择账号'); + return; + } + try { + const result = await accountApi.batchDeleteSelection(selectionPayload); + message.success(result.message); + clearSelection(); + loadAccounts(); + loadTags(); + loadSummary(); + } catch (e: unknown) { + message.error(getErrorMessage(e)); + } + }; + + const handleSensitiveVisibility = () => { + if (sensitiveVisible) { + // 立即清除已加载的敏感内容,再由 effect 重新获取脱敏列表。 + setAccounts((previous) => previous.map((account) => ({ + ...account, + password: undefined, + email: undefined, + email_password: undefined, + }))); + setSensitiveVisible(false); + return; + } + modal.confirm({ + title: '显示账号凭据?', + content: '密码、邮箱和邮箱密码将临时显示在当前页面。关闭或刷新页面后会恢复脱敏。', + okText: '显示', + cancelText: '取消', + onOk: () => setSensitiveVisible(true), + }); + }; + const columns: TableProps['columns'] = [ { // ID 不再单独占一列(客服不看),降到用户名下面做小字 —— 报障时还能念出来。 @@ -288,25 +353,13 @@ export default function AccountsPage() { }, ]; - columns.push({ - title: '分配给', - dataIndex: 'assigned_username', - render: (_: unknown, record: AccountItem) => { - if (canAssign) { - return ( - setBatchTagInput(vals.length > 0 ? vals[vals.length - 1] : '')} + options={tags.map((t) => ({ value: t, label: t }))} + /> + ); }