Initial import of weiyuan frontend

This commit is contained in:
qiubaishen
2026-10-15 16:33:46 +08:00
commit d8f3aee261
774 changed files with 374914 additions and 0 deletions
+156
View File
@@ -0,0 +1,156 @@
/*
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 { InputNumber, withField } from '@douyinfe/semi-ui';
import { useTranslation } from 'react-i18next';
import { getQuotaPerUnit } from '../../../helpers';
/**
* 金额额度输入组件
*
* 对外的 value/onChange 始终使用系统内部额度单位(token,与后端一致),
* UI 上则以人民币金额输入/展示(quotaPerUnit 额度 = ¥1,默认 500000),
* 后端无需任何改动。
*
* quota_display_type = TOKENS 时退化为原始额度输入,行为与旧版一致。
*
* @param {number|string|null} value 内部额度值(token 单位)
* @param {(quota:number|null)=>void} onChange 变更回调,参数为换算后的内部额度值;清空时为 null
* @param {number} [minQuota] 最小值(token 单位)
* @param {number} [maxQuota] 最大值(token 单位)
* @param {string} [placeholder] 占位文案,缺省根据货币模式自动生成
* 其余属性透传给 Semi InputNumber(size/style/disabled/prefix 等)
*/
const FALLBACK_QUOTA_PER_UNIT = 500000;
const CURRENCY_SYMBOL = '¥';
function getSafeQuotaPerUnit() {
const qpu = getQuotaPerUnit();
return Number.isFinite(qpu) && qpu > 0 ? qpu : FALLBACK_QUOTA_PER_UNIT;
}
function isTokensMode() {
return (localStorage.getItem('quota_display_type') || 'USD') === 'TOKENS';
}
/** 内部额度 → 显示金额(人民币元),保留 6 位小数避免浮点噪声 */
export function quotaToMoney(quota) {
const n = typeof quota === 'string' ? parseFloat(quota) : quota;
if (n === null || n === undefined || !Number.isFinite(n)) return null;
return Number((n / getSafeQuotaPerUnit()).toFixed(6));
}
/** 显示金额(人民币元)→ 内部额度(取整,支持负数) */
export function moneyToQuota(money) {
const n = typeof money === 'string' ? parseFloat(money) : money;
if (n === null || n === undefined || !Number.isFinite(n)) return null;
return Math.round(n * getSafeQuotaPerUnit());
}
const QuotaInput = ({
value,
onChange,
minQuota,
maxQuota,
placeholder,
...rest
}) => {
const { t } = useTranslation();
const tokensMode = isTokensMode();
const toDisplay = (q) => {
if (q === null || q === undefined || q === '') return null;
if (tokensMode) {
const n = typeof q === 'string' ? parseFloat(q) : q;
return Number.isFinite(n) ? n : null;
}
return quotaToMoney(q);
};
const [innerValue, setInnerValue] = useState(() => toDisplay(value));
// 记录组件自己最近一次上抛的额度值:外部值变化若并非来自本组件输入,
// 才回写显示值,避免用户输入"1."之类中间态被换算打断
const lastEmittedRef = useRef(
value === null || value === undefined || value === ''
? null
: Number(value),
);
useEffect(() => {
const normalized =
value === null || value === undefined || value === ''
? null
: Number(value);
if (normalized !== lastEmittedRef.current) {
lastEmittedRef.current = normalized;
setInnerValue(toDisplay(value));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value]);
const handleChange = (v) => {
setInnerValue(v);
const n = typeof v === 'string' ? parseFloat(v) : v;
let quota;
if (n === null || n === undefined || !Number.isFinite(n)) {
quota = null;
} else {
quota = tokensMode ? Math.round(n) : moneyToQuota(n);
}
lastEmittedRef.current = quota;
onChange?.(quota);
};
const min =
minQuota === undefined
? undefined
: tokensMode
? minQuota
: quotaToMoney(minQuota);
const max =
maxQuota === undefined
? undefined
: tokensMode
? maxQuota
: quotaToMoney(maxQuota);
return (
<InputNumber
value={innerValue}
onChange={handleChange}
min={min}
max={max}
prefix={tokensMode ? undefined : CURRENCY_SYMBOL}
placeholder={
placeholder ?? (tokensMode ? t('请输入额度') : t('请输入金额'))
}
{...rest}
/>
);
};
/** Semi Form 版本:表单值仍为内部额度单位,可直接配合 field/rules 使用 */
export const FormQuotaInput = withField(QuotaInput, {
valueKey: 'value',
onKeyChangeFnName: 'onChange',
});
export default QuotaInput;