Files
jurong_circle_frontdesk/src/views/BuyDetails.vue

786 lines
16 KiB
Vue
Raw Normal View History

2025-08-26 11:36:01 +08:00
<template>
<div class="buy-details-page">
<!-- 导航栏 -->
<nav class="navbar">
<div class="nav-left">
<el-button
type="text"
@click="$router.go(-1)"
class="back-btn"
>
<el-icon><ArrowLeft /></el-icon>
</el-button>
</div>
<div class="nav-center">
<h1 class="nav-title">确认订单</h1>
</div>
<div class="nav-right">
</div>
</nav>
<div v-loading="loading" class="page-content">
<!-- 收货地址 -->
<div class="address-section">
<div class="address-header">
<el-icon><Location /></el-icon>
<span class="address-label">收货地址</span>
<el-button
type="text"
@click="goToAddressManage"
class="manage-address-btn"
>
管理地址
</el-button>
2025-08-26 11:36:01 +08:00
</div>
<div class="address-content">
<el-select
v-model="selectedAddressId"
placeholder="请选择收货地址"
class="address-select"
@change="handleAddressChange"
>
<el-option
v-for="address in addresses"
:key="address.id"
:label="`${address.recipientName} ${address.recipientPhone} ${address.province}${address.city}${address.district}${address.detailAddress}`"
:value="address.id"
>
<div class="address-option">
<div class="address-info">
<span class="recipient-info">{{ address.recipientName }} {{ address.recipientPhone }}</span>
<el-tag v-if="address.isDefault" type="danger" size="small" class="default-tag">默认</el-tag>
</div>
<div class="address-detail">{{ address.province }}{{ address.city }}{{ address.district }}{{ address.detailAddress }}</div>
</div>
</el-option>
</el-select>
<div v-if="addresses.length === 0" class="no-address">
<span class="no-address-text">暂无收货地址</span>
<el-button type="text" @click="goToAddressManage" class="add-address-btn">
立即添加
</el-button>
</div>
2025-08-26 11:36:01 +08:00
</div>
</div>
<!-- 商品信息 -->
<div class="product-section">
<div class="product-info">
<div class="product-image">
<img :src="product?.image || '/imgs/productdetail/商品主图.png'" alt="商品主图" />
</div>
<div class="product-details">
<div class="product-price">
<span class="price-label">实付</span>
<el-icon class="coin-icon"><Coin /></el-icon>
<span class="price-value">{{ totalPrice }}</span>
</div>
<div class="quantity-selector">
<el-button size="small" @click="decreaseQuantity" :disabled="quantity <= 1">-</el-button>
<span class="quantity">{{ quantity }}</span>
<el-button size="small" @click="increaseQuantity">+</el-button>
</div>
</div>
</div>
</div>
<!-- 颜色分类 -->
<div class="category-section">
<h3 class="section-title">颜色分类 ({{ categories.length }})</h3>
<div class="category-grid">
<div
v-for="category in categories"
:key="category.id"
class="category-item"
:class="{ active: selectedCategory?.id === category.id }"
@click="selectCategory(category)"
>
<div class="category-image">
<img :src="category.image" :alt="category.name" />
</div>
<div class="category-info">
<div class="category-name">{{ category.name }}</div>
<div class="category-desc">{{ category.description }}</div>
</div>
</div>
</div>
</div>
<!-- 尺寸选择 -->
<div class="size-section">
<h3 class="section-title">尺寸</h3>
<div class="size-grid">
<div
v-for="size in sizes"
:key="size.id"
class="size-item"
:class="{ active: selectedSize?.id === size.id }"
@click="selectSize(size)"
>
<div class="size-label">{{ size.label }}</div>
<div class="size-range">{{ size.range }}</div>
</div>
</div>
</div>
<!-- 订单备注 -->
<div class="note-section">
<h3 class="section-title">订单备注</h3>
<div class="note-content" @click="showNoteEdit = true">
<span v-if="!showNoteEdit && !orderNote" class="note-placeholder">无备注</span>
<span v-if="!showNoteEdit && orderNote" class="note-text">{{ orderNote }}</span>
<el-input
v-if="showNoteEdit"
v-model="orderNote"
@blur="showNoteEdit = false"
@keyup.enter="showNoteEdit = false"
placeholder="请输入订单备注"
class="note-input"
autofocus
/>
</div>
</div>
</div>
<!-- 底部操作按钮 -->
<div class="bottom-actions">
<el-button
size="large"
class="cart-button"
@click="handleAddToCart"
2025-08-26 11:36:01 +08:00
:disabled="!canPurchase"
>
加入购物车
</el-button>
<el-button
type="primary"
size="large"
class="buy-button"
@click="handlePurchase"
:disabled="!canPurchase"
>
立即购买
</el-button>
</div>
</div>
</template>
<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import {
ArrowLeft,
Close,
Location,
Edit,
Coin,
ArrowRight
} from '@element-plus/icons-vue'
import api from '@/utils/api'
const route = useRoute()
const router = useRouter()
// 响应式数据
const loading = ref(false)
const product = ref(null)
const quantity = ref(1)
const categories = ref([])
const sizes = ref([])
const selectedCategory = ref(null)
const selectedSize = ref(null)
const addresses = ref([])
const selectedAddressId = ref('')
const selectedAddress = ref(null)
2025-08-26 11:36:01 +08:00
const orderNote = ref('')
const showNoteEdit = ref(false)
// 计算属性
const totalPrice = computed(() => {
if (!product.value) return 0
return product.value.points * quantity.value
})
const canPurchase = computed(() => {
return selectedCategory.value && selectedSize.value && quantity.value > 0
})
// 方法
const increaseQuantity = () => {
if (product.value && quantity.value < product.value.stock) {
quantity.value++
}
}
const decreaseQuantity = () => {
if (quantity.value > 1) {
quantity.value--
}
}
const selectCategory = (category) => {
selectedCategory.value = category
}
const selectSize = (size) => {
selectedSize.value = size
}
const getProductInfo = async () => {
try {
loading.value = true
const productId = route.query.productId
if (!productId) {
ElMessage.error('商品信息缺失')
router.go(-1)
return
}
const response = await api.get(`/products/${productId}`)
product.value = response.data.data.product
} catch (error) {
ElMessage.error('获取商品信息失败')
router.go(-1)
} finally {
loading.value = false
}
}
const getCategories = async () => {
try {
const productId = route.query.productId
const response = await api.get(`/products/${productId}/categories`)
categories.value = response.data.data.categories || []
} catch (error) {
console.error('获取分类信息失败:', error)
}
}
const getSizes = async () => {
try {
const productId = route.query.productId
const response = await api.get(`/products/${productId}/sizes`)
sizes.value = response.data.data.sizes || []
} catch (error) {
console.error('获取尺寸信息失败:', error)
}
}
const addToCart = async () => {
if (!canPurchase.value) {
ElMessage.error('请选择完整的商品信息')
return
}
try {
const cartItem = {
productId: product.value.id,
quantity: quantity.value,
categoryId: selectedCategory.value.id,
sizeId: selectedSize.value.id,
points: product.value.points,
name: product.value.name,
image: product.value.images?.[0] || product.value.image,
stock: product.value.stock
}
await api.post('/cart/add', cartItem)
ElMessage.success('商品已加入购物车!')
router.go(-1) // 返回上一页
} catch (error) {
ElMessage.error('加入购物车失败,请重试')
}
}
// 立即购买功能
2025-08-26 11:36:01 +08:00
const handlePurchase = async () => {
if (!canPurchase.value) {
ElMessage.error('请选择完整的商品信息')
return
}
if (!selectedAddress.value) {
ElMessage.error('请选择收货地址')
return
}
2025-08-26 11:36:01 +08:00
try {
// 创建单独的购买订单
const orderData = {
2025-08-26 11:36:01 +08:00
productId: product.value.id,
quantity: quantity.value,
categoryId: selectedCategory.value.id,
sizeId: selectedSize.value.id,
points: product.value.points,
name: product.value.name,
image: product.value.image,
stock: product.value.stock,
addressId: selectedAddress.value.id,
2025-08-26 11:36:01 +08:00
orderNote: orderNote.value
}
const response = await api.post('/cart/buy-now', orderData)
2025-08-26 11:36:01 +08:00
if (response.data.success) {
const cartId = response.data.data.cartId
// 跳转到支付页面
router.push({
path: '/pay',
query: {
cartId: cartId
}
})
} else {
throw new Error(response.data.message || '创建订单失败')
2025-08-26 11:36:01 +08:00
}
} catch (error) {
ElMessage.error(error.message || '操作失败,请重试')
}
}
// 添加到购物车功能(新增)
const handleAddToCart = async () => {
if (!canPurchase.value) {
ElMessage.error('请选择完整的商品信息')
return
}
try {
const cartItem = {
productId: product.value.id,
quantity: quantity.value,
categoryId: selectedCategory.value.id,
sizeId: selectedSize.value.id,
points: product.value.points,
name: product.value.name,
image: product.value.image,
stock: product.value.stock
}
const response = await api.post('/cart/add', cartItem)
if (response.data.success) {
ElMessage.success('商品已加入购物车!')
// 可以选择返回上一页或跳转到购物车页面
router.go(-1)
} else {
throw new Error(response.data.message || '添加到购物车失败')
}
} catch (error) {
ElMessage.error(error.message || '添加到购物车失败,请重试')
}
}
// 获取用户地址列表
const getAddressList = async () => {
try {
const response = await api.get('/address/list')
addresses.value = response.data.data.addresses || []
// 如果有默认地址,自动选中
const defaultAddress = addresses.value.find(addr => addr.isDefault)
if (defaultAddress) {
selectedAddressId.value = defaultAddress.id
selectedAddress.value = defaultAddress
}
} catch (error) {
console.error('获取地址列表失败:', error)
ElMessage.error('获取地址列表失败')
}
}
// 处理地址选择变化
const handleAddressChange = (addressId) => {
selectedAddress.value = addresses.value.find(addr => addr.id === addressId)
}
// 跳转到地址管理页面
const goToAddressManage = () => {
router.push('/address')
}
2025-08-26 11:36:01 +08:00
// 生命周期
onMounted(() => {
// 从URL参数获取初始数量
const initialQuantity = route.query.quantity
if (initialQuantity && !isNaN(initialQuantity)) {
quantity.value = parseInt(initialQuantity)
}
getProductInfo()
getCategories()
getSizes()
getAddressList()
2025-08-26 11:36:01 +08:00
})
</script>
<style scoped>
.buy-details-page {
min-height: 100vh;
background: #f5f5f5;
display: flex;
flex-direction: column;
}
.navbar {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px 16px;
background: white;
border-bottom: 1px solid #eee;
}
.nav-title {
font-size: 18px;
font-weight: 500;
margin: 0;
}
.back-btn {
color: #333;
padding: 0;
}
.page-content {
flex: 1;
padding: 0;
}
.address-section {
background: white;
padding: 16px;
margin-bottom: 8px;
}
.address-header {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
}
.address-label {
font-weight: 500;
}
.edit-icon {
margin-left: auto;
color: #666;
}
2025-08-26 11:36:01 +08:00
.product-section {
background: white;
padding: 16px;
margin-bottom: 8px;
}
.product-info {
display: flex;
gap: 12px;
}
.product-image {
width: 60px;
height: 60px;
border-radius: 8px;
overflow: hidden;
}
.product-image img {
width: 100%;
height: 100%;
object-fit: cover;
}
.product-details {
flex: 1;
display: flex;
justify-content: space-between;
align-items: center;
}
.product-price {
display: flex;
align-items: center;
gap: 4px;
}
.price-label {
font-size: 14px;
color: #666;
}
.coin-icon {
color: #ffae00;
}
.price-value {
font-size: 18px;
font-weight: bold;
color: #ffae00;
}
.quantity-selector {
display: flex;
align-items: center;
gap: 12px;
}
.quantity {
font-size: 16px;
min-width: 20px;
text-align: center;
}
.category-section,
.size-section,
.note-section,
.payment-section {
background: white;
padding: 16px;
margin-bottom: 8px;
}
.section-title {
font-size: 16px;
font-weight: 500;
margin: 0 0 12px 0;
}
.category-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
}
.category-item {
display: flex;
gap: 8px;
padding: 8px;
border: 1px solid #eee;
border-radius: 8px;
cursor: pointer;
transition: all 0.2s;
}
.category-item.active {
border-color: #ffae00;
background: #fff7e6;
}
.category-image {
width: 40px;
height: 40px;
border-radius: 4px;
overflow: hidden;
}
.category-image img {
width: 100%;
height: 100%;
object-fit: cover;
}
.category-info {
flex: 1;
}
.category-name {
font-size: 14px;
font-weight: 500;
margin-bottom: 2px;
}
.category-desc {
font-size: 12px;
color: #666;
}
.size-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
}
.size-item {
padding: 12px 8px;
border: 1px solid #eee;
border-radius: 8px;
text-align: center;
cursor: pointer;
transition: all 0.2s;
}
.size-item.active {
border-color: #ffae00;
background: #fff7e6;
}
.size-label {
font-size: 14px;
font-weight: 500;
margin-bottom: 4px;
}
.size-range {
font-size: 12px;
color: #666;
}
.note-content {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px 0;
}
.note-placeholder {
color: #999;
}
.address-select {
width: 100%;
margin-top: 10px;
}
.manage-address-btn {
color: #409eff;
font-size: 14px;
margin-left: auto;
}
.manage-address-btn:hover {
color: #66b1ff;
}
.address-option {
padding: 8px 0;
}
.address-info {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 4px;
}
.recipient-info {
font-weight: 500;
color: #303133;
}
.default-tag {
margin-left: 8px;
2025-08-26 11:36:01 +08:00
}
.address-detail {
color: #606266;
font-size: 12px;
line-height: 1.4;
}
.no-address {
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
color: #909399;
font-size: 14px;
}
.no-address-text {
margin-right: 8px;
}
.add-address-btn {
color: #409eff;
font-size: 14px;
}
.add-address-btn:hover {
color: #66b1ff;
2025-08-26 11:36:01 +08:00
}
.note-input {
flex: 1;
margin-right: 8px;
}
.note-text {
color: #333;
flex: 1;
}
2025-08-26 11:36:01 +08:00
.note-content {
cursor: pointer;
}
.payment-options {
display: flex;
flex-direction: column;
gap: 12px;
}
.payment-option {
display: flex;
align-items: center;
padding: 8px 0;
}
.bottom-actions {
padding: 16px;
background: white;
border-top: 1px solid #eee;
display: flex;
gap: 12px;
}
.cart-button {
flex: 1;
height: 48px;
background: white;
border: 1px solid #ffae00;
color: #ffae00;
border-radius: 24px;
font-size: 16px;
font-weight: 500;
}
.cart-button:hover {
background: #fff7e6;
}
.cart-button:disabled {
background: #f5f5f5;
border-color: #ccc;
color: #ccc;
cursor: not-allowed;
}
.buy-button {
flex: 1;
height: 48px;
background: #ffae00;
border: none;
border-radius: 24px;
font-size: 16px;
font-weight: 500;
}
.buy-button:hover {
background: #e69900;
}
.buy-button:disabled {
background: #ccc;
cursor: not-allowed;
}
</style>