191 lines
4.5 KiB
Markdown
191 lines
4.5 KiB
Markdown
# 前端核销传参方案
|
||
|
||
## 核销接口参数结构
|
||
|
||
```json
|
||
{
|
||
"otaOrderId": "ctriptest-10816870-454259-145573",
|
||
"itemId": "106943568",
|
||
"useQuantity": 1,
|
||
"remark": "门票入园核销"
|
||
}
|
||
```
|
||
|
||
## 前端传参方案
|
||
|
||
### 方案一:直接使用订单项ID
|
||
|
||
**查询接口返回的订单项数据:**
|
||
```json
|
||
{
|
||
"orderItems": [
|
||
{
|
||
"itemId": 456, // 数据库主键ID
|
||
"productId": "106943568", // 商品ID(携程的itemId)
|
||
"itemName": "门票",
|
||
"plu": "1",
|
||
"quantity": 2,
|
||
"canConsume": true,
|
||
"availableQuantity": 2
|
||
}
|
||
]
|
||
}
|
||
```
|
||
|
||
**前端传参:**
|
||
```javascript
|
||
// 选择第一个可核销的订单项
|
||
const orderItem = order.orderItems.find(item => item.canConsume);
|
||
const consumeParams = {
|
||
otaOrderId: order.otaOrderId,
|
||
itemId: orderItem.productId, // 使用productId作为itemId
|
||
useQuantity: 1, // 核销数量
|
||
remark: "使用优惠券核销门票"
|
||
};
|
||
```
|
||
|
||
### 方案二:支持批量核销
|
||
|
||
如果需要支持一次核销多个订单项,可以这样设计:
|
||
|
||
**前端传参:**
|
||
```javascript
|
||
// 核销多个订单项
|
||
const consumeParams = {
|
||
otaOrderId: order.otaOrderId,
|
||
items: [
|
||
{
|
||
itemId: "106943568",
|
||
useQuantity: 1,
|
||
remark: "核销门票1张"
|
||
},
|
||
{
|
||
itemId: "106943569",
|
||
useQuantity: 1,
|
||
remark: "核销门票1张"
|
||
}
|
||
]
|
||
};
|
||
```
|
||
|
||
## 推荐实现方案
|
||
|
||
### 1. 使用简化核销接口
|
||
|
||
**接口地址:** `POST /api/order-detail/consume-simple`
|
||
|
||
**请求参数:**
|
||
```json
|
||
{
|
||
"otaOrderId": "ctriptest-10816870-454259-145573",
|
||
"itemId": "106943568",
|
||
"useQuantity": 1,
|
||
"remark": "使用优惠券核销门票"
|
||
}
|
||
```
|
||
|
||
### 2. 前端实现示例
|
||
|
||
```typescript
|
||
interface SimpleConsumeRequest {
|
||
otaOrderId: string;
|
||
itemId: string;
|
||
useQuantity: number;
|
||
remark?: string;
|
||
}
|
||
|
||
// 简化核销订单
|
||
async function consumeOrderSimple(request: SimpleConsumeRequest) {
|
||
const response = await fetch('/api/order-detail/consume-simple', {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify(request)
|
||
});
|
||
|
||
return response.json();
|
||
}
|
||
|
||
// 使用示例
|
||
function handleConsume() {
|
||
const orderItem = order.orderItems.find(item => item.canConsume);
|
||
|
||
if (orderItem) {
|
||
const consumeRequest: SimpleConsumeRequest = {
|
||
otaOrderId: order.otaOrderId,
|
||
itemId: orderItem.productId, // 使用productId作为itemId
|
||
useQuantity: 1, // 核销数量
|
||
remark: `使用优惠券核销${orderItem.itemName}`
|
||
};
|
||
|
||
consumeOrderSimple(consumeRequest);
|
||
}
|
||
}
|
||
```
|
||
|
||
### 3. 前端UI交互设计
|
||
|
||
```typescript
|
||
// 订单项选择组件
|
||
function OrderItemSelector({ orderItem, onSelect }) {
|
||
const [selectedQuantity, setSelectedQuantity] = useState(0);
|
||
const maxQuantity = orderItem.availableQuantity;
|
||
|
||
return (
|
||
<div className="order-item">
|
||
<div className="item-info">
|
||
<span>{orderItem.itemName}</span>
|
||
<span>可用数量: {maxQuantity}</span>
|
||
</div>
|
||
|
||
{orderItem.canConsume && (
|
||
<div className="quantity-selector">
|
||
<button
|
||
onClick={() => setSelectedQuantity(Math.max(0, selectedQuantity - 1))}
|
||
disabled={selectedQuantity <= 0}
|
||
>
|
||
-
|
||
</button>
|
||
<span>{selectedQuantity}</span>
|
||
<button
|
||
onClick={() => setSelectedQuantity(Math.min(maxQuantity, selectedQuantity + 1))}
|
||
disabled={selectedQuantity >= maxQuantity}
|
||
>
|
||
+
|
||
</button>
|
||
</div>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
// 核销按钮
|
||
function ConsumeButton({ order, selectedItems }) {
|
||
const canConsume = selectedItems.length > 0 &&
|
||
selectedItems.every(item => item.useQuantity > 0);
|
||
|
||
return (
|
||
<button
|
||
onClick={() => handleConsume(order, selectedItems)}
|
||
disabled={!canConsume}
|
||
>
|
||
确认核销
|
||
</button>
|
||
);
|
||
}
|
||
```
|
||
|
||
## 数据映射关系
|
||
|
||
| 查询接口返回字段 | 核销接口参数 | 说明 |
|
||
|----------------|-------------|------|
|
||
| `orderItems[].productId` | `itemId` | 携程的商品ID |
|
||
| `orderItems[].availableQuantity` | `useQuantity` | 可核销数量 |
|
||
| `orderItems[].itemName` | `remark` | 商品名称用于备注 |
|
||
|
||
## 注意事项
|
||
|
||
1. **itemId映射**:使用 `productId` 作为核销接口的 `itemId`
|
||
2. **数量验证**:核销数量不能超过 `availableQuantity`
|
||
3. **状态检查**:只允许核销 `canConsume=true` 的订单项
|
||
4. **批量支持**:建议支持一次核销多个订单项
|
||
5. **错误处理**:前端需要处理核销失败的情况 |