商城实现
This commit is contained in:
@@ -153,14 +153,6 @@
|
||||
>
|
||||
加入购物车
|
||||
</el-button>
|
||||
<el-button
|
||||
type="primary"
|
||||
size="large"
|
||||
@click="buyNow"
|
||||
:disabled="product.stock === 0 || totalPoints > userPoints"
|
||||
>
|
||||
{{ product.stock === 0 ? '缺货' : totalPoints > userPoints ? '积分不足' : '立即兑换' }}
|
||||
</el-button>
|
||||
</div>
|
||||
|
||||
<!-- 商品评价 -->
|
||||
@@ -247,6 +239,7 @@ import {
|
||||
Loading
|
||||
} from '@element-plus/icons-vue'
|
||||
import api from '@/utils/api'
|
||||
import { getImageUrl } from '@/config'
|
||||
|
||||
import { watch } from 'vue'
|
||||
|
||||
@@ -290,8 +283,24 @@ const getProductDetail = async () => {
|
||||
console.log(recommendedRes,'recommendedRes');
|
||||
|
||||
product.value = productRes.data.data.product
|
||||
|
||||
// 处理商品图片路径
|
||||
if (product.value.image) {
|
||||
product.value.image = getImageUrl(product.value.image)
|
||||
}
|
||||
if (product.value.images && Array.isArray(product.value.images)) {
|
||||
product.value.images = product.value.images.map(img => getImageUrl(img))
|
||||
}
|
||||
|
||||
reviews.value = reviewsRes.data.data.reviews || []
|
||||
recommendedProducts.value = recommendedRes.data.data.products || []
|
||||
|
||||
// 处理推荐商品图片路径
|
||||
recommendedProducts.value.forEach(item => {
|
||||
if (item.image) {
|
||||
item.image = getImageUrl(item.image)
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
ElMessage.error('获取商品详情失败')
|
||||
router.go(-1)
|
||||
@@ -359,49 +368,7 @@ const addToCart = async () => {
|
||||
|
||||
// 购物车数据同步和结算方法已移除
|
||||
|
||||
const buyNow = async () => {
|
||||
if (!product.value) {
|
||||
ElMessage.error('商品信息加载中,请稍后再试')
|
||||
return
|
||||
}
|
||||
|
||||
if (product.value.stock === 0) {
|
||||
ElMessage.error('商品已售罄')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
// 先将商品添加到购物车
|
||||
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)
|
||||
|
||||
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) {
|
||||
ElMessage.error(error.message || '操作失败,请重试')
|
||||
}
|
||||
}
|
||||
|
||||
const goToProduct = (productId) => {
|
||||
router.replace(`/product/${productId}`)
|
||||
|
||||
Reference in New Issue
Block a user