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 (
, part1: ( ), }} /> } closeIcon={null} /> {isOrgToken && ( )}
{t('启用 / 禁用请使用列表中的专用按钮')}
(formApiRef.current = api)} labelPosition='left' labelWidth={110} > setUnlimited(v)} /> {!unlimited && ( {t('当前已用')}:{' '} {renderQuotaWithPrompt(token.used_quota || 0)} } className='w-full' /> )} setNeverExpire(v)} /> {!neverExpire && ( )}
); }; export default AdminEditTokenModal;