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

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
+112 -24
View File
@@ -6,24 +6,29 @@
<div class="back-btn" @click="handleBackClick">
<el-icon><ArrowLeft /></el-icon>
</div>
<h1>Wishlist</h1>
<h1>我的收藏</h1>
<div class="empty"></div>
</header>
<!-- Wishlist Items -->
<div class="wishlist-items">
<div v-for="item in wishlistItems" :key="item.id" class="wishlist-item">
<div class="item-image" :style="{ backgroundColor: item.bgColor }">
<span>{{ item.name[0] }}</span>
<div class="item-image" @click="handleProductClick(item.product.id)">
<img v-if="getProductImages(item.product) && getProductImages(item.product).length > 0"
:src="getProductImages(item.product)[0]"
:alt="item.product.title" />
<div v-else class="image-placeholder" :style="{ backgroundColor: getRandomColor() }">
<span>{{ item.product.title ? item.product.title[0] : '商' }}</span>
</div>
</div>
<div class="item-details">
<h3 class="item-name">{{ item.name }}</h3>
<p class="item-price">${{ item.price }}</p>
<h3 class="item-name" @click="handleProductClick(item.product.id)">{{ item.product.title }}</h3>
<p class="item-price">¥{{ item.product.price }}</p>
<div class="item-actions">
<el-button size="small" type="primary" @click="handleAddToCart(item)">
Add to Cart
<el-button size="small" type="primary" @click="handleAddToCart(item.product.id)">
添加到购物车
</el-button>
<el-icon class="delete-icon" @click="handleRemoveItem(item)"><Delete /></el-icon>
<el-icon class="delete-icon" @click="handleRemoveItem(item.product.id)"><Delete /></el-icon>
</div>
</div>
</div>
@@ -32,42 +37,96 @@
<!-- Empty Wishlist Message -->
<div v-if="wishlistItems.length === 0" class="empty-wishlist">
<el-icon :size="60"><Star /></el-icon>
<p>Your wishlist is empty</p>
<p>收藏列表为空</p>
<el-button type="primary" @click="handleBrowseProducts">
Browse Products
浏览商品
</el-button>
</div>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { ref, onMounted } from 'vue'
import { ArrowLeft, Delete, Star } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { wishlistApi, type WishlistItem } from '@/api/wishlist'
import { cartApi } from '@/api/cart'
import { fileApi } from '@/api/file'
const router = useRouter()
const wishlistItems = ref([
{ id: 1, name: 'Watch', price: 40, bgColor: '#ffe4d6' },
{ id: 2, name: 'LG TV', price: 330, bgColor: '#f0d6ff' },
{ id: 3, name: 'Jacket', price: 50, bgColor: '#ffd6e2' },
{ id: 4, name: 'Nike Shoes', price: 430, bgColor: '#d6eaff' }
])
const wishlistItems = ref<WishlistItem[]>([])
const imageIdCache = ref<Record<string, string>>({})
const imageUrls = ref<Record<number, string[]>>({})
const DEFAULT_PRODUCT_IMAGE = 'data:image/svg+xml;base64,PHN2ZyB3aW0dPSJodHRwOi8vd3d3Lm9yZy8yMDAwL3N2ZyI+Pjwvc3ZnPg=='
const handleBackClick = () => {
console.log('返回按钮被点击')
router.back()
}
const handleAddToCart = (item: any) => {
console.log('添加到购物车:', item.name)
const fetchWishlist = async () => {
try {
const response = await wishlistApi.getWishlist()
wishlistItems.value = response
await loadImageUrls(response)
} catch (error) {
console.error('获取收藏列表失败:', error)
ElMessage.error('获取收藏列表失败')
}
}
const handleRemoveItem = (item: any) => {
console.log('从收藏夹移除:', item.name)
const index = wishlistItems.value.findIndex(i => i.id === item.id)
if (index > -1) {
wishlistItems.value.splice(index, 1)
const loadImageUrls = async (items: WishlistItem[]) => {
for (const item of items) {
if (item.product.imageFileIds) {
const ids = item.product.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)
}
}
}
imageUrls.value[item.product.id] = urls
} else {
imageUrls.value[item.product.id] = [DEFAULT_PRODUCT_IMAGE]
}
}
}
const getProductImages = (product: any): string[] => {
return imageUrls.value[product.id] || [DEFAULT_PRODUCT_IMAGE]
}
const handleAddToCart = async (productId: number) => {
try {
await cartApi.addToCart({ productId, quantity: 1 })
ElMessage.success('已添加到购物车')
} catch (error) {
console.error('添加到购物车失败:', error)
ElMessage.error('添加到购物车失败')
}
}
const handleRemoveItem = async (productId: number) => {
try {
await wishlistApi.removeFromWishlist(productId)
wishlistItems.value = wishlistItems.value.filter(item => item.productId !== productId)
ElMessage.success('已从收藏中移除')
} catch (error) {
console.error('移除收藏失败:', error)
ElMessage.error('移除收藏失败')
}
}
@@ -75,6 +134,19 @@ const handleBrowseProducts = () => {
console.log('浏览商品')
router.push('/products')
}
const handleProductClick = (productId: number) => {
router.push(`/product/${productId}`)
}
const getRandomColor = () => {
const colors = ['#ffe4d6', '#d6eaff', '#e2ffd6', '#fff2d6', '#d6ffe4', '#e4d6ff']
return colors[Math.floor(Math.random() * colors.length)]
}
onMounted(() => {
fetchWishlist()
})
</script>
<style scoped>
@@ -121,6 +193,22 @@ const handleBrowseProducts = () => {
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
cursor: pointer;
}
.item-image img {
width: 100%;
height: 100%;
object-fit: cover;
}
.image-placeholder {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
font-size: 30px;
font-weight: bold;
color: rgba(0,0,0,0.1);