import React, { useState, useEffect } from 'react'; import { Table, Card, Button, Input, Space, Modal, Form, Select, InputNumber, Tag, message, Popconfirm, Divider, Tooltip, Radio, Row, Col, Typography, Badge } from 'antd'; import { PlusOutlined, EditOutlined, DeleteOutlined, SearchOutlined, UserOutlined, GlobalOutlined } from '@ant-design/icons'; import { useTranslation } from 'react-i18next'; import { useDict } from '../../hooks/useDict'; import { getHotWordPage, saveHotWord, updateHotWord, deleteHotWord, getPinyinSuggestion, HotWordVO, HotWordDTO } from '../../api/business/hotword'; const { Option } = Select; const { Text } = Typography; const HotWords: React.FC = () => { const { t } = useTranslation(); const [form] = Form.useForm(); const { items: categories, loading: dictLoading } = useDict('biz_hotword_category'); const [loading, setLoading] = useState(false); const [data, setData] = useState([]); const [total, setTotal] = useState(0); const [current, setCurrent] = useState(1); const [size, setSize] = useState(10); const [searchWord, setSearchWord] = useState(''); const [searchCategory, setSearchCategory] = useState(undefined); const [searchType, setSearchType] = useState(undefined); const [modalVisible, setModalVisible] = useState(false); const [editingId, setEditingId] = useState(null); const [submitLoading, setSubmitLoading] = useState(false); // 获取当前用户信息 const userProfile = React.useMemo(() => { const profileStr = sessionStorage.getItem("userProfile"); return profileStr ? JSON.parse(profileStr) : {}; }, []); // 判定是否具有管理员权限 (平台管理员或租户管理员) const isAdmin = React.useMemo(() => { return userProfile.isPlatformAdmin === true || userProfile.isTenantAdmin === true; }, [userProfile]); useEffect(() => { fetchData(); }, [current, size, searchWord, searchCategory, searchType]); const fetchData = async () => { setLoading(true); try { const res = await getHotWordPage({ current, size, word: searchWord, category: searchCategory, isPublic: searchType }); if (res.data && res.data.data) { setData(res.data.data.records); setTotal(res.data.data.total); } } catch (err) { console.error(err); } finally { setLoading(false); } }; const handleOpenModal = (record?: HotWordVO) => { if (record) { if (record.isPublic === 1 && !isAdmin) { message.error('公开热词仅限管理员修改'); return; } setEditingId(record.id); form.setFieldsValue({ ...record }); } else { setEditingId(null); form.resetFields(); form.setFieldsValue({ weight: 2, status: 1, isPublic: 0 }); } setModalVisible(true); }; const handleSubmit = async () => { try { const values = await form.validateFields(); setSubmitLoading(true); const payload: any = { ...values, pinyinList: values.pinyinList ? (Array.isArray(values.pinyinList) ? values.pinyinList : values.pinyinList.split(',').map((s: string) => s.trim()).filter(Boolean)) : [] }; if (editingId) { await updateHotWord({ ...payload, id: editingId }); message.success('更新成功'); } else { await saveHotWord(payload); message.success('添加成功'); } setModalVisible(false); fetchData(); } catch (err) { console.error(err); } finally { setSubmitLoading(false); } }; const handleWordBlur = async (e: React.FocusEvent) => { const word = e.target.value; if (word) { try { const res = await getPinyinSuggestion(word); if (res.data && res.data.data) { form.setFieldsValue({ pinyinList: res.data.data.join(', ') }); } } catch (err) { console.error(err); } } }; const columns = [ { title: '热词原文', dataIndex: 'word', key: 'word', render: (text: string, record: HotWordVO) => ( {text} {record.isPublic === 1 ? ( ) : ( )} ) }, { title: '拼音', dataIndex: 'pinyinList', key: 'pinyinList', render: (list: string[]) => ( {list?.map(p => {p})} ) }, { title: '类别', dataIndex: 'category', key: 'category', render: (val: string) => categories.find(i => i.itemValue === val)?.itemLabel || val }, { title: '范围', dataIndex: 'isPublic', key: 'isPublic', render: (val: number) => val === 1 ? 公开 : 私有 }, { title: '权重', dataIndex: 'weight', key: 'weight', render: (val: number) => {val} }, { title: '状态', dataIndex: 'status', key: 'status', render: (status: number) => status === 1 ? : }, { title: '操作', key: 'action', render: (_: any, record: HotWordVO) => { const isMine = record.creatorId === userProfile.userId; const canEdit = record.isPublic === 1 ? isAdmin : (isMine || isAdmin); return ( {canEdit ? ( <> handleDelete(record.id)} okText={t('common.confirm')} cancelText={t('common.cancel')} > ) : ( 无权操作 )} ); } } ]; return (
} allowClear onPressEnter={(e) => {setSearchWord((e.target as any).value); setCurrent(1);}} style={{ width: 180 }} /> } > `共 ${t} 条`, onChange: (p, s) => { setCurrent(p); setSize(s); } }} /> setModalVisible(false)} confirmLoading={submitLoading} width={550} >
{isAdmin && ( )} ); }; export default HotWords;