consumerm模块完成度90%,完善消费者和商家端数据库表,商品、聊天、订单数据对接好了supabase,和商家端对接了聊天功能,安卓端编译通过了css样式,剩余几个页面在处理函数规范问题
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<template>
|
||||
<view class="favorites-page">
|
||||
<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>
|
||||
@@ -26,7 +26,7 @@
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</scroll-view>
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
@@ -45,13 +45,9 @@ type Product = {
|
||||
|
||||
const favorites = ref<Product[]>([])
|
||||
|
||||
onMounted(() => {
|
||||
loadFavorites()
|
||||
})
|
||||
|
||||
const addToCart = async (product: Product) => {
|
||||
uni.showLoading({ title: '添加中' })
|
||||
const success = await supabaseService.addToCart(product.id, 1)
|
||||
const success = await supabaseService.addToCart(product.id, 1, '')
|
||||
uni.hideLoading()
|
||||
if (success) {
|
||||
uni.showToast({ title: '已添加到购物车', icon: 'success' })
|
||||
@@ -63,13 +59,16 @@ const addToCart = async (product: Product) => {
|
||||
const loadFavorites = async () => {
|
||||
const res = await supabaseService.getFavorites()
|
||||
|
||||
// Map response
|
||||
favorites.value = res.map((item: any): Product => {
|
||||
let prod: any = null
|
||||
let prod: any | null = null
|
||||
let itemObj: UTSJSONObject | null = null
|
||||
|
||||
if (item instanceof UTSJSONObject) {
|
||||
prod = item.get('ml_products')
|
||||
itemObj = item as UTSJSONObject
|
||||
prod = itemObj.get('ml_products')
|
||||
} else {
|
||||
prod = item['ml_products']
|
||||
itemObj = JSON.parse(JSON.stringify(item)) as UTSJSONObject
|
||||
prod = itemObj.get('ml_products')
|
||||
}
|
||||
|
||||
let image = '/static/default-product.png'
|
||||
@@ -79,46 +78,33 @@ const loadFavorites = async () => {
|
||||
let sales = 0
|
||||
|
||||
if (prod != null) {
|
||||
let prodObj: UTSJSONObject
|
||||
if (prod instanceof UTSJSONObject) {
|
||||
id = prod.getString('id') || ''
|
||||
name = prod.getString('name') || '未知商品'
|
||||
price = prod.getNumber('base_price') || 0
|
||||
image = prod.getString('main_image_url') || image
|
||||
sales = prod.getNumber('sale_count') || 0
|
||||
|
||||
// 如果 main_image_url 为空,尝试解析 image_urls
|
||||
if (image === '/static/default-product.png') {
|
||||
const imgUrls = prod.getString('image_urls')
|
||||
if (imgUrls) {
|
||||
try {
|
||||
const arr = JSON.parse(imgUrls)
|
||||
if (Array.isArray(arr) && arr.length > 0) image = arr[0] as string
|
||||
} catch(e) {}
|
||||
}
|
||||
}
|
||||
prodObj = prod as UTSJSONObject
|
||||
} else {
|
||||
id = (prod['id'] as string) || ''
|
||||
name = (prod['name'] as string) || '未知商品'
|
||||
price = (prod['base_price'] as number) || 0
|
||||
image = (prod['main_image_url'] as string) || image
|
||||
sales = (prod['sale_count'] as number) || 0
|
||||
|
||||
if (image === '/static/default-product.png') {
|
||||
const imgUrls = prod['image_urls'] as string
|
||||
if (imgUrls) {
|
||||
try {
|
||||
const arr = JSON.parse(imgUrls)
|
||||
if (Array.isArray(arr) && arr.length > 0) image = arr[0] as string
|
||||
} catch(e) {}
|
||||
}
|
||||
}
|
||||
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
|
||||
|
||||
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 {
|
||||
// 如果没取到商品,尝试直接从 item 取 target_id
|
||||
if (item instanceof UTSJSONObject) {
|
||||
id = item.getString('target_id') || ''
|
||||
} else {
|
||||
id = (item['target_id'] as string) || ''
|
||||
if (itemObj != null) {
|
||||
id = itemObj.getString('target_id') ?? ''
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,7 +116,7 @@ const loadFavorites = async () => {
|
||||
sales: sales,
|
||||
shopId: '',
|
||||
shopName: ''
|
||||
}
|
||||
} as Product
|
||||
})
|
||||
}
|
||||
|
||||
@@ -146,50 +132,53 @@ const goToDetail = (id: string) => {
|
||||
})
|
||||
}
|
||||
|
||||
const removeFavorite = async (id: string) => {
|
||||
const removeFavorite = (id: string) => {
|
||||
uni.showModal({
|
||||
title: '取消收藏',
|
||||
content: '确定要取消收藏该商品吗?',
|
||||
success: async (res) => {
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
// toggleFavorite 返回最新的状态:true=已收藏,false=未收藏
|
||||
const isStillFavorite = await supabaseService.toggleFavorite(id)
|
||||
|
||||
if (!isStillFavorite) {
|
||||
// 现在的状态是"未收藏",说明取消成功
|
||||
// Remove from local list
|
||||
const index = favorites.value.findIndex(item => item.id === id)
|
||||
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)
|
||||
favorites.value.splice(index, 1)
|
||||
}
|
||||
uni.showToast({
|
||||
title: '已取消收藏',
|
||||
icon: 'none'
|
||||
title: '已取消收藏',
|
||||
icon: 'none'
|
||||
})
|
||||
} else {
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '取消失败',
|
||||
icon: 'none'
|
||||
title: '取消失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
loadFavorites()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.favorites-page {
|
||||
padding: 15px;
|
||||
background-color: #f5f5f5;
|
||||
min-height: 100vh;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.product-grid {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
gap: 15px;
|
||||
justify-content: space-between;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
@@ -224,7 +213,7 @@ const removeFavorite = async (id: string) => {
|
||||
}
|
||||
|
||||
.product-item {
|
||||
width: calc(50% - 8px); /* Default Mobile: 2 items per row */
|
||||
width: 48%; /* Default Mobile: 2 items per row */
|
||||
background-color: white;
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
@@ -236,13 +225,13 @@ const removeFavorite = async (id: string) => {
|
||||
/* PC/Tablet Responsive */
|
||||
@media (min-width: 768px) {
|
||||
.product-item {
|
||||
width: calc(33.33% - 10px) !important; /* Tablet: 3 items (gap 15px roughly distributed) */
|
||||
width: 31% !important; /* Tablet: 3 items (gap 15px roughly distributed) */
|
||||
}
|
||||
}
|
||||
|
||||
@media (min-width: 1024px) {
|
||||
.product-item {
|
||||
width: calc(16.66% - 13px) !important; /* PC: 6 items */
|
||||
width: 15% !important; /* PC: 6 items */
|
||||
}
|
||||
|
||||
/* Center content on large screens */
|
||||
@@ -269,9 +258,8 @@ const removeFavorite = async (id: string) => {
|
||||
font-size: 14px;
|
||||
color: #333;
|
||||
margin-bottom: 6px;
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
text-overflow: ellipsis;
|
||||
lines: 2;
|
||||
overflow: hidden;
|
||||
height: 40px;
|
||||
line-height: 20px;
|
||||
@@ -300,16 +288,16 @@ const removeFavorite = async (id: string) => {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.cart-btn, .remove-btn {
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border-radius: 50%;
|
||||
border-radius: 14px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
margin-left: 8px; /* Replacement for gap */
|
||||
}
|
||||
|
||||
.cart-btn {
|
||||
@@ -328,4 +316,4 @@ const removeFavorite = async (id: string) => {
|
||||
.remove-icon {
|
||||
font-size: 14px;
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user