Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,327 @@
|
||||
/*
|
||||
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, { useState, useEffect } from 'react';
|
||||
import {
|
||||
Form,
|
||||
Button,
|
||||
Table,
|
||||
Modal,
|
||||
Space,
|
||||
Card,
|
||||
Typography,
|
||||
Tag,
|
||||
Popconfirm,
|
||||
} from '@douyinfe/semi-ui';
|
||||
import { IconPlus, IconEdit, IconDelete } from '@douyinfe/semi-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);
|
||||
|
||||
useEffect(() => {
|
||||
loadOptions();
|
||||
}, [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),
|
||||
percent: Math.round(100 - v * 100),
|
||||
}),
|
||||
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
|
||||
size='small'
|
||||
icon={<IconEdit />}
|
||||
onClick={() => handleEdit(record)}
|
||||
>
|
||||
{t('编辑')}
|
||||
</Button>
|
||||
<Popconfirm
|
||||
title={t('确定删除该档位吗?')}
|
||||
onConfirm={() => handleDelete(record.id)}
|
||||
>
|
||||
<Button size='small' type='danger' icon={<IconDelete />}>
|
||||
{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 style={{ padding: '20px' }}>
|
||||
<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;
|
||||
Reference in New Issue
Block a user