修复一些问题,个人账号功能完善,注册功能完善,收藏,购物车功能完善,初版基本完成,投入测试

This commit is contained in:
xiaowang
2026-03-03 17:50:10 +08:00
parent b301e37468
commit b0470b8f86
23 changed files with 804 additions and 129 deletions
+94 -12
View File
@@ -69,7 +69,27 @@
</div>
</div>
<div class="tags">
<el-tag size="small" effect="light" style="margin-right: 8px; margin-bottom: 8px">
<el-tag
v-for="tag in product.tagInfos"
:key="tag.id"
size="small"
effect="light"
:style="{
marginRight: '8px',
marginBottom: '8px',
color: tag.textColor || '#409eff',
backgroundColor: tag.bgColor || '#ecf5ff',
borderColor: tag.borderColor || '#409eff'
}"
>
{{ tag.tagName }}
</el-tag>
<el-tag
v-if="!product.tagInfos || product.tagInfos.length === 0"
size="small"
effect="light"
style="margin-right: 8px; margin-bottom: 8px"
>
{{ product.category }}
</el-tag>
</div>
@@ -96,8 +116,9 @@ import { ArrowLeft, Star, Clock, Collection, ShoppingCart, Right } from '@elemen
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { productApi, type ProductVO } from '@/api/product'
import { cartApi } from '@/api/cart'
import { wishlistApi } from '@/api/wishlist'
import { fileApi } from '@/api/file'
// 注入更新购物车数量的方法
const updateCartCount = inject<() => Promise<void>>('updateCartCount', async () => {})
@@ -122,6 +143,8 @@ const product = ref<ProductVO>({
const isFavorite = ref(false)
const currentImageIndex = ref(0)
const productImages = ref<string[]>([])
const imageIdCache = ref<Record<string, string>>({})
const DEFAULT_PRODUCT_IMAGE = 'data:image/svg+xml;base64,PHN2ZyB3aW0dPSJodHRwOi8vd3d3Lm9yZy8yMDAwL3N2ZyI+Pjwvc3ZnPg=='
// 从后端获取商品详情
const fetchProductDetail = async () => {
@@ -133,18 +156,59 @@ const fetchProductDetail = async () => {
product.value = response || {}
await loadProductImages()
await checkWishlistStatus()
} catch (error) {
console.error('获取商品详情失败:', error)
ElMessage.error('未登录,请先登录')
}
}
// 检查商品是否在收藏列表中
const checkWishlistStatus = async () => {
if (product.value.id) {
const token = localStorage.getItem('accessToken') || sessionStorage.getItem('accessToken')
if (!token) {
isFavorite.value = false
return
}
try {
const status = await wishlistApi.checkWishlist(product.value.id)
isFavorite.value = status
} catch (error) {
console.error('检查收藏状态失败:', error)
isFavorite.value = false
}
}
}
// 加载商品图片
const loadProductImages = async () => {
// 由于ProductVO类型中没有imageFileIds属性,这里简化处理
if (product.value.image) {
productImages.value = [product.value.image]
currentImageIndex.value = 0
if (product.value.imageFileIds) {
const ids = product.value.imageFileIds.split(',')
const urls: string[] = []
for (const id of ids) {
if (id) {
try {
if (!imageIdCache.value[id]) {
const response = await fileApi.getSignedUrl(Number(id)) as any
imageIdCache.value[id] = response
}
urls.push(imageIdCache.value[id])
} catch (error) {
console.error('获取签名链接失败:', error)
urls.push(DEFAULT_PRODUCT_IMAGE)
}
}
}
if (urls.length > 0) {
productImages.value = urls
currentImageIndex.value = 0
}
} else {
productImages.value = [DEFAULT_PRODUCT_IMAGE]
}
}
@@ -158,10 +222,23 @@ const handleBackClick = () => {
router.back()
}
const handleFavoriteClick = () => {
isFavorite.value = !isFavorite.value
console.log('收藏状态:', isFavorite.value)
ElMessage.success(isFavorite.value ? '已收藏' : '已取消收藏')
const handleFavoriteClick = async () => {
if (!product.value.id) return
try {
if (isFavorite.value) {
await wishlistApi.removeFromWishlist(product.value.id)
ElMessage.success('已取消收藏')
} else {
await wishlistApi.addToWishlist(product.value.id)
ElMessage.success('已收藏')
}
isFavorite.value = !isFavorite.value
console.log('收藏状态:', isFavorite.value)
} catch (error) {
console.error('操作收藏失败:', error)
ElMessage.error('操作失败,请重试')
}
}
const handleAddToCart = async () => {
@@ -174,7 +251,7 @@ const handleAddToCart = async () => {
console.log('调用cartApi.addToCart成功')
// 实时更新购物车角标数字
await updateCartCount()
ElMessage.success(`已将 ${product.value.name} 添加到购物车`)
ElMessage.success(`已将 ${product.value.title || product.value.name || '商品'} 添加到购物车`)
} catch (error) {
console.error('添加到购物车失败:', error)
ElMessage.error('添加到购物车失败,请重试')
@@ -247,7 +324,12 @@ onMounted(() => {
}
.actions .active {
color: #ff4d4f;
color: #FFD700;
fill: #FFD700;
}
.actions .active svg {
fill: #FFD700;
}
.product-image-container {