8a48765a46
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
239 lines
7.2 KiB
React
239 lines
7.2 KiB
React
import { Trans } from 'react-i18next';
|
||
import React, { useEffect, useRef, useState } from 'react';
|
||
import { Modal, Form, Typography, Banner, Spin, Tag } from '@/components/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 > 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>
|
||
<Trans
|
||
t={t}
|
||
i18nKey='正在以管理员身份编辑 <part0>{{value}}</part0> (ID:{{user_id}}) 的密钥<part1>{{label}}</part1>'
|
||
values={{
|
||
value: token.display_name || token.username || '—',
|
||
user_id: token.user_id,
|
||
label: t(curStatusMeta.label),
|
||
}}
|
||
components={{
|
||
part0: <Text strong />,
|
||
part1: (
|
||
<Tag
|
||
color={curStatusMeta.color}
|
||
shape='circle'
|
||
size='small'
|
||
className='!ml-2'
|
||
/>
|
||
),
|
||
}}
|
||
/>
|
||
</span>
|
||
}
|
||
closeIcon={null}
|
||
/>
|
||
{isOrgToken && (
|
||
<Banner
|
||
type='warning'
|
||
description={t(
|
||
'该密钥属于企业 Token,修改额度或过期时间将影响企业内多用户的使用',
|
||
)}
|
||
closeIcon={null}
|
||
className='!mt-2'
|
||
/>
|
||
)}
|
||
<div className='mt-2 text-xs text-[var(--ui-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;
|