import React, { useEffect, useState } from 'react'; import { API, showError, showSuccess, renderGroup, renderNumber, renderQuota } from '../../helpers'; import { Button, Card, Divider, Dropdown, Input, Modal, Select, Space, Table, Tag, Typography, } from '@douyinfe/semi-ui'; import { IconPlus, IconSearch, IconEdit, IconDelete, IconStop, IconPlay, IconMore, IconUserAdd, IconArrowUp, IconArrowDown, } from '@douyinfe/semi-icons'; import { ITEMS_PER_PAGE } from '../../constants'; import AddUser from '../../pages/User/AddUser'; import EditUser from '../../pages/User/EditUser'; import { useTranslation } from 'react-i18next'; const { Text } = Typography; const UsersTable = () => { const { t } = useTranslation(); function renderRole(role) { switch (role) { case 1: return ( {t('普通用户')} ); case 10: return ( {t('管理员')} ); case 100: return ( {t('超级管理员')} ); default: return ( {t('未知身份')} ); } } const renderStatus = (status) => { switch (status) { case 1: return {t('已激活')}; case 2: return ( {t('已封禁')} ); default: return ( {t('未知状态')} ); } }; const columns = [ { title: 'ID', dataIndex: 'id', width: 50, }, { title: t('用户名'), dataIndex: 'username', width: 100, }, { title: t('分组'), dataIndex: 'group', width: 100, render: (text, record, index) => { return
{renderGroup(text)}
; }, }, { title: t('统计信息'), dataIndex: 'info', width: 280, render: (text, record, index) => { return (
{t('剩余')}: {renderQuota(record.quota)} {t('已用')}: {renderQuota(record.used_quota)} {t('调用')}: {renderNumber(record.request_count)}
); }, }, { title: t('邀请信息'), dataIndex: 'invite', width: 250, render: (text, record, index) => { return (
{t('邀请')}: {renderNumber(record.aff_count)} {t('收益')}: {renderQuota(record.aff_history_quota)} {record.inviter_id === 0 ? t('无邀请人') : `邀请人: ${record.inviter_id}`}
); }, }, { title: t('角色'), dataIndex: 'role', width: 120, render: (text, record, index) => { return
{renderRole(text)}
; }, }, { title: t('状态'), dataIndex: 'status', width: 100, render: (text, record, index) => { return (
{record.DeletedAt !== null ? ( {t('已注销')} ) : ( renderStatus(text) )}
); }, }, { title: '', dataIndex: 'operate', width: 150, render: (text, record, index) => { if (record.DeletedAt !== null) { return <>; } // 创建更多操作的下拉菜单项 const moreMenuItems = [ { node: 'item', name: t('提升'), icon: , type: 'warning', onClick: () => { Modal.confirm({ title: t('确定要提升此用户吗?'), content: t('此操作将提升用户的权限级别'), onOk: () => { manageUser(record.id, 'promote', record); }, }); }, }, { node: 'item', name: t('降级'), icon: , type: 'secondary', onClick: () => { Modal.confirm({ title: t('确定要降级此用户吗?'), content: t('此操作将降低用户的权限级别'), onOk: () => { manageUser(record.id, 'demote', record); }, }); }, }, { node: 'item', name: t('注销'), icon: , type: 'danger', onClick: () => { Modal.confirm({ title: t('确定是否要注销此用户?'), content: t('相当于删除用户,此修改将不可逆'), onOk: () => { manageUser(record.id, 'delete', record).then(() => { removeRecord(record.id); }); }, }); }, } ]; // 动态添加启用/禁用按钮 if (record.status === 1) { moreMenuItems.splice(-1, 0, { node: 'item', name: t('禁用'), icon: , type: 'warning', onClick: () => { manageUser(record.id, 'disable', record); }, }); } else { moreMenuItems.splice(-1, 0, { node: 'item', name: t('启用'), icon: , type: 'secondary', onClick: () => { manageUser(record.id, 'enable', record); }, disabled: record.status === 3, }); } return (
} placeholder={t('支持搜索用户的 ID、用户名、显示名称和邮箱地址')} value={searchKeyword} onChange={handleKeywordChange} className="!rounded-full" showClear />