Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,252 @@
|
||||
/*
|
||||
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, { useEffect, useRef, useState } from 'react';
|
||||
import { Modal, Form, Typography, Banner, Spin, Tag } from '@douyinfe/semi-ui';
|
||||
import { API, showError, renderQuotaWithPrompt } from '../../../../helpers';
|
||||
import {
|
||||
quotaToMoney,
|
||||
moneyToQuota,
|
||||
} from '../../../common/ui/QuotaInput';
|
||||
|
||||
const { Text } = Typography;
|
||||
|
||||
const statusMeta = {
|
||||
1: { color: 'green', label: '已启用' },
|
||||
2: { color: 'grey', label: '已禁用' },
|
||||
3: { color: 'red', label: '已过期' },
|
||||
4: { color: 'orange', label: '已耗尽' },
|
||||
};
|
||||
|
||||
/**
|
||||
* 管理员编辑秘钥弹窗
|
||||
* 允许修改:name / group / unlimited_quota / remain_quota / expired_time
|
||||
* 不修改:
|
||||
* - status —— 启用/禁用走专用按钮,避免在编辑路径上绕过状态校验
|
||||
* - user_id / key / used_quota / model_limits / allow_ips —— 由用户自管
|
||||
*/
|
||||
const AdminEditTokenModal = ({
|
||||
visible,
|
||||
onCancel,
|
||||
onConfirm,
|
||||
token,
|
||||
groupOptions,
|
||||
t,
|
||||
}) => {
|
||||
const formApiRef = useRef(null);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [unlimited, setUnlimited] = useState(false);
|
||||
const [neverExpire, setNeverExpire] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (visible && token) {
|
||||
// expired_time 视为"永不过期"的情况:-1(约定)或 0(旧数据兜底)
|
||||
const isNeverExpire =
|
||||
token.expired_time === -1 || token.expired_time === 0;
|
||||
setUnlimited(!!token.unlimited_quota);
|
||||
setNeverExpire(isNeverExpire);
|
||||
setTimeout(() => {
|
||||
formApiRef.current?.setValues({
|
||||
name: token.name || '',
|
||||
group: token.group || '',
|
||||
unlimited_quota: !!token.unlimited_quota,
|
||||
// remain_quota 内部单位是 token 数;UI 用元(quota_per_unit 额度 = ¥1)
|
||||
remain_quota_amount: token.unlimited_quota
|
||||
? 0
|
||||
: quotaToMoney(token.remain_quota || 0) || 0,
|
||||
expired_time:
|
||||
!isNeverExpire && token.expired_time > 0
|
||||
? new Date(token.expired_time * 1000)
|
||||
: null,
|
||||
never_expire: isNeverExpire,
|
||||
});
|
||||
}, 0);
|
||||
}
|
||||
}, [visible, token]);
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (!formApiRef.current) return;
|
||||
let values;
|
||||
try {
|
||||
values = await formApiRef.current.validate();
|
||||
} catch (e) {
|
||||
return;
|
||||
}
|
||||
if (!token) return;
|
||||
|
||||
// 显式校验:未勾选"永不过期"时必须选过期时间,避免被默默改为 -1
|
||||
if (!values.never_expire && !values.expired_time) {
|
||||
showError(t('请选择过期时间'));
|
||||
return;
|
||||
}
|
||||
setSubmitting(true);
|
||||
|
||||
// 注意:不传 status / model_limits / allow_ips / model_limits_enabled
|
||||
// status 由专用按钮处理;其余字段由 Token 所属用户自行管理
|
||||
// 后端 AdminUpdateTokenById 会保留 DB 中的原值
|
||||
const payload = {
|
||||
id: token.id,
|
||||
name: values.name || '',
|
||||
group: values.group || '',
|
||||
unlimited_quota: !!values.unlimited_quota,
|
||||
remain_quota: values.unlimited_quota
|
||||
? token.remain_quota
|
||||
: moneyToQuota(values.remain_quota_amount || 0) || 0,
|
||||
expired_time: values.never_expire
|
||||
? -1
|
||||
: Math.floor(new Date(values.expired_time).getTime() / 1000),
|
||||
};
|
||||
|
||||
try {
|
||||
const res = await API.put('/api/token/admin', payload);
|
||||
const { success, message } = res.data;
|
||||
if (success) {
|
||||
onConfirm && onConfirm();
|
||||
} else {
|
||||
showError(message);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[AdminEditToken] save failed:', e);
|
||||
showError(e.message || t('保存失败'));
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (!token) return null;
|
||||
|
||||
const isOrgToken = token.organization_id && token.organization_id > 0;
|
||||
const curStatusMeta = statusMeta[token.status] || statusMeta[2];
|
||||
|
||||
return (
|
||||
<Modal
|
||||
title={t('编辑秘钥')}
|
||||
visible={visible}
|
||||
onCancel={onCancel}
|
||||
onOk={handleSubmit}
|
||||
confirmLoading={submitting}
|
||||
okText={t('保存')}
|
||||
cancelText={t('取消')}
|
||||
width={560}
|
||||
>
|
||||
<Spin spinning={submitting}>
|
||||
<div className='mb-3'>
|
||||
<Banner
|
||||
type='info'
|
||||
description={
|
||||
<span>
|
||||
{t('正在以管理员身份编辑')}{' '}
|
||||
<Text strong>
|
||||
{token.display_name || token.username || '—'}
|
||||
</Text>{' '}
|
||||
(ID: {token.user_id}) {t('的秘钥')}
|
||||
<Tag
|
||||
color={curStatusMeta.color}
|
||||
shape='circle'
|
||||
size='small'
|
||||
className='!ml-2'
|
||||
>
|
||||
{t(curStatusMeta.label)}
|
||||
</Tag>
|
||||
</span>
|
||||
}
|
||||
closeIcon={null}
|
||||
/>
|
||||
{isOrgToken && (
|
||||
<Banner
|
||||
type='warning'
|
||||
description={t(
|
||||
'该秘钥属于企业 Token,修改额度或过期时间将影响企业内多用户的使用',
|
||||
)}
|
||||
closeIcon={null}
|
||||
className='!mt-2'
|
||||
/>
|
||||
)}
|
||||
<div className='mt-2 text-xs text-[var(--semi-color-text-2)]'>
|
||||
{t('启用 / 禁用请使用列表中的专用按钮')}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Form
|
||||
getFormApi={(api) => (formApiRef.current = api)}
|
||||
labelPosition='left'
|
||||
labelWidth={110}
|
||||
>
|
||||
<Form.Input
|
||||
field='name'
|
||||
label={t('秘钥名称')}
|
||||
placeholder={t('给秘钥起个名字')}
|
||||
maxLength={30}
|
||||
rules={[{ required: true, message: t('请输入秘钥名称') }]}
|
||||
/>
|
||||
|
||||
<Form.Select
|
||||
field='group'
|
||||
label={t('分组')}
|
||||
optionList={groupOptions}
|
||||
allowClear
|
||||
placeholder={t('使用用户默认分组')}
|
||||
className='w-full'
|
||||
/>
|
||||
|
||||
<Form.Switch
|
||||
field='unlimited_quota'
|
||||
label={t('无限额度')}
|
||||
onChange={(v) => setUnlimited(v)}
|
||||
/>
|
||||
|
||||
{!unlimited && (
|
||||
<Form.InputNumber
|
||||
field='remain_quota_amount'
|
||||
label={t('剩余额度(元)')}
|
||||
min={0}
|
||||
precision={2}
|
||||
step={1}
|
||||
suffix='¥'
|
||||
extraText={
|
||||
<Text type='tertiary' size='small'>
|
||||
{t('当前已用')}:{' '}
|
||||
{renderQuotaWithPrompt(token.used_quota || 0)}
|
||||
</Text>
|
||||
}
|
||||
className='w-full'
|
||||
/>
|
||||
)}
|
||||
|
||||
<Form.Switch
|
||||
field='never_expire'
|
||||
label={t('永不过期')}
|
||||
onChange={(v) => setNeverExpire(v)}
|
||||
/>
|
||||
|
||||
{!neverExpire && (
|
||||
<Form.DatePicker
|
||||
field='expired_time'
|
||||
label={t('过期时间')}
|
||||
type='dateTime'
|
||||
className='w-full'
|
||||
/>
|
||||
)}
|
||||
</Form>
|
||||
</Spin>
|
||||
</Modal>
|
||||
);
|
||||
};
|
||||
|
||||
export default AdminEditTokenModal;
|
||||
Reference in New Issue
Block a user