Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,289 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
import React from 'react';
|
||||
import {
|
||||
Button,
|
||||
Space,
|
||||
Tag,
|
||||
Typography,
|
||||
Popover,
|
||||
Dropdown,
|
||||
} from '@douyinfe/semi-ui';
|
||||
import { IconMore } from '@douyinfe/semi-icons';
|
||||
import {
|
||||
renderGroup,
|
||||
renderQuota,
|
||||
isAdmin,
|
||||
timestamp2string,
|
||||
} from '../../../helpers';
|
||||
|
||||
const { Text } = Typography;
|
||||
|
||||
const statusMeta = {
|
||||
1: { color: 'green', label: '已启用' },
|
||||
2: { color: 'grey', label: '已禁用' },
|
||||
3: { color: 'red', label: '已过期' },
|
||||
4: { color: 'orange', label: '已耗尽' },
|
||||
};
|
||||
|
||||
const renderStatus = (status, t) => {
|
||||
const meta = statusMeta[status] || { color: 'red', label: t('未知状态') };
|
||||
return (
|
||||
<Tag color={meta.color} shape='circle' size='small'>
|
||||
{t(meta.label)}
|
||||
</Tag>
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* 用户列:display_name 主显,user_id 与 username 小字辅显
|
||||
*/
|
||||
const renderUserCell = (text, record, t) => {
|
||||
const displayName = record.display_name || record.username || t('未知用户');
|
||||
return (
|
||||
<div className='flex flex-col leading-tight'>
|
||||
<Text strong className='!text-sm'>
|
||||
{displayName}
|
||||
</Text>
|
||||
<div className='flex items-center gap-1 text-xs text-[var(--semi-color-text-2)]'>
|
||||
<span>#{record.user_id}</span>
|
||||
{record.username && record.username !== displayName && (
|
||||
<span className='truncate max-w-[120px]'>@{record.username}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const renderKey = (text, record) => {
|
||||
// 后端已脱敏,前端再加 sk- 前缀
|
||||
return (
|
||||
<code className='text-xs bg-[var(--semi-color-fill-0)] px-2 py-0.5 rounded font-mono'>
|
||||
sk-{text}
|
||||
</code>
|
||||
);
|
||||
};
|
||||
|
||||
const renderQuotaCell = (text, record, t) => {
|
||||
if (record.unlimited_quota) {
|
||||
return (
|
||||
<Tag color='violet' shape='circle' size='small'>
|
||||
{t('无限制')}
|
||||
</Tag>
|
||||
);
|
||||
}
|
||||
const used = record.used_quota || 0;
|
||||
const remain = record.remain_quota || 0;
|
||||
const popoverContent = (
|
||||
<div className='text-xs p-2'>
|
||||
<div>
|
||||
{t('已用')}: {renderQuota(used)}
|
||||
</div>
|
||||
<div>
|
||||
{t('剩余')}: {renderQuota(remain)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
return (
|
||||
<Popover content={popoverContent} position='top'>
|
||||
<Tag color='white' shape='circle' size='small'>
|
||||
{renderQuota(remain)}
|
||||
</Tag>
|
||||
</Popover>
|
||||
);
|
||||
};
|
||||
|
||||
const renderTime = (ts, t) => {
|
||||
if (!ts || ts === 0)
|
||||
return <span className='text-[var(--semi-color-text-2)]'>—</span>;
|
||||
if (ts === -1)
|
||||
return (
|
||||
<Tag color='blue' shape='circle' size='small'>
|
||||
{t('永不过期')}
|
||||
</Tag>
|
||||
);
|
||||
return <span className='text-xs'>{timestamp2string(ts)}</span>;
|
||||
};
|
||||
|
||||
const renderScope = (orgId, t) => {
|
||||
if (!orgId || orgId === 0) {
|
||||
return (
|
||||
<Tag color='blue' shape='circle' size='small'>
|
||||
{t('个人')}
|
||||
</Tag>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<Tag color='purple' shape='circle' size='small'>
|
||||
{t('企业')} #{orgId}
|
||||
</Tag>
|
||||
);
|
||||
};
|
||||
|
||||
const renderOperations = (
|
||||
text,
|
||||
record,
|
||||
{ showEditTokenModal, showEnableDisableModal, showDeleteTokenModal, t },
|
||||
) => {
|
||||
if (record.DeletedAt) return <></>;
|
||||
const isEnabled = record.status === 1;
|
||||
if (!isAdmin()) return null;
|
||||
return (
|
||||
<Space>
|
||||
<Button
|
||||
size='small'
|
||||
type='tertiary'
|
||||
onClick={() => showEditTokenModal(record)}
|
||||
>
|
||||
{t('编辑')}
|
||||
</Button>
|
||||
{isEnabled ? (
|
||||
<Button
|
||||
size='small'
|
||||
type='danger'
|
||||
onClick={() => showEnableDisableModal(record, 'disable')}
|
||||
>
|
||||
{t('禁用')}
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
size='small'
|
||||
onClick={() => showEnableDisableModal(record, 'enable')}
|
||||
>
|
||||
{t('启用')}
|
||||
</Button>
|
||||
)}
|
||||
<Dropdown
|
||||
trigger='click'
|
||||
position='bottomRight'
|
||||
menu={[
|
||||
{
|
||||
node: 'item',
|
||||
name: t('删除'),
|
||||
type: 'danger',
|
||||
onClick: () => showDeleteTokenModal(record),
|
||||
},
|
||||
]}
|
||||
>
|
||||
<Button size='small' type='tertiary' icon={<IconMore />} />
|
||||
</Dropdown>
|
||||
</Space>
|
||||
);
|
||||
};
|
||||
|
||||
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) => (
|
||||
<span className='truncate inline-block max-w-[160px]' title={text}>
|
||||
{text || <Text type='tertiary'>—</Text>}
|
||||
</span>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('秘钥'),
|
||||
dataIndex: 'key',
|
||||
render: renderKey,
|
||||
},
|
||||
{
|
||||
title: t('状态'),
|
||||
dataIndex: 'status',
|
||||
width: 90,
|
||||
render: (text) => renderStatus(text, t),
|
||||
},
|
||||
{
|
||||
title: t('归属'),
|
||||
dataIndex: 'organization_id',
|
||||
width: 100,
|
||||
render: (text) => renderScope(text, t),
|
||||
},
|
||||
{
|
||||
title: t('分组'),
|
||||
dataIndex: 'group',
|
||||
width: 110,
|
||||
render: (text) => <div>{renderGroup(text)}</div>,
|
||||
},
|
||||
{
|
||||
title: t('剩余额度'),
|
||||
dataIndex: 'remain_quota',
|
||||
width: 110,
|
||||
render: (text, record) => renderQuotaCell(text, record, t),
|
||||
},
|
||||
{
|
||||
title: t('已用额度'),
|
||||
dataIndex: 'used_quota',
|
||||
width: 110,
|
||||
render: (text) => (
|
||||
<Text type='tertiary' size='small'>
|
||||
{renderQuota(text || 0)}
|
||||
</Text>
|
||||
),
|
||||
},
|
||||
{
|
||||
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: '',
|
||||
dataIndex: 'operate',
|
||||
fixed: 'right',
|
||||
width: 200,
|
||||
render: (text, record) =>
|
||||
renderOperations(text, record, {
|
||||
showEditTokenModal,
|
||||
showEnableDisableModal,
|
||||
showDeleteTokenModal,
|
||||
t,
|
||||
}),
|
||||
},
|
||||
];
|
||||
};
|
||||
Reference in New Issue
Block a user