From 14781f442ae1ee2fec6c5007396eb60793b9adfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=84=8F=E7=8B=BC?= <786316265@qq.com> Date: Tue, 26 Aug 2025 11:36:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=9F=8E=E5=8F=8A=E8=B4=AD=E7=89=A9?= =?UTF-8?q?=E8=BD=A6=E7=9B=B8=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/index.js | 14 +- src/views/BuyDetails.vue | 647 ++++++++++++++++++++++++++++++++++++ src/views/Pay.vue | 566 +++++++++++++++++++++++++++++++ src/views/ProductDetail.vue | 117 +++---- 4 files changed, 1272 insertions(+), 72 deletions(-) create mode 100644 src/views/BuyDetails.vue create mode 100644 src/views/Pay.vue diff --git a/src/router/index.js b/src/router/index.js index abc46fe..b8e94f2 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -233,11 +233,23 @@ const routes = [ { path: '/productsummary/:id', name: 'productSummary', - component: () => import('@/views/productSummary.vue'), + component: () => import('@/views/ProductSummary.vue'), meta: { title: '商品汇总' } }, + { + path: '/buydetail', + name: 'BuyDetail', + component: () => import('../views/BuyDetails.vue'), + meta: { title: '确认订单' } + }, + { + path: '/pay', + name: 'Pay', + component: () => import('../views/Pay.vue'), + meta: { title: '确认支付' } + }, { path: '/:pathMatch(.*)*', name: 'NotFound', diff --git a/src/views/BuyDetails.vue b/src/views/BuyDetails.vue new file mode 100644 index 0000000..fb36e08 --- /dev/null +++ b/src/views/BuyDetails.vue @@ -0,0 +1,647 @@ + + + + + \ No newline at end of file diff --git a/src/views/Pay.vue b/src/views/Pay.vue new file mode 100644 index 0000000..52de885 --- /dev/null +++ b/src/views/Pay.vue @@ -0,0 +1,566 @@ + + + + + \ No newline at end of file diff --git a/src/views/ProductDetail.vue b/src/views/ProductDetail.vue index e572bef..d2bfdf6 100644 --- a/src/views/ProductDetail.vue +++ b/src/views/ProductDetail.vue @@ -104,7 +104,7 @@
商品详情 - 商品材质|| + {{product.material}}|| {{ tag }} - ||品牌||产地 + ||{{product.brand}}||{{product.origin}}
@@ -432,7 +432,7 @@ const getProductDetail = async () => { } } -const addToCart = async () => { +const addToCart = () => { if (!product.value) { ElMessage.error('商品信息加载中,请稍后再试') return @@ -443,51 +443,14 @@ const addToCart = async () => { return } - if (quantity.value <= 0) { - ElMessage.error('请选择有效数量') - return - } - - // 检查是否已存在该商品 - const existingItemIndex = cartItems.value.findIndex(item => item.id === product.value.id) - - if (existingItemIndex !== -1) { - // 商品已存在,增加数量 - const existingItem = cartItems.value[existingItemIndex] - const newQuantity = existingItem.quantity + quantity.value - - if (newQuantity > product.value.stock) { - ElMessage.error(`库存不足,最多只能添加 ${product.value.stock - existingItem.quantity} 个`) - return + // 跳转到BuyDetails页面进行确认订单 + router.push({ + path: '/buydetail', + query: { + productId: product.value.id, + quantity: quantity.value } - - existingItem.quantity = newQuantity - ElMessage.success(`已将 ${quantity.value} 个商品添加到购物车`) - } else { - // 新商品,直接添加 - if (quantity.value > product.value.stock) { - ElMessage.error(`库存不足,最多只能添加 ${product.value.stock} 个`) - return - } - - const cartItem = { - id: product.value.id, - name: product.value.name, - image: product.value.images?.[0] || product.value.image, - points: product.value.points, - quantity: quantity.value, - stock: product.value.stock - } - - cartItems.value.push(cartItem) - ElMessage.success(`已将 ${quantity.value} 个商品添加到购物车`) - } - - // 重置数量选择器 - quantity.value = 1 - - // 同步购物车数据到后端 - await syncCartToBackend() + }) } // 购物车商品管理方法 @@ -625,34 +588,46 @@ const checkoutCart = async () => { } const buyNow = async () => { + if (!product.value) { + ElMessage.error('商品信息加载中,请稍后再试') + return + } + + if (product.value.stock === 0) { + ElMessage.error('商品已售罄') + return + } + try { - await ElMessageBox.confirm( - `确定要花费 ${totalPoints.value} 积分兑换 ${quantity.value} 个 ${product.value.name} 吗?`, - '确认兑换', - { - confirmButtonText: '确定', - cancelButtonText: '取消', - type: 'warning' - } - ) - - const orderData = { - items: [{ - productId: product.value.id, - quantity: quantity.value, - points: product.value.points - }], - totalPoints: totalPoints.value + // 先将商品添加到购物车 + const cartItem = { + productId: product.value.id, + quantity: quantity.value, + categoryId: selectedCategory.value?.id || null, + sizeId: selectedSize.value?.id || null, + points: product.value.points, + name: product.value.name, + image: product.value.images?.[0] || '', + stock: product.value.stock } + + const response = await api.post('/cart/add', cartItem) - await api.post('/orders', orderData) - - ElMessage.success('兑换成功!') - router.push('/orders') + if (response.data.success) { + const cartId = response.data.data.cartId + + // 跳转到支付页面 + router.push({ + path: '/pay', + query: { + cartId: cartId + } + }) + } else { + throw new Error(response.data.message || '添加到购物车失败') + } } catch (error) { - if (error !== 'cancel') { - ElMessage.error('兑换失败,请重试') - } + ElMessage.error(error.message || '操作失败,请重试') } }