Files
monkey-ai-web/src/pages/Setting/AmountOptionsManager.jsx
T
zhanjunhao 92380ebab6 chore(tooling): 对齐协作规范与浏览器验收工具
验证:713 项单测、8 视口环境、代码及格式检查通过;未运行生产构建。
2026-11-17 14:48:50 +08:00

325 lines
9.1 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
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 { useEffectEvent } from '../../hooks/common/useEffectEvent';
import React, { useState, useEffect } from 'react';
import {
Form,
Button,
Table,
Modal,
Space,
Card,
Typography,
Tag,
Popconfirm,
} from '@/components/ui';
import { IconPlus } from '@/components/ui/icons';
import { useTranslation } from 'react-i18next';
import { API, showError, showSuccess } from '../../helpers';
const { Title } = Typography;
const AmountOptionsManager = ({
apiPath = '/api/user/payment/amount-options',
title = '充值档位管理',
}) => {
const { t } = useTranslation();
const [options, setOptions] = useState([]);
const [loading, setLoading] = useState(false);
const [editModal, setEditModal] = useState({ visible: false, data: null });
const [, setFormApi] = useState(null);
const onScopeChange = useEffectEvent(() => {
loadOptions();
});
useEffect(() => onScopeChange(), [onScopeChange, apiPath]);
const loadOptions = async () => {
try {
setLoading(true);
const res = await API.get(apiPath);
if (res.data.success) {
setOptions(res.data.data || []);
} else {
showError(t('加载失败'));
}
} catch (error) {
showError(t('加载失败'));
} finally {
setLoading(false);
}
};
const columns = [
{ title: 'ID', dataIndex: 'id', width: 60 },
{
title: t('金额'),
dataIndex: 'amount',
render: (v) => `¥${v}`,
width: 100,
},
{
title: t('折扣'),
dataIndex: 'discount',
render: (v) =>
t('{{rate}}折', {
rate: (v * 10).toFixed(1),
}),
width: 80,
},
{ title: t('标签'), dataIndex: 'label', width: 100 },
{
title: t('角标'),
dataIndex: 'badge',
render: (text, record) => {
if (!text) return '-';
return <Tag color={record.badge_color || 'blue'}>{text}</Tag>;
},
width: 100,
},
{
title: t('赠送额度'),
dataIndex: 'bonus',
render: (v) => (v > 0 ? v.toLocaleString() : '-'),
width: 120,
},
{
title: t('标记'),
render: (text, record) => (
<Space>
{record.is_recommend && <Tag color='blue'>{t('推荐')}</Tag>}
{record.is_hot && <Tag color='red'>{t('热门')}</Tag>}
{record.min_vip_level > 0 && (
<Tag color='purple'>VIP{record.min_vip_level}+</Tag>
)}
</Space>
),
width: 150,
},
{ title: t('排序'), dataIndex: 'sort', width: 70 },
{
title: t('操作'),
render: (text, record) => (
<Space>
<Button onClick={() => handleEdit(record)} type='link' size='small'>
{t('编辑')}
</Button>
<Popconfirm
title={t('确定删除该档位吗?')}
onConfirm={() => handleDelete(record.id)}
>
<Button type='link' size='small' danger>
{t('删除')}
</Button>
</Popconfirm>
</Space>
),
width: 150,
},
];
const handleEdit = (record) => {
setEditModal({ visible: true, data: record });
};
const handleDelete = async (id) => {
const newOptions = options.filter((opt) => opt.id !== id);
await saveOptions(newOptions);
};
const handleAdd = () => {
setEditModal({
visible: true,
data: {
id: Math.max(...options.map((o) => o.id), 0) + 1,
amount: 10,
discount: 1.0,
label: '',
badge: '',
badge_color: '#1890ff',
is_recommend: false,
is_hot: false,
min_vip_level: 0,
bonus: 0,
sort: options.length + 1,
},
});
};
const handleSave = async (values) => {
try {
setLoading(true);
const newOptions =
editModal.data.id && options.find((o) => o.id === editModal.data.id)
? options.map((opt) =>
opt.id === editModal.data.id ? { ...opt, ...values } : opt,
)
: [...options, { ...values, id: editModal.data.id }];
await saveOptions(newOptions);
setEditModal({ visible: false, data: null });
} catch (error) {
showError(t('保存失败'));
} finally {
setLoading(false);
}
};
const saveOptions = async (newOptions) => {
try {
const res = await API.post(apiPath, {
amount_options: newOptions.sort((a, b) => a.sort - b.sort),
});
if (res.data.success) {
showSuccess(t('保存成功'));
loadOptions();
} else {
showError(res.data.message);
}
} catch (error) {
showError(t('保存失败'));
}
};
return (
<div>
<Card>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
marginBottom: 20,
}}
>
<Title heading={3}>{t(title)}</Title>
<Button type='primary' icon={<IconPlus />} onClick={handleAdd}>
{t('新增档位')}
</Button>
</div>
<Table
// 列宽合计远超窄屏视口;没有 scroll 时表格会压缩到 min-content 硬塞进去,
// 末尾的操作列落在视口外,且 iOS 上滚动条不可见、毫无可拖动提示
scroll={{ x: 'max-content' }}
columns={columns}
dataSource={options}
rowKey='id'
loading={loading}
pagination={false}
/>
</Card>
{/* 编辑/新增弹窗 */}
<Modal
title={
editModal.data?.id && options.find((o) => o.id === editModal.data.id)
? t('编辑档位')
: t('新增档位')
}
visible={editModal.visible}
onCancel={() => setEditModal({ visible: false, data: null })}
footer={null}
width={600}
>
<Form
initValues={editModal.data || {}}
onSubmit={handleSave}
getFormApi={(api) => setFormApi(api)}
>
<Form.InputNumber
field='amount'
label={t('充值金额(元)')}
rules={[{ required: true, message: t('请输入金额') }]}
min={1}
style={{ width: '100%' }}
/>
<Form.InputNumber
field='discount'
label={t('折扣率')}
rules={[{ required: true, message: t('请输入折扣率') }]}
step={0.01}
min={0.01}
max={1}
style={{ width: '100%' }}
extraText={t('1.0 = 无折扣,0.9 = 9折,0.01 = 1折')}
/>
<Form.Input
field='label'
label={t('显示标签')}
rules={[{ required: true, message: t('请输入标签') }]}
placeholder={t('如:10元')}
/>
<Form.Input
field='badge'
label={t('角标文字')}
placeholder={t('如:最划算(可选)')}
/>
<Form.Input
field='badge_color'
label={t('角标颜色')}
placeholder='#1890ff'
/>
<Form.InputNumber
field='bonus'
label={t('赠送额度')}
min={0}
style={{ width: '100%' }}
extraText={t('额外赠送的额度(0 = 不赠送)')}
/>
<Form.InputNumber
field='min_vip_level'
label={t('最低VIP等级')}
min={0}
style={{ width: '100%' }}
extraText={t('0 = 无限制')}
/>
<Form.Switch field='is_recommend' label={t('推荐标记')} />
<Form.Switch field='is_hot' label={t('热门标记')} />
<Form.InputNumber
field='sort'
label={t('排序')}
rules={[{ required: true, message: t('请输入排序') }]}
min={1}
style={{ width: '100%' }}
extraText={t('数字越小越靠前')}
/>
<div style={{ marginTop: 20, textAlign: 'right' }}>
<Button
htmlType='button'
style={{ marginRight: 8 }}
onClick={() => setEditModal({ visible: false, data: null })}
>
{t('取消')}
</Button>
<Button htmlType='submit' type='primary' loading={loading}>
{t('保存')}
</Button>
</div>
</Form>
</Modal>
</div>
);
};
export default AmountOptionsManager;