import React from 'react';
import { Button, Switch, Tag as AntTag } from 'antd';
import { Tag, Typography, Popover } from '@/components/ui';
import {
renderGroup,
renderQuota,
isAdmin,
timestamp2string,
} from '../../../helpers';
const { Text } = Typography;
const statusMeta = {
3: { color: 'error', label: '已过期' },
4: { color: 'warning', label: '已耗尽' },
};
const renderStatus = (status, record, t, showEnableDisableModal) => {
const meta =
statusMeta[status] ||
(![1, 2].includes(status) ? { color: 'error', label: '未知状态' } : null);
return (
showEnableDisableModal(record, status === 1 ? 'disable' : 'enable')
}
/>
{meta && (
{t(meta.label)}
)}
);
};
/**
* 用户列:display_name 主显,user_id 与 username 小字辅显
*/
const renderUserCell = (text, record, t) => {
const displayName = record.display_name || record.username || t('未知用户');
return (
{displayName}
#{record.user_id}
{record.username && record.username !== displayName && (
@{record.username}
)}
);
};
const renderKey = (text) => {
// 后端已脱敏,前端再加 sk- 前缀
return sk-{text};
};
const renderQuotaCell = (text, record, t) => {
if (record.unlimited_quota) {
return (
{t('无限制')}
);
}
const used = record.used_quota || 0;
const remain = record.remain_quota || 0;
const popoverContent = (
{t('已用')}: {renderQuota(used)}
{t('剩余')}: {renderQuota(remain)}
);
return (
{renderQuota(remain)}
);
};
const renderTime = (ts, t) => {
if (!ts || ts === 0)
return —;
if (ts === -1) return {t('永不过期')};
return {timestamp2string(ts)};
};
const renderScope = (orgId, t) => {
if (!orgId || orgId === 0) {
return (
{t('个人')}
);
}
return (
{t('企业')} #{orgId}
);
};
const renderOperations = (
text,
record,
{ showEditTokenModal, showDeleteTokenModal, t },
) => {
if (record.DeletedAt) return <>>;
if (!isAdmin()) return null;
return (
);
};
export const getAdminTokensColumns = ({
t,
showEditTokenModal,
showEnableDisableModal,
showDeleteTokenModal,
}) => {
return [
{
title: 'ID',
dataIndex: 'id',
width: 70,
},
{
title: t('用户'),
dataIndex: 'user',
width: 180,
render: (text, record) => renderUserCell(text, record, t),
},
{
title: t('名称'),
dataIndex: 'name',
render: (text) => (
{text || —}
),
},
{
title: t('密钥'),
dataIndex: 'key',
render: renderKey,
},
{
title: t('状态'),
dataIndex: 'status',
width: 150,
render: (text, record) =>
renderStatus(text, record, t, showEnableDisableModal),
},
{
title: t('归属'),
dataIndex: 'organization_id',
width: 100,
render: (text) => renderScope(text, t),
},
{
title: t('分组'),
dataIndex: 'group',
width: 110,
render: (text) => {renderGroup(text)}
,
},
{
title: t('剩余额度'),
dataIndex: 'remain_quota',
width: 110,
render: (text, record) => renderQuotaCell(text, record, t),
},
{
title: t('已用额度'),
dataIndex: 'used_quota',
width: 110,
render: (text) => {renderQuota(text || 0)},
},
{
title: t('创建时间'),
dataIndex: 'created_time',
width: 160,
render: (text) => renderTime(text, t),
},
{
title: t('过期时间'),
dataIndex: 'expired_time',
width: 160,
render: (text) => renderTime(text, t),
},
{
title: t('最近访问'),
dataIndex: 'accessed_time',
width: 160,
render: (text) => renderTime(text, t),
},
{
title: t('操作'),
dataIndex: 'operate',
fixed: 'right',
width: 130,
render: (text, record) =>
renderOperations(text, record, {
showEditTokenModal,
showDeleteTokenModal,
t,
}),
},
];
};