consumer模块完成90%,前端完成supabase对接
This commit is contained in:
@@ -65,26 +65,69 @@ const loadFavorites = async () => {
|
||||
|
||||
// Map response
|
||||
favorites.value = res.map((item: any): Product => {
|
||||
const prod = item.ml_products
|
||||
let prod: any = null
|
||||
if (item instanceof UTSJSONObject) {
|
||||
prod = item.get('ml_products')
|
||||
} else {
|
||||
prod = item['ml_products']
|
||||
}
|
||||
|
||||
let image = '/static/default-product.png'
|
||||
if (prod) {
|
||||
if (prod.main_image_url) image = prod.main_image_url
|
||||
else if (prod.image_url) image = prod.image_url
|
||||
else if (prod.image_urls) {
|
||||
// Try parse
|
||||
try {
|
||||
const arr = JSON.parse(prod.image_urls)
|
||||
if (Array.isArray(arr) && arr.length > 0) image = arr[0]
|
||||
} catch(e) {}
|
||||
let id = ''
|
||||
let name = '未知商品'
|
||||
let price = 0
|
||||
let sales = 0
|
||||
|
||||
if (prod != null) {
|
||||
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) {}
|
||||
}
|
||||
}
|
||||
} 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) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 如果没取到商品,尝试直接从 item 取 target_id
|
||||
if (item instanceof UTSJSONObject) {
|
||||
id = item.getString('target_id') || ''
|
||||
} else {
|
||||
id = (item['target_id'] as string) || ''
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
id: prod?.id || item.target_id,
|
||||
name: prod?.name || '未知商品',
|
||||
price: prod?.price || 0,
|
||||
id: id,
|
||||
name: name,
|
||||
price: price,
|
||||
image: image,
|
||||
sales: prod?.sales || 0,
|
||||
sales: sales,
|
||||
shopId: '',
|
||||
shopName: ''
|
||||
}
|
||||
@@ -109,8 +152,11 @@ const removeFavorite = async (id: string) => {
|
||||
content: '确定要取消收藏该商品吗?',
|
||||
success: async (res) => {
|
||||
if (res.confirm) {
|
||||
const success = await supabaseService.toggleFavorite(id) // Toggle removes if exists
|
||||
if (success) {
|
||||
// toggleFavorite 返回最新的状态:true=已收藏,false=未收藏
|
||||
const isStillFavorite = await supabaseService.toggleFavorite(id)
|
||||
|
||||
if (!isStillFavorite) {
|
||||
// 现在的状态是"未收藏",说明取消成功
|
||||
// Remove from local list
|
||||
const index = favorites.value.findIndex(item => item.id === id)
|
||||
if (index !== -1) {
|
||||
@@ -120,6 +166,11 @@ const removeFavorite = async (id: string) => {
|
||||
title: '已取消收藏',
|
||||
icon: 'none'
|
||||
})
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '取消失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user