商城实现
This commit is contained in:
@@ -366,31 +366,42 @@ const handlePurchase = async () => {
|
||||
}
|
||||
|
||||
try {
|
||||
// 构建商品数据,直接跳转到Pay页面
|
||||
const purchaseData = {
|
||||
items: [{
|
||||
id: product.value.id,
|
||||
productId: product.value.id,
|
||||
name: product.value.name,
|
||||
image: product.value.image,
|
||||
points: product.value.points,
|
||||
quantity: quantity.value,
|
||||
specificationId: specificationId,
|
||||
specs: Object.keys(selectedSpecs.value).map(specName =>
|
||||
`${specName}: ${selectedSpecs.value[specName].name}`
|
||||
).join(', '),
|
||||
orderNote: orderNote.value
|
||||
}]
|
||||
// 先将商品添加到购物车
|
||||
const cartItem = {
|
||||
productId: product.value.id,
|
||||
quantity: quantity.value,
|
||||
specificationId: specificationId,
|
||||
points: product.value.points,
|
||||
name: product.value.name,
|
||||
image: product.value.image,
|
||||
stock: product.value.stock
|
||||
}
|
||||
|
||||
const addToCartResponse = await api.post('/cart/add', cartItem)
|
||||
|
||||
if (!addToCartResponse.data.success) {
|
||||
throw new Error(addToCartResponse.data.message || '添加到购物车失败')
|
||||
}
|
||||
|
||||
// 获取刚添加的购物车项ID
|
||||
const cartItemId = addToCartResponse.data.data?.cart_item_id || addToCartResponse.data.data?.id || addToCartResponse.data.data?.cartItemId || addToCartResponse.data.id
|
||||
|
||||
if (!cartItemId) {
|
||||
console.error('添加购物车API响应:', addToCartResponse.data)
|
||||
throw new Error('无法获取购物车项ID')
|
||||
}
|
||||
|
||||
// 跳转到Pay页面,传递商品数据
|
||||
router.push({
|
||||
path: '/pay',
|
||||
query: {
|
||||
from: 'buydetails',
|
||||
cartData: JSON.stringify(purchaseData)
|
||||
}
|
||||
// 创建订单
|
||||
const response = await api.post('/orders/create-from-cart', {
|
||||
cart_item_ids: [cartItemId]
|
||||
})
|
||||
|
||||
if (response.data.success) {
|
||||
// 跳转到Pay页面
|
||||
router.push(`/pay/${response.data.data.preOrderId}`)
|
||||
} else {
|
||||
throw new Error(response.data.message || '创建订单失败')
|
||||
}
|
||||
} catch (error) {
|
||||
ElMessage.error(error.message || '操作失败,请重试')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user