Files

8.0 KiB

订单明细查询核销接口使用示例

场景描述

用户有一张100元的优惠券,想要查询可以用这张优惠券核销的携程订单。

使用步骤

1. 查询可核销订单

请求:

curl -X POST http://localhost:8002/coupon/api/order-detail/query \
  -H "Content-Type: application/json" \
  -d '{
    "userPhone": "13800138000",
    "couponPrice": 100.00,
    "onlyConsumable": true,
    "page": 1,
    "size": 10
  }'

响应:

{
    "resp_code": 0,
    "resp_msg": "",
    "data": {
        "data": [
            {
                "orderId": 123,
                "otaOrderId": "ctriptest-10816828-454259-145573",
                "supplierOrderId": "SUPPLIER_ctriptest-10816828-454259-145573_301495ef",
                "orderStatus": 1,
                "orderStatusDesc": "已确认",
                "totalAmount": 100.00,
                "currency": "CNY",
                "createTime": "2025-08-30T09:33:42",
                "orderItems": [
                    {
                        "itemId": 456,
                        "productId": "1801589811",
                        "itemName": "门票",
                        "itemType": "1",
                        "plu": "1",
                        "quantity": 2,
                        "unitPrice": 50.00,
                        "totalPrice": 100.00,
                        "currency": "CNY",
                        "useStartDate": "2025-07-25",
                        "useEndDate": "2025-07-25",
                        "canConsume": true,
                        "usedQuantity": 0,
                        "availableQuantity": 2
                    }
                ],
                "passengers": [
                    {
                        "passengerId": "PASS_1",
                        "passengerName": "张三",
                        "phone": "13800138000",
                        "passengerStatus": 0,
                        "passengerStatusDesc": "未使用",
                        "idCard": "110101199001011234",
                        "gender": "ADULT",
                        "age": null
                    }
                ],
                "contact": {
                    "name": "张三",
                    "mobile": "13800138000",
                    "email": "zhangsan@example.com",
                    "contactType": "BOOKER"
                },
                "canConsume": true,
                "consumeReason": null
            }
        ],
        "count": 1,
        "resp_code": 0
    }
}

2. 验证订单是否可核销

请求:

curl -X GET http://localhost:8002/coupon/api/order-detail/ctriptest-10816828-454259-145573/validate

响应:

{
    "resp_code": 0,
    "resp_msg": "",
    "data": true
}

3. 执行简化核销操作

请求:

curl -X POST http://localhost:8002/coupon/api/order-detail/consume-simple \
  -H "Content-Type: application/json" \
  -d '{
    "otaOrderId": "ctriptest-10816870-454259-145573",
    "itemId": "106943568",
    "useQuantity": 1,
    "remark": "使用100元优惠券核销门票"
  }'

响应:

{
    "resp_code": 0,
    "resp_msg": "核销成功",
    "data": true
}

前端集成示例

JavaScript/TypeScript

// 查询可核销订单
async function queryConsumableOrders(userPhone: string, couponPrice: number) {
    const response = await fetch('/api/order-detail/query', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            userPhone,
            couponPrice,
            onlyConsumable: true,
            page: 1,
            size: 10
        })
    });
    
    const result = await response.json();
    return result.data;
}

// 简化核销订单
async function consumeOrderSimple(otaOrderId: string, itemId: string, useQuantity: number, remark: string) {
    const response = await fetch('/api/order-detail/consume-simple', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            otaOrderId,
            itemId,
            useQuantity,
            remark
        })
    });
    
    const result = await response.json();
    return result;
}

// 使用示例
async function useCoupon() {
    const userPhone = '13800138000';
    const couponPrice = 100.00;
    
    try {
        // 1. 查询可核销订单
        const orders = await queryConsumableOrders(userPhone, couponPrice);
        console.log('可核销订单:', orders);
        
        if (orders.data.length > 0) {
            const order = orders.data[0];
            const orderItem = order.orderItems.find(item => item.canConsume);
            
            if (orderItem) {
                // 2. 执行简化核销
                const consumeResult = await consumeOrderSimple(
                    order.otaOrderId,
                    orderItem.productId,  // 使用productId作为itemId
                    1,                    // 核销数量
                    '使用优惠券核销'
                );
                
                console.log('核销结果:', consumeResult);
            }
        }
    } catch (error) {
        console.error('操作失败:', error);
    }
}

React Hook 示例

import { useState, useEffect } from 'react';

interface OrderDetail {
    orderId: number;
    otaOrderId: string;
    supplierOrderId: string;
    totalAmount: number;
    canConsume: boolean;
    orderItems: any[];
    passengers: any[];
}

export function useConsumableOrders(userPhone: string, couponPrice: number) {
    const [orders, setOrders] = useState<OrderDetail[]>([]);
    const [loading, setLoading] = useState(false);
    const [error, setError] = useState<string | null>(null);

    const fetchOrders = async () => {
        setLoading(true);
        setError(null);
        
        try {
            const response = await fetch('/api/order-detail/query', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    userPhone,
                    couponPrice,
                    onlyConsumable: true,
                    page: 1,
                    size: 10
                })
            });
            
            const result = await response.json();
            
            if (result.resp_code === 0) {
                setOrders(result.data.data);
            } else {
                setError(result.resp_msg);
            }
        } catch (err) {
            setError('查询失败');
        } finally {
            setLoading(false);
        }
    };

    const consumeOrder = async (order: OrderDetail, itemId: string, useQuantity: number) => {
        try {
            const response = await fetch('/api/order-detail/consume-simple', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    otaOrderId: order.otaOrderId,
                    itemId: itemId,
                    useQuantity: useQuantity,
                    remark: '使用优惠券核销'
                })
            });
            
            const result = await response.json();
            return result.resp_code === 0;
        } catch (err) {
            return false;
        }
    };

    useEffect(() => {
        if (userPhone && couponPrice) {
            fetchOrders();
        }
    }, [userPhone, couponPrice]);

    return { orders, loading, error, consumeOrder, refetch: fetchOrders };
}

注意事项

  1. 优惠券价格匹配:订单总金额必须与优惠券价格完全匹配
  2. 可核销条件:只有已确认且在使用有效期内的订单才能核销
  3. 数量限制:核销数量不能超过订单项的剩余可用数量
  4. 幂等性:重复核销同一订单项会返回成功,但不会重复扣减数量
  5. 错误处理:建议在前端做好错误处理和用户提示