Files
medical-mall/pages/mall/consumer/favorites.uvue
2026-02-27 16:51:56 +08:00

301 lines
7.3 KiB
Plaintext

<template>
<scroll-view class="favorites-page" direction="vertical">
<view class="product-grid">
<view v-if="favorites.length === 0" class="empty-state">
<text class="empty-icon">❤️</text>
<text class="empty-text">暂无收藏商品</text>
<button class="go-shopping-btn" @click="goShopping">去逛逛</button>
</view>
<view v-else v-for="(product, index) in favorites" :key="index" class="product-item" @click="goToDetail(product.id)">
<image :src="product.main_image_url" class="product-image" mode="aspectFill" />
<text class="product-name" :lines="2">{{ product.name }}</text>
<view class="product-bottom">
<text class="product-price">¥{{ product.price }}</text>
<view class="product-add-btn" @click.stop="addToCart(product)">
<text class="add-icon">+</text>
</view>
</view>
</view>
</view>
</scroll-view>
</template>
<script setup lang="uts">
import { ref, onMounted } from 'vue'
import { supabaseService } from '@/utils/supabaseService.uts'
import type { Product } from '@/utils/supabaseService.uts'
const favorites = ref<Array<Product>>([])
const addToCart = async (product: Product) => {
uni.showLoading({ title: '检查商品...' })
try {
const merchantId = product.merchant_id ?? product.shop_id ?? ''
// 检查商品是否有SKU
const skus = await supabaseService.getProductSkus(product.id)
uni.hideLoading()
if (skus.length > 0) {
// 有规格,提示并跳转到商品详情页选择规格
uni.showToast({ title: '请选择规格', icon: 'none' })
setTimeout(() => {
uni.navigateTo({
url: '/pages/mall/consumer/product-detail?id=' + product.id
})
}, 500)
} else {
// 无规格,直接加入购物车
uni.showLoading({ title: '添加中...' })
const success = await supabaseService.addToCart(product.id, 1, '', merchantId)
uni.hideLoading()
if (success) {
uni.showToast({ title: '已添加到购物车', icon: 'success' })
} else {
uni.showToast({ title: '添加失败', icon: 'none' })
}
}
} catch (e) {
console.error('添加到购物车异常', e)
uni.hideLoading()
uni.showToast({ title: '操作失败', icon: 'none' })
}
}
const loadFavorites = async () => {
const res = await supabaseService.getFavorites()
favorites.value = res.map((item: any): Product => {
let prod: any | null = null
let itemObj: UTSJSONObject | null = null
if (item instanceof UTSJSONObject) {
itemObj = item as UTSJSONObject
prod = itemObj.get('ml_products')
} else {
itemObj = JSON.parse(JSON.stringify(item)) as UTSJSONObject
prod = itemObj.get('ml_products')
}
let image = '/static/default-product.png'
let id = ''
let name = '未知商品'
let price = 0
let sales = 0
let merchantId = ''
if (prod != null) {
let prodObj: UTSJSONObject
if (prod instanceof UTSJSONObject) {
prodObj = prod as UTSJSONObject
} else {
prodObj = JSON.parse(JSON.stringify(prod)) as UTSJSONObject
}
id = prodObj.getString('id') ?? ''
name = prodObj.getString('name') ?? '未知商品'
price = prodObj.getNumber('base_price') ?? 0
image = prodObj.getString('main_image_url') ?? image
sales = prodObj.getNumber('sale_count') ?? 0
merchantId = prodObj.getString('merchant_id') ?? ''
if (image === '/static/default-product.png') {
const imgUrls = prodObj.getString('image_urls')
if (imgUrls != null) {
try {
const arr = JSON.parse(imgUrls)
if (Array.isArray(arr) && arr.length > 0) {
image = arr[0] as string
}
} catch(e) {}
}
}
} else {
if (itemObj != null) {
id = itemObj.getString('target_id') ?? ''
}
}
return {
id: id,
name: name,
price: price,
category_id: '',
merchant_id: merchantId,
main_image_url: image,
sale_count: sales
} as Product
})
}
const goShopping = () => {
uni.switchTab({
url: '/pages/mall/consumer/index'
})
}
const goToDetail = (id: string) => {
uni.navigateTo({
url: `/pages/mall/consumer/product-detail?productId=${id}`
})
}
const removeFavorite = (id: string) => {
uni.showModal({
title: '取消收藏',
content: '确定要取消收藏该商品吗?',
success: (res) => {
if (res.confirm) {
supabaseService.toggleFavorite(id).then((isStillFavorite) => {
if (!isStillFavorite) {
const index = favorites.value.findIndex((item): Boolean => {
return item.id === id
})
if (index !== -1) {
favorites.value.splice(index, 1)
}
uni.showToast({
title: '已取消收藏',
icon: 'none'
})
} else {
uni.showToast({
title: '取消失败',
icon: 'none'
})
}
})
}
}
})
}
onMounted(() => {
loadFavorites()
})
</script>
<style>
.favorites-page {
padding: 15px;
background-color: #f5f5f5;
flex: 1;
}
.product-grid {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
width: 100%;
}
.empty-state {
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding-top: 100px;
}
.empty-icon {
font-size: 60px;
margin-bottom: 20px;
color: #ddd;
}
.empty-text {
font-size: 16px;
color: #999;
margin-bottom: 20px;
}
.go-shopping-btn {
background-color: #ff5000;
color: white;
padding: 8px 24px;
border-radius: 20px;
font-size: 14px;
border: none;
}
.product-item {
display: flex;
flex-direction: column;
background: #fff;
border-radius: 8px;
overflow: hidden;
width: 48%;
margin-bottom: 12px;
}
.product-image {
width: 100%;
height: 170px;
border-radius: 8px;
margin-bottom: 8px;
background: #f5f5f5;
}
.product-name {
font-size: 13px;
color: #333;
margin-bottom: 5px;
line-height: 1.4;
height: 36px;
overflow: hidden;
text-overflow: ellipsis;
padding: 0 8px;
}
.product-bottom {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
padding: 0 8px 8px;
}
.product-price {
font-size: 15px;
color: #ff5000;
font-weight: bold;
}
.product-add-btn {
width: 24px;
height: 24px;
background-color: #ff5000;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
}
.add-icon {
color: #fff;
font-size: 16px;
font-weight: bold;
}
/* PC/Tablet Responsive */
@media (min-width: 768px) {
.product-item {
width: 31% !important;
}
}
@media (min-width: 1024px) {
.product-item {
width: 15% !important;
}
.product-grid, .header {
max-width: 1200px;
margin: 0 auto;
}
}
</style>