fix: 添加购物车时检查购物资格
- 后端 CartHandler.Add 新增购物资格检查 - 后端新增库存检查和最大购买数量检查 - 前端商品详情页添加资格不足提示和视觉反馈 - 显示用户当前资格和所需资格总数 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -33,8 +33,15 @@
|
||||
<p class="price">¥{{ product.price }}</p>
|
||||
<p class="description">{{ product.description }}</p>
|
||||
<div class="meta">
|
||||
<el-tag v-if="product.require_credit" type="warning">{{ $t('product.requireCredit') }}: {{ product.credit_cost }}</el-tag>
|
||||
<el-tag v-if="product.credit_reward > 0" type="success">+{{ product.credit_reward }} {{ $t('product.creditReward') }}</el-tag>
|
||||
<el-tag v-if="product.require_credit" :type="hasEnoughCredit() ? 'warning' : 'danger'">
|
||||
{{ $t('product.requireCredit') }}: {{ product.credit_cost * quantity }}
|
||||
<span v-if="!hasEnoughCredit()"> (资格不足)</span>
|
||||
</el-tag>
|
||||
<el-tag v-if="product.credit_reward > 0" type="success">+{{ product.credit_reward * quantity }} {{ $t('product.creditReward') }}</el-tag>
|
||||
</div>
|
||||
<div class="user-credits" v-if="product.require_credit">
|
||||
<span class="credits-label">当前资格:</span>
|
||||
<span class="credits-value" :class="{ 'insufficient': !hasEnoughCredit() }">{{ userCredits }}</span>
|
||||
</div>
|
||||
<div class="custom-fields" v-if="product.custom_fields?.length">
|
||||
<h4>{{ $t('product.customFields') }}</h4>
|
||||
@@ -59,7 +66,7 @@ import { ref, computed, onMounted } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { Goods } from '@element-plus/icons-vue'
|
||||
import { productApi } from '../../api'
|
||||
import { productApi, userApi } from '../../api'
|
||||
import { useCartStore } from '../../store/cart'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { getImageUrl } from '../../utils/image'
|
||||
@@ -71,24 +78,62 @@ const { t } = useI18n()
|
||||
const cartStore = useCartStore()
|
||||
const product = ref<any>(null)
|
||||
const quantity = ref(1)
|
||||
const userCredits = ref(0)
|
||||
|
||||
const productImages = computed(() => {
|
||||
if (!product.value?.images) return []
|
||||
return product.value.images.split(',').map((s: string) => s.trim()).filter(Boolean).map((url: string) => getImageUrl(url))
|
||||
})
|
||||
|
||||
// 检查用户是否有足够资格购买
|
||||
function hasEnoughCredit(): boolean {
|
||||
if (!product.value || !product.value.require_credit) return true
|
||||
const totalCost = product.value.credit_cost * quantity.value
|
||||
return userCredits.value >= totalCost
|
||||
}
|
||||
|
||||
// 获取用户资格信息
|
||||
async function fetchUserCredits() {
|
||||
try {
|
||||
const res: any = await userApi.getProfile()
|
||||
userCredits.value = res.data?.purchase_credits || 0
|
||||
} catch {}
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
const res: any = await productApi.getById(Number(route.params.id))
|
||||
product.value = res.data
|
||||
// 设置默认数量为最小购买量
|
||||
if (product.value?.min_purchase && product.value.min_purchase > 1) {
|
||||
quantity.value = product.value.min_purchase
|
||||
}
|
||||
// 获取用户资格信息
|
||||
await fetchUserCredits()
|
||||
})
|
||||
|
||||
async function addToCart() {
|
||||
await cartStore.addItem(product.value.id, quantity.value)
|
||||
ElMessage.success(t('product.addToCart') + ' ✓')
|
||||
// 检查购物资格
|
||||
if (!hasEnoughCredit()) {
|
||||
const totalCost = product.value.credit_cost * quantity.value
|
||||
ElMessage.warning(`资格不足!需要 ${totalCost} 资格,您当前只有 ${userCredits.value} 资格`)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await cartStore.addItem(product.value.id, quantity.value)
|
||||
ElMessage.success(t('product.addToCart') + ' ✓')
|
||||
} catch (err: any) {
|
||||
// 处理后端返回的错误
|
||||
const errorMsg = err.response?.data?.error || '添加购物车失败'
|
||||
ElMessage.error(errorMsg)
|
||||
}
|
||||
}
|
||||
|
||||
function buyNow() {
|
||||
addToCart().then(() => router.push('/cart'))
|
||||
addToCart().then(() => {
|
||||
// 只有成功添加后才跳转
|
||||
router.push('/cart')
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -123,6 +168,30 @@ h1 { font-size: 28px; color: #fff; margin-bottom: 16px; }
|
||||
.price { font-size: 32px; font-weight: 700; color: #4e6ef2; margin-bottom: 16px; }
|
||||
.description { color: rgba(255, 255, 255, 0.5); margin-bottom: 16px; line-height: 1.6; }
|
||||
.meta { display: flex; gap: 8px; margin-bottom: 16px; }
|
||||
.user-credits {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
margin-bottom: 16px;
|
||||
padding: 8px 12px;
|
||||
background: rgba(255, 255, 255, 0.04);
|
||||
border-radius: 6px;
|
||||
|
||||
.credits-label {
|
||||
color: rgba(255, 255, 255, 0.6);
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.credits-value {
|
||||
color: #10b981;
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
|
||||
&.insufficient {
|
||||
color: #f56c6c;
|
||||
}
|
||||
}
|
||||
}
|
||||
.custom-fields { margin-bottom: 24px; h4 { color: #fff; margin-bottom: 8px; } .field-item { color: rgba(255, 255, 255, 0.7); margin-bottom: 4px; .field-name { color: rgba(255, 255, 255, 0.5); margin-right: 8px; } } }
|
||||
.purchase-section { display: flex; gap: 12px; align-items: center; margin-top: 24px; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user