471d7843de
- 公共表格:统一分组标签、状态开关和危险操作确认,支持防重复提交和失败重试,修复浮层触发组件的 ref 转发。 - 控制台:统一用户和渠道编辑抽屉,修正分组回填与提交格式,对齐系统设置操作区和预约列表。 - 企业中心:调整技能中心页签和窄屏布局,优化质量监控提示栏、效能分析及成本对账,统一消耗明细和审计日志筛选。 - 用户中心及公共页面:优化密钥和消费记录展示,对齐模型超市筛选,统一公开与内嵌客户端下载页。 - 同步多语言文案、组件规范与移动端视口要求。
554 lines
16 KiB
React
554 lines
16 KiB
React
import TableActionPopconfirm from '@/components/common/table/TableActionPopconfirm';
|
||
import TableStatusSwitch from '@/components/common/table/TableStatusSwitch';
|
||
import React, { useEffect, useRef, useState } from 'react';
|
||
import {
|
||
Button,
|
||
Col,
|
||
Form,
|
||
Input,
|
||
InputNumber,
|
||
Modal,
|
||
Row,
|
||
Space,
|
||
Spin,
|
||
Table,
|
||
Tag,
|
||
Typography,
|
||
} from '@/components/ui';
|
||
import { IconDelete, IconPlus } from '@/components/ui/icons';
|
||
import { useTranslation } from 'react-i18next';
|
||
import { API, showError, showSuccess } from '../../../helpers';
|
||
|
||
const OPTION_KEY = 'image.image_resolution_pricing';
|
||
const MATCH_MODE_OPTIONS = [
|
||
{ value: 'exact', label: '精确匹配' },
|
||
{ value: 'prefix', label: '前缀匹配' },
|
||
];
|
||
const RESOLUTION_RULE_OPTIONS = [
|
||
{
|
||
value: 'manual',
|
||
label: '手工别名',
|
||
requiredTiers: [],
|
||
help: '仅按档位 key 和别名匹配;未命中时使用默认档位。',
|
||
},
|
||
{
|
||
value: 'gpt_image_2',
|
||
label: 'GPT Image 2',
|
||
requiredTiers: ['1k', '2k', '4k'],
|
||
help: '识别官方尺寸及合法自定义宽高,自定义尺寸按总像素向上取档。',
|
||
},
|
||
{
|
||
value: 'gemini',
|
||
label: 'Gemini',
|
||
requiredTiers: ['1k', '2k', '4k'],
|
||
help: '识别 imageSize、官方比例尺寸及自定义宽高。0.5K 按 1K 档计费。',
|
||
},
|
||
{
|
||
value: 'seedream_4_0',
|
||
label: 'Seedream 4.0',
|
||
requiredTiers: ['1k', '2k', '4k'],
|
||
help: '按 Seedream 4.0 官方 1K、2K、4K 尺寸和自定义宽高识别。',
|
||
},
|
||
{
|
||
value: 'seedream_4_5',
|
||
label: 'Seedream 4.5',
|
||
requiredTiers: ['2k', '4k'],
|
||
help: '按 Seedream 4.5 官方 2K、4K 尺寸和自定义宽高识别。',
|
||
},
|
||
{
|
||
value: 'seedream_5_0_pro',
|
||
label: 'Seedream 5.0 Pro',
|
||
requiredTiers: ['1k', '2k'],
|
||
help: '识别 1K、1.5K、2K,1.5K 按 1K 档计费。',
|
||
},
|
||
{
|
||
value: 'seedream_5_0_lite',
|
||
label: 'Seedream 5.0 Lite',
|
||
requiredTiers: ['2k', '3k', '4k'],
|
||
help: '按 Seedream 5.0 Lite 官方 2K、3K、4K 尺寸和自定义宽高识别。',
|
||
},
|
||
];
|
||
|
||
const emptyTier = () => ({
|
||
key: '',
|
||
label: '',
|
||
price_per_image: 0,
|
||
aliases: '',
|
||
});
|
||
|
||
const normalize = (value) =>
|
||
String(value || '')
|
||
.trim()
|
||
.toLowerCase()
|
||
.replaceAll(' ', '');
|
||
|
||
const parsePricing = (value) => {
|
||
try {
|
||
const parsed = JSON.parse(value || '[]');
|
||
return Array.isArray(parsed) ? parsed : [];
|
||
} catch {
|
||
return [];
|
||
}
|
||
};
|
||
|
||
const resolutionRuleOption = (value) =>
|
||
RESOLUTION_RULE_OPTIONS.find((option) => option.value === value) ||
|
||
RESOLUTION_RULE_OPTIONS[0];
|
||
|
||
export default function SettingImagePricing({ options, refresh }) {
|
||
const { t } = useTranslation();
|
||
const [loading, setLoading] = useState(false);
|
||
const [configs, setConfigs] = useState([]);
|
||
const [modalVisible, setModalVisible] = useState(false);
|
||
const [editingIndex, setEditingIndex] = useState(-1);
|
||
const [resolutionRule, setResolutionRule] = useState('manual');
|
||
const [tierRows, setTierRows] = useState([emptyTier()]);
|
||
const formRef = useRef();
|
||
|
||
useEffect(() => {
|
||
setConfigs(parsePricing(options?.[OPTION_KEY]));
|
||
}, [options]);
|
||
|
||
const save = async (next) => {
|
||
setLoading(true);
|
||
try {
|
||
const response = await API.put('/api/option/', {
|
||
key: OPTION_KEY,
|
||
value: JSON.stringify(next),
|
||
});
|
||
if (!response.data.success) {
|
||
showError(response.data.message || t('保存失败'));
|
||
return false;
|
||
}
|
||
setConfigs(next);
|
||
showSuccess(t('保存成功'));
|
||
refresh();
|
||
return true;
|
||
} catch (error) {
|
||
showError(error?.response?.data?.message || t('保存失败,请重试'));
|
||
return false;
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
};
|
||
|
||
const openAdd = () => {
|
||
setEditingIndex(-1);
|
||
setResolutionRule('manual');
|
||
setTierRows([emptyTier()]);
|
||
setModalVisible(true);
|
||
setTimeout(() => {
|
||
formRef.current?.setValues({
|
||
model_name: '',
|
||
match_mode: 'exact',
|
||
resolution_rule: 'manual',
|
||
default_tier: '',
|
||
enabled: true,
|
||
});
|
||
}, 0);
|
||
};
|
||
|
||
const openEdit = (record, index) => {
|
||
const nextResolutionRule = record.resolution_rule || 'manual';
|
||
setEditingIndex(index);
|
||
setResolutionRule(nextResolutionRule);
|
||
setTierRows(
|
||
(record.tiers || []).map((tier) => ({
|
||
key: tier.key || '',
|
||
label: tier.label || '',
|
||
price_per_image: tier.price_per_image || 0,
|
||
aliases: Array.isArray(tier.aliases) ? tier.aliases.join(',') : '',
|
||
})),
|
||
);
|
||
setModalVisible(true);
|
||
setTimeout(() => {
|
||
formRef.current?.setValues({
|
||
model_name: record.model_name || '',
|
||
match_mode: record.match_mode || 'exact',
|
||
resolution_rule: nextResolutionRule,
|
||
default_tier: record.default_tier || '',
|
||
enabled: record.enabled !== false,
|
||
});
|
||
}, 0);
|
||
};
|
||
|
||
const updateTier = (index, patch) => {
|
||
setTierRows((rows) =>
|
||
rows.map((row, rowIndex) =>
|
||
rowIndex === index ? { ...row, ...patch } : row,
|
||
),
|
||
);
|
||
};
|
||
|
||
const buildConfig = (values) => {
|
||
const modelName = String(values.model_name || '').trim();
|
||
if (
|
||
configs.some(
|
||
(config, index) =>
|
||
index !== editingIndex &&
|
||
normalize(config.model_name) === normalize(modelName),
|
||
)
|
||
) {
|
||
showError(t('该模型已配置图片分档计费'));
|
||
return null;
|
||
}
|
||
if (tierRows.length === 0 || tierRows.some((row) => !normalize(row.key))) {
|
||
showError(t('每个档位都必须填写档位 key'));
|
||
return null;
|
||
}
|
||
|
||
const tiers = tierRows.map((row) => ({
|
||
key: String(row.key).trim(),
|
||
label: String(row.label || row.key).trim(),
|
||
price_per_image: Number(row.price_per_image),
|
||
aliases: String(row.aliases || '')
|
||
.split(',')
|
||
.map((alias) => alias.trim())
|
||
.filter(Boolean),
|
||
}));
|
||
if (tiers.some((tier) => !(tier.price_per_image > 0))) {
|
||
showError(t('每个档位的每张单价必须大于 0'));
|
||
return null;
|
||
}
|
||
|
||
const seen = new Set();
|
||
for (const tier of tiers) {
|
||
for (const matchValue of [tier.key, ...tier.aliases]) {
|
||
const key = normalize(matchValue);
|
||
if (seen.has(key)) {
|
||
showError(t('同一模型内的档位 key 和别名不能重复'));
|
||
return null;
|
||
}
|
||
seen.add(key);
|
||
}
|
||
}
|
||
|
||
const selectedRule = values.resolution_rule || 'manual';
|
||
const missingTier = resolutionRuleOption(selectedRule).requiredTiers.find(
|
||
(requiredTier) =>
|
||
!tiers.some((tier) => normalize(tier.key) === requiredTier),
|
||
);
|
||
if (missingTier) {
|
||
showError(t('当前识别规则要求配置 {{tier}} 档位', { tier: missingTier }));
|
||
return null;
|
||
}
|
||
|
||
const defaultTier = String(values.default_tier || tiers[0].key).trim();
|
||
if (!tiers.some((tier) => normalize(tier.key) === normalize(defaultTier))) {
|
||
showError(t('默认档位必须填写一个已有的档位 key'));
|
||
return null;
|
||
}
|
||
return {
|
||
model_name: modelName,
|
||
match_mode: values.match_mode || 'exact',
|
||
resolution_rule: selectedRule,
|
||
enabled: values.enabled !== false,
|
||
default_tier: defaultTier,
|
||
tiers,
|
||
};
|
||
};
|
||
|
||
const submit = () => {
|
||
formRef.current
|
||
?.validate()
|
||
.then(async (values) => {
|
||
const config = buildConfig(values);
|
||
if (!config) return;
|
||
const next = [...configs];
|
||
if (editingIndex >= 0) {
|
||
next[editingIndex] = config;
|
||
} else {
|
||
next.push(config);
|
||
}
|
||
if (await save(next)) setModalVisible(false);
|
||
})
|
||
.catch(() => {});
|
||
};
|
||
|
||
const remove = async (index) => {
|
||
const next = configs.filter((_, configIndex) => configIndex !== index);
|
||
return save(next);
|
||
};
|
||
|
||
const toggle = async (index, enabled) => {
|
||
const next = configs.map((config, configIndex) =>
|
||
configIndex === index ? { ...config, enabled } : config,
|
||
);
|
||
await save(next);
|
||
};
|
||
|
||
const columns = [
|
||
{
|
||
title: t('模型名称'),
|
||
dataIndex: 'model_name',
|
||
key: 'model_name',
|
||
},
|
||
{
|
||
title: t('匹配方式'),
|
||
dataIndex: 'match_mode',
|
||
key: 'match_mode',
|
||
render: (matchMode) =>
|
||
t(matchMode === 'prefix' ? '前缀匹配' : '精确匹配'),
|
||
},
|
||
{
|
||
title: t('识别规则'),
|
||
dataIndex: 'resolution_rule',
|
||
key: 'resolution_rule',
|
||
render: (rule) => t(resolutionRuleOption(rule || 'manual').label),
|
||
},
|
||
{
|
||
title: t('默认档位'),
|
||
dataIndex: 'default_tier',
|
||
key: 'default_tier',
|
||
},
|
||
{
|
||
title: t('分辨率档位'),
|
||
dataIndex: 'tiers',
|
||
key: 'tiers',
|
||
render: (tiers) => (
|
||
<Space wrap>
|
||
{(tiers || []).map((tier) => (
|
||
<Tag key={tier.key} color='blue'>
|
||
{t('{{label}}: ${{price}}/张', {
|
||
label: tier.label || tier.key,
|
||
price: tier.price_per_image,
|
||
})}
|
||
</Tag>
|
||
))}
|
||
</Space>
|
||
),
|
||
},
|
||
{
|
||
title: t('启用'),
|
||
dataIndex: 'enabled',
|
||
key: 'enabled',
|
||
render: (enabled, _record, index) => (
|
||
<TableStatusSwitch
|
||
checked={enabled !== false}
|
||
onChange={(checked) => toggle(index, checked)}
|
||
aria-label={t('启用图片分档计费')}
|
||
/>
|
||
),
|
||
},
|
||
{
|
||
title: t('操作'),
|
||
key: 'action',
|
||
width: 110,
|
||
render: (_text, record, index) => (
|
||
<Space>
|
||
<Button
|
||
aria-label={t('编辑')}
|
||
onClick={() => openEdit(record, index)}
|
||
type='link'
|
||
size='small'
|
||
>
|
||
{t('编辑')}
|
||
</Button>
|
||
<TableActionPopconfirm
|
||
title={t('确定删除此图片定价配置吗?')}
|
||
onConfirm={() => remove(index)}
|
||
>
|
||
<Button aria-label={t('删除')} type='link' size='small' danger>
|
||
{t('删除')}
|
||
</Button>
|
||
</TableActionPopconfirm>
|
||
</Space>
|
||
),
|
||
},
|
||
];
|
||
|
||
const currentRule = resolutionRuleOption(resolutionRule);
|
||
|
||
return (
|
||
<Spin spinning={loading}>
|
||
<Form>
|
||
<Form.Section text={t('图片分辨率分档计费')}>
|
||
<Typography.Text type='tertiary'>
|
||
{t(
|
||
'仅已启用且匹配的模型按分辨率档位计费,未配置模型继续使用现有固定价格或模型倍率。单价单位为美元/张,最终费用仍乘分组倍率。',
|
||
)}
|
||
</Typography.Text>
|
||
<div style={{ marginTop: 12, marginBottom: 12 }}>
|
||
<Button icon={<IconPlus />} onClick={openAdd}>
|
||
{t('添加图片计费模型')}
|
||
</Button>
|
||
</div>
|
||
<Table
|
||
columns={columns}
|
||
dataSource={configs.map((record, index) => ({
|
||
...record,
|
||
key: `${record.model_name}-${index}`,
|
||
}))}
|
||
rowKey='key'
|
||
pagination={false}
|
||
empty={t('暂无图片分档计费配置')}
|
||
/>
|
||
</Form.Section>
|
||
</Form>
|
||
|
||
<Modal
|
||
title={editingIndex >= 0 ? t('编辑图片定价') : t('添加图片定价')}
|
||
visible={modalVisible}
|
||
onOk={submit}
|
||
onCancel={() => setModalVisible(false)}
|
||
okText={t('保存')}
|
||
cancelText={t('取消')}
|
||
width={760}
|
||
confirmLoading={loading}
|
||
>
|
||
<Form
|
||
getFormApi={(formApi) => (formRef.current = formApi)}
|
||
initValues={{
|
||
model_name: '',
|
||
match_mode: 'exact',
|
||
resolution_rule: 'manual',
|
||
default_tier: '',
|
||
enabled: true,
|
||
}}
|
||
>
|
||
<Form.Input
|
||
field='model_name'
|
||
label={t('模型名称')}
|
||
placeholder={t('例如 gpt-image-2-count')}
|
||
rules={[{ required: true, message: t('请输入模型名称') }]}
|
||
extraText={t('填写平台对外暴露的模型名称。')}
|
||
/>
|
||
<Row gutter={12}>
|
||
<Col span={12}>
|
||
<Form.Select
|
||
field='match_mode'
|
||
label={t('模型匹配方式')}
|
||
extraText={t('精确匹配不会影响同名前缀的其他模型。')}
|
||
>
|
||
{MATCH_MODE_OPTIONS.map((option) => (
|
||
<Form.Select.Option key={option.value} value={option.value}>
|
||
{t(option.label)}
|
||
</Form.Select.Option>
|
||
))}
|
||
</Form.Select>
|
||
</Col>
|
||
<Col span={12}>
|
||
<Form.Select
|
||
field='resolution_rule'
|
||
label={t('分辨率识别规则')}
|
||
onChange={(value) => setResolutionRule(value)}
|
||
extraText={t(currentRule.help)}
|
||
>
|
||
{RESOLUTION_RULE_OPTIONS.map((option) => (
|
||
<Form.Select.Option key={option.value} value={option.value}>
|
||
{t(option.label)}
|
||
</Form.Select.Option>
|
||
))}
|
||
</Form.Select>
|
||
</Col>
|
||
</Row>
|
||
<Form.Input
|
||
field='default_tier'
|
||
label={t('默认档位')}
|
||
placeholder={t('留空时使用第一个档位')}
|
||
extraText={t(
|
||
'请求未提供尺寸或传入 auto 时使用;自动规则下,非法的显式尺寸会直接返回 invalid_size。',
|
||
)}
|
||
/>
|
||
<Form.Switch field='enabled' label={t('启用')} />
|
||
</Form>
|
||
|
||
<div
|
||
style={{
|
||
marginTop: 8,
|
||
padding: '8px 12px',
|
||
borderRadius: 6,
|
||
background: 'var(--ui-color-fill-0)',
|
||
color: 'var(--ui-color-text-2)',
|
||
fontSize: 12,
|
||
lineHeight: 1.6,
|
||
}}
|
||
>
|
||
<div>
|
||
{currentRule.requiredTiers.length > 0
|
||
? t('当前规则必须配置档位:{{tiers}}', {
|
||
tiers: currentRule.requiredTiers.join('、'),
|
||
})
|
||
: t('手工规则可自定义档位 key,并通过别名匹配请求尺寸。')}
|
||
</div>
|
||
<div>
|
||
{t('别名始终优先于自动识别,可用于覆盖个别尺寸的计费档位。')}
|
||
</div>
|
||
<div>{t('自定义宽高按总像素向上取最近档位,避免低档计费。')}</div>
|
||
</div>
|
||
|
||
<div style={{ marginTop: 14 }}>
|
||
<Typography.Text strong>{t('档位列表')}</Typography.Text>
|
||
{tierRows.map((row, index) => (
|
||
<Row
|
||
key={index}
|
||
gutter={8}
|
||
type='flex'
|
||
align='middle'
|
||
style={{ marginTop: 8 }}
|
||
>
|
||
<Col span={5}>
|
||
<Input
|
||
value={row.key}
|
||
placeholder={t('档位 key')}
|
||
onChange={(value) => updateTier(index, { key: value })}
|
||
/>
|
||
</Col>
|
||
<Col span={5}>
|
||
<Input
|
||
value={row.label}
|
||
placeholder={t('显示名称')}
|
||
onChange={(value) => updateTier(index, { label: value })}
|
||
/>
|
||
</Col>
|
||
<Col span={5}>
|
||
<InputNumber
|
||
value={row.price_per_image}
|
||
placeholder={t('单价/张')}
|
||
prefix='$'
|
||
min={0}
|
||
step={0.01}
|
||
style={{ width: '100%' }}
|
||
onChange={(value) =>
|
||
updateTier(index, { price_per_image: value })
|
||
}
|
||
/>
|
||
</Col>
|
||
<Col span={7}>
|
||
<Input
|
||
value={row.aliases}
|
||
placeholder={t('别名,英文逗号分隔')}
|
||
onChange={(value) => updateTier(index, { aliases: value })}
|
||
/>
|
||
</Col>
|
||
<Col span={2}>
|
||
<Button
|
||
icon={<IconDelete />}
|
||
size='small'
|
||
type='danger'
|
||
theme='borderless'
|
||
disabled={tierRows.length === 1}
|
||
aria-label={t('删除档位')}
|
||
onClick={() =>
|
||
setTierRows((rows) =>
|
||
rows.filter((_, rowIndex) => rowIndex !== index),
|
||
)
|
||
}
|
||
/>
|
||
</Col>
|
||
</Row>
|
||
))}
|
||
<Button
|
||
icon={<IconPlus />}
|
||
size='small'
|
||
style={{ marginTop: 10 }}
|
||
onClick={() => setTierRows((rows) => [...rows, emptyTier()])}
|
||
>
|
||
{t('添加档位')}
|
||
</Button>
|
||
</div>
|
||
</Modal>
|
||
</Spin>
|
||
);
|
||
}
|