商城及购物车相关
This commit is contained in:
@@ -104,7 +104,7 @@
|
||||
<!-- 第一组:商品详情图标 + 文本 -->
|
||||
<div class="detail-item-group"> <!-- 每组独立容器 -->
|
||||
<img src="/imgs/productdetail/商品详情.png" alt="商品详情" class="detail-icon">
|
||||
<span class="detail-text">商品材质||
|
||||
<span class="detail-text">{{product.material}}||
|
||||
<el-tag
|
||||
v-for="tag in product.tags"
|
||||
:key="tag"
|
||||
@@ -112,7 +112,7 @@
|
||||
class="product-tag"
|
||||
>
|
||||
{{ tag }}
|
||||
</el-tag>||品牌||产地
|
||||
</el-tag>||{{product.brand}}||{{product.origin}}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
@@ -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 || '操作失败,请重试')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user