471d7843de
- 公共表格:统一分组标签、状态开关和危险操作确认,支持防重复提交和失败重试,修复浮层触发组件的 ref 转发。 - 控制台:统一用户和渠道编辑抽屉,修正分组回填与提交格式,对齐系统设置操作区和预约列表。 - 企业中心:调整技能中心页签和窄屏布局,优化质量监控提示栏、效能分析及成本对账,统一消耗明细和审计日志筛选。 - 用户中心及公共页面:优化密钥和消费记录展示,对齐模型超市筛选,统一公开与内嵌客户端下载页。 - 同步多语言文案、组件规范与移动端视口要求。
305 lines
8.5 KiB
React
305 lines
8.5 KiB
React
import TableActionPopconfirm from '@/components/common/table/TableActionPopconfirm';
|
||
import { useEffectEvent } from '../../hooks/common/useEffectEvent';
|
||
import React, { useState, useEffect } from 'react';
|
||
import {
|
||
Form,
|
||
Button,
|
||
Table,
|
||
Modal,
|
||
Space,
|
||
Card,
|
||
Typography,
|
||
Tag,
|
||
} 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 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>
|
||
<TableActionPopconfirm
|
||
title={t('确定删除该档位吗?')}
|
||
onConfirm={() => handleDelete(record.id)}
|
||
>
|
||
<Button type='link' size='small' danger>
|
||
{t('删除')}
|
||
</Button>
|
||
</TableActionPopconfirm>
|
||
</Space>
|
||
),
|
||
width: 150,
|
||
},
|
||
];
|
||
|
||
const handleEdit = (record) => {
|
||
setEditModal({ visible: true, data: record });
|
||
};
|
||
|
||
const handleDelete = async (id) => {
|
||
const newOptions = options.filter((opt) => opt.id !== id);
|
||
return 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();
|
||
return true;
|
||
} else {
|
||
showError(res.data.message);
|
||
}
|
||
} catch (error) {
|
||
showError(t('保存失败'));
|
||
}
|
||
return false;
|
||
};
|
||
|
||
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}
|
||
// initValues 仅在挂载时生效,关闭后销毁表单以便下次按当前行初始化。
|
||
keepDOM={false}
|
||
>
|
||
<Form initValues={editModal.data || {}} onSubmit={handleSave}>
|
||
<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;
|