feat(订单): 添加订单相关接口并集成到购物车和结算页面
添加了订单相关的API接口,包括获取订单列表、订单详情、提交订单和取消订单。在购物车上下文中集成了提交订单的API调用,替换了原有的模拟逻辑。同时,更新了结算页面的表单字段,以匹配订单API的请求参数。
Showing
3 changed files
with
85 additions
and
33 deletions
src/api/order.js
0 → 100644
| 1 | +/* | ||
| 2 | + * @Date: 2025-04-16 16:21:37 | ||
| 3 | + * @LastEditors: hookehuyr hookehuyr@gmail.com | ||
| 4 | + * @LastEditTime: 2025-04-16 16:41:16 | ||
| 5 | + * @FilePath: /mlaj/src/api/order.js | ||
| 6 | + * @Description: 订单相关接口 | ||
| 7 | + */ | ||
| 8 | +import { fn, fetch } from './fn' | ||
| 9 | + | ||
| 10 | +const Api = { | ||
| 11 | + ORDER_LIST: '/srv/?a=order_list', | ||
| 12 | + ORDER_INFO: '/srv/?a=order_info', | ||
| 13 | + ORDER_ADD: '/srv/?a=order_add', | ||
| 14 | + ORDER_CANCEL: '/srv/?a=order_cancel', | ||
| 15 | +} | ||
| 16 | + | ||
| 17 | +/** | ||
| 18 | + * @description: 获取订单列表 | ||
| 19 | + * @param: page 页码 | ||
| 20 | + * @param: limit 每页数量 | ||
| 21 | + * @param: status 订单状态 NOT_PAY=待支付,PAY=已支付,CANCEL=已取消,APPLY_REFUND=申请退款,REFUND=已退款,REFUND_ERROR=退款失败 | ||
| 22 | + * @return: data: { } | ||
| 23 | + */ | ||
| 24 | +export const getOrderListAPI = (params) => fn(fetch.get(Api.ORDER_LIST, params)) | ||
| 25 | + | ||
| 26 | +/** | ||
| 27 | + * @description: 获取订单详情 | ||
| 28 | + * @param: i 订单ID | ||
| 29 | + * @return: data: { } | ||
| 30 | + */ | ||
| 31 | +export const getOrderInfoAPI = (params) => fn(fetch.get(Api.ORDER_INFO, params)) | ||
| 32 | + | ||
| 33 | +/** | ||
| 34 | + * @description: 提交订单 | ||
| 35 | + * @param: pay_type 支付方式 Alipay=支付宝,WeChat=微信 | ||
| 36 | + * @param: total_price 订单总金额 | ||
| 37 | + * @param: receive_name 姓名 | ||
| 38 | + * @param: receive_phone 手机 | ||
| 39 | + * @param: receive_email 邮箱 | ||
| 40 | + * @param: receive_address 地址 | ||
| 41 | + * @param: note 备注 | ||
| 42 | + * @param: details 订单明细 | ||
| 43 | + * @return: data: { id: 订单ID, status: 订单状态 NOT_PAY=待支付,PAY=已支付 } | ||
| 44 | + */ | ||
| 45 | +export const addOrderAPI = (params) => fn(fetch.post(Api.ORDER_ADD, params)) | ||
| 46 | + | ||
| 47 | +/** | ||
| 48 | + * @description: 取消订单 | ||
| 49 | + * @param: i 订单ID | ||
| 50 | + * @return: data: { } | ||
| 51 | + */ | ||
| 52 | +export const cancelOrderAPI = (params) => fn(fetch.post(Api.ORDER_CANCEL, params)) |
| 1 | import { ref, provide, inject, watchEffect } from 'vue' | 1 | import { ref, provide, inject, watchEffect } from 'vue' |
| 2 | import { useRouter } from 'vue-router' | 2 | import { useRouter } from 'vue-router' |
| 3 | 3 | ||
| 4 | +// 导入接口 | ||
| 5 | +import { addOrderAPI } from "@/api/order"; | ||
| 6 | + | ||
| 4 | const CartSymbol = Symbol() | 7 | const CartSymbol = Symbol() |
| 5 | 8 | ||
| 6 | // 购物车模式配置 | 9 | // 购物车模式配置 |
| ... | @@ -109,35 +112,32 @@ export function provideCart(mode = CartMode.MULTIPLE) { | ... | @@ -109,35 +112,32 @@ export function provideCart(mode = CartMode.MULTIPLE) { |
| 109 | function handleCheckout(userData) { | 112 | function handleCheckout(userData) { |
| 110 | // 构建订单数据 | 113 | // 构建订单数据 |
| 111 | const orderData = { | 114 | const orderData = { |
| 112 | - items: cartItems.value.map(item => ({ | 115 | + details: cartItems.value.map(item => ({ |
| 113 | - id: item.id, | 116 | + good_id: item.id, |
| 114 | type: item.type, | 117 | type: item.type, |
| 115 | - quantity: item.quantity, | 118 | + number: item.quantity, |
| 116 | price: item.price, | 119 | price: item.price, |
| 117 | title: item.title | 120 | title: item.title |
| 118 | })), | 121 | })), |
| 119 | - totalAmount: getTotalPrice(), | 122 | + total_price: getTotalPrice(), |
| 120 | - userData: userData, | 123 | + ...userData, |
| 121 | orderDate: new Date().toISOString() | 124 | orderDate: new Date().toISOString() |
| 122 | } | 125 | } |
| 123 | 126 | ||
| 124 | - // TODO: 替换为实际的API调用 | 127 | + return addOrderAPI(orderData).then(response => { |
| 125 | - return new Promise((resolve) => { | 128 | + if (response.code === 1) { |
| 126 | - // 模拟API调用 | ||
| 127 | - setTimeout(() => { | ||
| 128 | - // 在实际应用中,这里应该调用后端API | ||
| 129 | - console.warn('提交订单数据:', orderData) | ||
| 130 | - | ||
| 131 | // 订单提交成功后清空购物车 | 129 | // 订单提交成功后清空购物车 |
| 132 | clearCart() | 130 | clearCart() |
| 133 | - | 131 | + return { |
| 134 | - // 返回订单ID | ||
| 135 | - resolve({ | ||
| 136 | success: true, | 132 | success: true, |
| 137 | - orderId: 'ORD-' + Date.now(), | 133 | + orderId: response.data.id, |
| 138 | orderData: orderData | 134 | orderData: orderData |
| 139 | - }) | 135 | + } |
| 140 | - }, 1500) | 136 | + } |
| 137 | + return { | ||
| 138 | + success: false, | ||
| 139 | + message: response.msg || '订单提交失败' | ||
| 140 | + } | ||
| 141 | }) | 141 | }) |
| 142 | } | 142 | } |
| 143 | 143 | ... | ... |
| ... | @@ -105,7 +105,7 @@ | ... | @@ -105,7 +105,7 @@ |
| 105 | <div> | 105 | <div> |
| 106 | <label class="block text-sm text-gray-600 mb-1">姓名 <span class="text-red-500">*</span></label> | 106 | <label class="block text-sm text-gray-600 mb-1">姓名 <span class="text-red-500">*</span></label> |
| 107 | <input | 107 | <input |
| 108 | - v-model="formData.name" | 108 | + v-model="formData.receive_name" |
| 109 | type="text" | 109 | type="text" |
| 110 | class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm" | 110 | class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm" |
| 111 | placeholder="请输入您的姓名" | 111 | placeholder="请输入您的姓名" |
| ... | @@ -116,7 +116,7 @@ | ... | @@ -116,7 +116,7 @@ |
| 116 | <div> | 116 | <div> |
| 117 | <label class="block text-sm text-gray-600 mb-1">手机号码 <span class="text-red-500">*</span></label> | 117 | <label class="block text-sm text-gray-600 mb-1">手机号码 <span class="text-red-500">*</span></label> |
| 118 | <input | 118 | <input |
| 119 | - v-model="formData.phone" | 119 | + v-model="formData.receive_phone" |
| 120 | type="tel" | 120 | type="tel" |
| 121 | class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm" | 121 | class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm" |
| 122 | placeholder="请输入您的手机号码" | 122 | placeholder="请输入您的手机号码" |
| ... | @@ -127,7 +127,7 @@ | ... | @@ -127,7 +127,7 @@ |
| 127 | <div> | 127 | <div> |
| 128 | <label class="block text-sm text-gray-600 mb-1">电子邮箱</label> | 128 | <label class="block text-sm text-gray-600 mb-1">电子邮箱</label> |
| 129 | <input | 129 | <input |
| 130 | - v-model="formData.email" | 130 | + v-model="formData.receive_email" |
| 131 | type="email" | 131 | type="email" |
| 132 | class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm" | 132 | class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm" |
| 133 | placeholder="请输入您的邮箱(选填)" | 133 | placeholder="请输入您的邮箱(选填)" |
| ... | @@ -137,7 +137,7 @@ | ... | @@ -137,7 +137,7 @@ |
| 137 | <div> | 137 | <div> |
| 138 | <label class="block text-sm text-gray-600 mb-1">联系地址 <span class="text-red-500">*</span></label> | 138 | <label class="block text-sm text-gray-600 mb-1">联系地址 <span class="text-red-500">*</span></label> |
| 139 | <input | 139 | <input |
| 140 | - v-model="formData.address" | 140 | + v-model="formData.receive_address" |
| 141 | type="text" | 141 | type="text" |
| 142 | class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm" | 142 | class="w-full px-3 py-2 border border-gray-200 rounded-lg text-sm" |
| 143 | placeholder="请输入您的详细地址" | 143 | placeholder="请输入您的详细地址" |
| ... | @@ -161,9 +161,9 @@ | ... | @@ -161,9 +161,9 @@ |
| 161 | <div class="space-y-2"> | 161 | <div class="space-y-2"> |
| 162 | <label class="flex items-center p-3 border border-gray-200 rounded-lg bg-white/50"> | 162 | <label class="flex items-center p-3 border border-gray-200 rounded-lg bg-white/50"> |
| 163 | <input | 163 | <input |
| 164 | - v-model="formData.paymentMethod" | 164 | + v-model="formData.pay_type" |
| 165 | type="radio" | 165 | type="radio" |
| 166 | - value="wechat" | 166 | + value="WeChat" |
| 167 | class="mr-3" | 167 | class="mr-3" |
| 168 | /> | 168 | /> |
| 169 | <span class="flex-1">微信支付</span> | 169 | <span class="flex-1">微信支付</span> |
| ... | @@ -283,12 +283,12 @@ const { items: cartItems, mode, getTotalPrice, handleCheckout, clearCart, remove | ... | @@ -283,12 +283,12 @@ const { items: cartItems, mode, getTotalPrice, handleCheckout, clearCart, remove |
| 283 | 283 | ||
| 284 | // Form state | 284 | // Form state |
| 285 | const formData = ref({ | 285 | const formData = ref({ |
| 286 | - name: '', | 286 | + receive_name: '', |
| 287 | - phone: '', | 287 | + receive_phone: '', |
| 288 | - email: '', | 288 | + receive_email: '', |
| 289 | - address: '', | 289 | + receive_address: '', |
| 290 | notes: '', | 290 | notes: '', |
| 291 | - paymentMethod: 'wechat' | 291 | + pay_type: 'WeChat' |
| 292 | }) | 292 | }) |
| 293 | 293 | ||
| 294 | // Loading and success states | 294 | // Loading and success states |
| ... | @@ -315,16 +315,16 @@ const handleImageError = (e) => { | ... | @@ -315,16 +315,16 @@ const handleImageError = (e) => { |
| 315 | const handleSubmit = async (e) => { | 315 | const handleSubmit = async (e) => { |
| 316 | try { | 316 | try { |
| 317 | // 表单验证 | 317 | // 表单验证 |
| 318 | - if (!formData.value.name?.trim()) { | 318 | + if (!formData.value.receive_name?.trim()) { |
| 319 | throw new Error('请输入姓名') | 319 | throw new Error('请输入姓名') |
| 320 | } | 320 | } |
| 321 | - if (!formData.value.phone?.trim()) { | 321 | + if (!formData.value.receive_phone?.trim()) { |
| 322 | throw new Error('请输入手机号码') | 322 | throw new Error('请输入手机号码') |
| 323 | } | 323 | } |
| 324 | - if (!formData.value.address?.trim()) { | 324 | + if (!formData.value.receive_address?.trim()) { |
| 325 | throw new Error('请输入联系地址') | 325 | throw new Error('请输入联系地址') |
| 326 | } | 326 | } |
| 327 | - if (!formData.value.paymentMethod) { | 327 | + if (!formData.value.pay_type) { |
| 328 | throw new Error('请选择支付方式') | 328 | throw new Error('请选择支付方式') |
| 329 | } | 329 | } |
| 330 | 330 | ... | ... |
-
Please register or login to post a comment