fix(优惠券管理):新增优惠券管理功能模块
This commit is contained in:
@@ -0,0 +1,449 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import {
|
||||
Card,
|
||||
Table,
|
||||
Button,
|
||||
Space,
|
||||
Modal,
|
||||
Form,
|
||||
Input,
|
||||
Select,
|
||||
DatePicker,
|
||||
InputNumber,
|
||||
message,
|
||||
Popconfirm,
|
||||
Tag,
|
||||
Tooltip,
|
||||
} from 'antd';
|
||||
import { PlusOutlined, EditOutlined, DeleteOutlined, EyeOutlined } from '@ant-design/icons';
|
||||
import { getCouponList, createCoupon, updateCoupon, deleteCoupon, getCouponById } from '@/services/orders/api';
|
||||
import moment from 'moment';
|
||||
|
||||
const { Option } = Select;
|
||||
const { RangePicker } = DatePicker;
|
||||
|
||||
const CouponManagement: React.FC = () => {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [dataSource, setDataSource] = useState<ORDERS.Coupon[]>([]);
|
||||
const [total, setTotal] = useState(0);
|
||||
const [current, setCurrent] = useState(1);
|
||||
const [pageSize, setPageSize] = useState(10);
|
||||
const [modalVisible, setModalVisible] = useState(false);
|
||||
const [editingCoupon, setEditingCoupon] = useState<ORDERS.Coupon | null>(null);
|
||||
const [form] = Form.useForm();
|
||||
|
||||
// 优惠券类型选项
|
||||
const couponTypeOptions = [
|
||||
{ label: '满减券', value: 1 },
|
||||
{ label: '折扣券', value: 2 },
|
||||
{ label: '免费券', value: 3 },
|
||||
];
|
||||
|
||||
// 优惠券状态选项
|
||||
const statusOptions = [
|
||||
{ label: '未使用', value: 0 },
|
||||
{ label: '已使用', value: 1 },
|
||||
{ label: '已过期', value: 2 },
|
||||
{ label: '已作废', value: 3 },
|
||||
];
|
||||
|
||||
// 来源选项
|
||||
const sourceOptions = [
|
||||
{ label: '携程', value: 1 },
|
||||
{ label: '高飞自建', value: 2 },
|
||||
];
|
||||
|
||||
// 获取优惠券列表
|
||||
const fetchCouponList = async (params?: any) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const result = await getCouponList({
|
||||
current: params?.current || current,
|
||||
pageSize: params?.pageSize || pageSize,
|
||||
...params,
|
||||
});
|
||||
setDataSource(result.data || []);
|
||||
setTotal(result.total || 0);
|
||||
} catch (error) {
|
||||
message.error('获取优惠券列表失败');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchCouponList();
|
||||
}, []);
|
||||
|
||||
// 表格列定义
|
||||
const columns = [
|
||||
{
|
||||
title: '优惠券编号',
|
||||
dataIndex: 'couponNo',
|
||||
key: 'couponNo',
|
||||
width: 150,
|
||||
},
|
||||
{
|
||||
title: '优惠券名称',
|
||||
dataIndex: 'couponName',
|
||||
key: 'couponName',
|
||||
width: 200,
|
||||
},
|
||||
{
|
||||
title: '优惠券类型',
|
||||
dataIndex: 'couponType',
|
||||
key: 'couponType',
|
||||
width: 100,
|
||||
render: (type: number) => {
|
||||
const option = couponTypeOptions.find(item => item.value === type);
|
||||
return option ? option.label : '-';
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '面额/折扣',
|
||||
key: 'amount',
|
||||
width: 120,
|
||||
render: (record: ORDERS.Coupon) => {
|
||||
if (record.couponType === 2) {
|
||||
return `${record.discountRate}%`;
|
||||
}
|
||||
return `¥${record.amount}`;
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '使用门槛',
|
||||
dataIndex: 'minAmount',
|
||||
key: 'minAmount',
|
||||
width: 100,
|
||||
render: (amount: number) => amount ? `¥${amount}` : '-',
|
||||
},
|
||||
{
|
||||
title: '状态',
|
||||
dataIndex: 'status',
|
||||
key: 'status',
|
||||
width: 100,
|
||||
render: (status: number) => {
|
||||
const option = statusOptions.find(item => item.value === status);
|
||||
const color = status === 0 ? 'green' : status === 1 ? 'blue' : status === 2 ? 'orange' : 'red';
|
||||
return option ? <Tag color={color}>{option.label}</Tag> : '-';
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '用户手机号',
|
||||
dataIndex: 'userPhone',
|
||||
key: 'userPhone',
|
||||
width: 130,
|
||||
},
|
||||
{
|
||||
title: '来源',
|
||||
dataIndex: 'source',
|
||||
key: 'source',
|
||||
width: 100,
|
||||
render: (source: number) => {
|
||||
const option = sourceOptions.find(item => item.value === source);
|
||||
return option ? option.label : '-';
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '有效期',
|
||||
key: 'validTime',
|
||||
width: 200,
|
||||
render: (record: ORDERS.Coupon) => (
|
||||
<div>
|
||||
<div>{moment(record.validStartTime).format('YYYY-MM-DD HH:mm')}</div>
|
||||
<div>至</div>
|
||||
<div>{moment(record.validEndTime).format('YYYY-MM-DD HH:mm')}</div>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: '创建时间',
|
||||
dataIndex: 'createTime',
|
||||
key: 'createTime',
|
||||
width: 150,
|
||||
render: (time: string) => time ? moment(time).format('YYYY-MM-DD HH:mm') : '-',
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
width: 150,
|
||||
fixed: 'right' as const,
|
||||
render: (record: ORDERS.Coupon) => (
|
||||
<Space size="small">
|
||||
<Tooltip title="查看详情">
|
||||
<Button
|
||||
type="link"
|
||||
size="small"
|
||||
icon={<EyeOutlined />}
|
||||
onClick={() => handleView(record)}
|
||||
/>
|
||||
</Tooltip>
|
||||
<Tooltip title="编辑">
|
||||
<Button
|
||||
type="link"
|
||||
size="small"
|
||||
icon={<EditOutlined />}
|
||||
onClick={() => handleEdit(record)}
|
||||
/>
|
||||
</Tooltip>
|
||||
<Popconfirm
|
||||
title="确定要删除这个优惠券吗?"
|
||||
onConfirm={() => handleDelete(record.id)}
|
||||
okText="确定"
|
||||
cancelText="取消"
|
||||
>
|
||||
<Tooltip title="删除">
|
||||
<Button
|
||||
type="link"
|
||||
size="small"
|
||||
danger
|
||||
icon={<DeleteOutlined />}
|
||||
/>
|
||||
</Tooltip>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
// 查看详情
|
||||
const handleView = (record: ORDERS.Coupon) => {
|
||||
setEditingCoupon(record);
|
||||
setModalVisible(true);
|
||||
};
|
||||
|
||||
// 编辑
|
||||
const handleEdit = (record: ORDERS.Coupon) => {
|
||||
setEditingCoupon(record);
|
||||
form.setFieldsValue({
|
||||
...record,
|
||||
validTime: [moment(record.validStartTime), moment(record.validEndTime)],
|
||||
});
|
||||
setModalVisible(true);
|
||||
};
|
||||
|
||||
// 删除
|
||||
const handleDelete = async (id: number) => {
|
||||
try {
|
||||
await deleteCoupon(id);
|
||||
message.success('删除成功');
|
||||
fetchCouponList();
|
||||
} catch (error) {
|
||||
message.error('删除失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 新增
|
||||
const handleAdd = () => {
|
||||
setEditingCoupon(null);
|
||||
form.resetFields();
|
||||
setModalVisible(true);
|
||||
};
|
||||
|
||||
// 提交表单
|
||||
const handleSubmit = async () => {
|
||||
try {
|
||||
const values = await form.validateFields();
|
||||
const [validStartTime, validEndTime] = values.validTime;
|
||||
|
||||
const couponData = {
|
||||
...values,
|
||||
validStartTime: validStartTime.format('YYYY-MM-DD HH:mm:ss'),
|
||||
validEndTime: validEndTime.format('YYYY-MM-DD HH:mm:ss'),
|
||||
};
|
||||
|
||||
if (editingCoupon) {
|
||||
await updateCoupon(editingCoupon.id, couponData);
|
||||
message.success('更新成功');
|
||||
} else {
|
||||
await createCoupon(couponData);
|
||||
message.success('创建成功');
|
||||
}
|
||||
|
||||
setModalVisible(false);
|
||||
fetchCouponList();
|
||||
} catch (error) {
|
||||
message.error('操作失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 表格分页变化
|
||||
const handleTableChange = (pagination: any) => {
|
||||
setCurrent(pagination.current);
|
||||
setPageSize(pagination.pageSize);
|
||||
fetchCouponList({
|
||||
current: pagination.current,
|
||||
pageSize: pagination.pageSize,
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<Card title="优惠券管理" extra={
|
||||
<Button type="primary" icon={<PlusOutlined />} onClick={handleAdd}>
|
||||
新增优惠券
|
||||
</Button>
|
||||
}>
|
||||
<Table
|
||||
columns={columns}
|
||||
dataSource={dataSource}
|
||||
loading={loading}
|
||||
rowKey="id"
|
||||
pagination={{
|
||||
current,
|
||||
pageSize,
|
||||
total,
|
||||
showSizeChanger: true,
|
||||
showQuickJumper: true,
|
||||
showTotal: (total) => `共 ${total} 条记录`,
|
||||
}}
|
||||
onChange={handleTableChange}
|
||||
scroll={{ x: 1500 }}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
title={editingCoupon ? '编辑优惠券' : '新增优惠券'}
|
||||
open={modalVisible}
|
||||
onOk={handleSubmit}
|
||||
onCancel={() => setModalVisible(false)}
|
||||
width={600}
|
||||
destroyOnClose
|
||||
>
|
||||
<Form
|
||||
form={form}
|
||||
layout="vertical"
|
||||
initialValues={{
|
||||
couponType: 1,
|
||||
status: 0,
|
||||
source: 2,
|
||||
}}
|
||||
>
|
||||
<Form.Item
|
||||
name="couponName"
|
||||
label="优惠券名称"
|
||||
rules={[{ required: true, message: '请输入优惠券名称' }]}
|
||||
>
|
||||
<Input placeholder="请输入优惠券名称" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
name="couponType"
|
||||
label="优惠券类型"
|
||||
rules={[{ required: true, message: '请选择优惠券类型' }]}
|
||||
>
|
||||
<Select placeholder="请选择优惠券类型">
|
||||
{couponTypeOptions.map(option => (
|
||||
<Option key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</Option>
|
||||
))}
|
||||
</Select>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
noStyle
|
||||
shouldUpdate={(prevValues, currentValues) => prevValues.couponType !== currentValues.couponType}
|
||||
>
|
||||
{({ getFieldValue }) => {
|
||||
const couponType = getFieldValue('couponType');
|
||||
return couponType === 2 ? (
|
||||
<Form.Item
|
||||
name="discountRate"
|
||||
label="折扣率(%)"
|
||||
rules={[{ required: true, message: '请输入折扣率' }]}
|
||||
>
|
||||
<InputNumber
|
||||
min={1}
|
||||
max={99}
|
||||
placeholder="请输入折扣率"
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
</Form.Item>
|
||||
) : (
|
||||
<Form.Item
|
||||
name="amount"
|
||||
label="优惠券面额"
|
||||
rules={[{ required: true, message: '请输入优惠券面额' }]}
|
||||
>
|
||||
<InputNumber
|
||||
min={0}
|
||||
placeholder="请输入优惠券面额"
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
</Form.Item>
|
||||
);
|
||||
}}
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
name="minAmount"
|
||||
label="使用门槛金额"
|
||||
>
|
||||
<InputNumber
|
||||
min={0}
|
||||
placeholder="请输入使用门槛金额"
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
name="userPhone"
|
||||
label="用户手机号"
|
||||
rules={[{ required: true, message: '请输入用户手机号' }]}
|
||||
>
|
||||
<Input placeholder="请输入用户手机号" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
name="source"
|
||||
label="来源"
|
||||
rules={[{ required: true, message: '请选择来源' }]}
|
||||
>
|
||||
<Select placeholder="请选择来源">
|
||||
{sourceOptions.map(option => (
|
||||
<Option key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</Option>
|
||||
))}
|
||||
</Select>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
name="ctripCouponId"
|
||||
label="携程优惠券ID"
|
||||
>
|
||||
<Input placeholder="请输入携程优惠券ID" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
name="validTime"
|
||||
label="有效期"
|
||||
rules={[{ required: true, message: '请选择有效期' }]}
|
||||
>
|
||||
<RangePicker
|
||||
showTime
|
||||
format="YYYY-MM-DD HH:mm:ss"
|
||||
placeholder={['开始时间', '结束时间']}
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
name="remark"
|
||||
label="备注"
|
||||
>
|
||||
<Input.TextArea rows={3} placeholder="请输入备注" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
name="tenantId"
|
||||
label="租户ID"
|
||||
>
|
||||
<Input placeholder="请输入租户ID" />
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</Card>
|
||||
);
|
||||
};
|
||||
|
||||
export default CouponManagement;
|
||||
@@ -339,3 +339,97 @@ export async function getWxMessageStatistics(params: {
|
||||
});
|
||||
}
|
||||
|
||||
// 优惠券相关接口
|
||||
export async function getCouponList(params?: { [key: string]: string | number }) {
|
||||
const { current, pageSize, ...rest } = params ?? {};
|
||||
const result = await request<SYSTEM.Page<ORDERS.Coupon>>('/api-order/coupon/page', {
|
||||
method: 'GET',
|
||||
params: { current, size: pageSize, ...rest },
|
||||
});
|
||||
return { data: result.data, total: result.total };
|
||||
}
|
||||
|
||||
export async function getCouponById(id: number) {
|
||||
const result = await request<API.Result<ORDERS.Coupon>>(`/api-order/coupon/${id}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function createCoupon(data: ORDERS.Coupon) {
|
||||
const result = await request<API.Result<boolean>>('/api-order/coupon', {
|
||||
method: 'POST',
|
||||
data,
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function updateCoupon(id: number, data: ORDERS.Coupon) {
|
||||
const result = await request<API.Result<boolean>>(`/api-order/coupon/${id}`, {
|
||||
method: 'PUT',
|
||||
data,
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function deleteCoupon(id: number) {
|
||||
const result = await request<API.Result<boolean>>(`/api-order/coupon/${id}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getCouponsByUserPhone(userPhone: string) {
|
||||
const result = await request<API.Result<ORDERS.Coupon[]>>(`/api-order/coupon/user/${userPhone}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getAvailableCouponsByUserPhone(userPhone: string) {
|
||||
const result = await request<API.Result<ORDERS.Coupon[]>>(`/api-order/coupon/user/${userPhone}/available`, {
|
||||
method: 'GET',
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function useCoupon(id: number, orderNo: string) {
|
||||
const result = await request<API.Result<boolean>>(`/api-order/coupon/${id}/use`, {
|
||||
method: 'POST',
|
||||
params: { orderNo },
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function createCouponFromCtrip(params: {
|
||||
ctripCouponId: string;
|
||||
userPhone: string;
|
||||
couponName: string;
|
||||
couponType: number;
|
||||
amount: number;
|
||||
minAmount?: number;
|
||||
discountRate?: number;
|
||||
validStartTime: string;
|
||||
validEndTime: string;
|
||||
}) {
|
||||
const result = await request<API.Result<boolean>>('/api-order/coupon/ctrip/create', {
|
||||
method: 'POST',
|
||||
params,
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function verifyCtripCoupon(ctripCouponId: string, orderNo: string) {
|
||||
const result = await request<API.Result<boolean>>('/api-order/coupon/ctrip/verify', {
|
||||
method: 'POST',
|
||||
params: { ctripCouponId, orderNo },
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function updateExpiredCoupons() {
|
||||
const result = await request<API.Result<void>>('/api-order/coupon/update-expired', {
|
||||
method: 'POST',
|
||||
});
|
||||
return result;
|
||||
}
|
||||
@@ -98,7 +98,31 @@ declare namespace ORDERS {
|
||||
approveReason: string;
|
||||
createTime?: string;
|
||||
tenantId?: string;
|
||||
}
|
||||
|
||||
type Coupon = {
|
||||
id: number;
|
||||
couponNo: string;
|
||||
couponName: string;
|
||||
couponType: number; // 1-满减券,2-折扣券,3-免费券
|
||||
amount: number;
|
||||
minAmount?: number;
|
||||
discountRate?: number;
|
||||
status: number; // 0-未使用,1-已使用,2-已过期,3-已作废
|
||||
userPhone: string;
|
||||
userId?: number;
|
||||
source: number; // 1-携程,2-高飞自建
|
||||
ctripCouponId?: string;
|
||||
validStartTime: string;
|
||||
validEndTime: string;
|
||||
usedTime?: string;
|
||||
usedOrderNo?: string;
|
||||
createTime?: string;
|
||||
updateTime?: string;
|
||||
tenantId?: string;
|
||||
createBy?: string;
|
||||
updateBy?: string;
|
||||
remark?: string;
|
||||
}
|
||||
|
||||
// 添加 Dashboard 相关类型
|
||||
|
||||
Reference in New Issue
Block a user