98 lines
2.0 KiB
JavaScript
98 lines
2.0 KiB
JavaScript
const wishlistApi = require('../../api/wishlist')
|
|||
|
|
const auth = require('../../utils/auth')
|
||
|
|
const navigation = require('../../utils/navigation')
|
||
|
|
const { buildProductCard } = require('../../utils/product')
|
||
|
|
const { runWithLoading } = require('../../utils/loading')
|
||
|
|
|
||
|
|
Page({
|
||
|
|
data: {
|
||
|
|
loading: false,
|
||
|
|
isLoggedIn: false,
|
||
|
|
wishlist: []
|
||
|
|
},
|
||
|
|
|
||
|
|
onShow() {
|
||
|
|
this.loadWishlist()
|
||
|
|
},
|
||
|
|
|
||
|
|
async loadWishlist() {
|
||
|
|
const isLoggedIn = auth.isLoggedIn()
|
||
|
|
this.setData({
|
||
|
|
isLoggedIn,
|
||
|
|
loading: isLoggedIn
|
||
|
|
})
|
||
|
|
|
||
|
|
if (!isLoggedIn) {
|
||
|
|
this.setData({
|
||
|
|
wishlist: [],
|
||
|
|
loading: false
|
||
|
|
})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
const wishlist = await wishlistApi.getWishlist()
|
||
|
|
const list = Array.isArray(wishlist) ? wishlist : []
|
||
|
|
const cards = await Promise.all(list.map(async item => ({
|
||
|
|
id: item.id,
|
||
|
|
productId: item.productId,
|
||
|
|
productCard: await buildProductCard(item.product || {})
|
||
|
|
})))
|
||
|
|
this.setData({
|
||
|
|
wishlist: cards
|
||
|
|
})
|
||
|
|
} catch (error) {
|
||
|
|
this.setData({
|
||
|
|
wishlist: []
|
||
|
|
})
|
||
|
|
wx.showToast({
|
||
|
|
title: error.message || '收藏列表加载失败',
|
||
|
|
icon: 'none'
|
||
|
|
})
|
||
|
|
} finally {
|
||
|
|
this.setData({
|
||
|
|
loading: false
|
||
|
|
})
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
handleOpenAuth() {
|
||
|
|
navigation.openAuth('/pages/wishlist/index')
|
||
|
|
},
|
||
|
|
|
||
|
|
handleProductSelect(event) {
|
||
|
|
const product = event.detail || {}
|
||
|
|
if (!product.id) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
navigation.navigate('/pages/product-detail/index', {
|
||
|
|
id: product.id
|
||
|
|
})
|
||
|
|
},
|
||
|
|
|
||
|
|
async handleRemove(event) {
|
||
|
|
const productId = event.currentTarget.dataset.productId
|
||
|
|
if (!productId) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
await runWithLoading(() => wishlistApi.removeFromWishlist(productId))
|
||
|
|
wx.showToast({
|
||
|
|
title: '已取消收藏',
|
||
|
|
icon: 'none'
|
||
|
|
})
|
||
|
|
this.loadWishlist()
|
||
|
|
} catch (error) {
|
||
|
|
wx.showToast({
|
||
|
|
title: error.message || '取消收藏失败',
|
||
|
|
icon: 'none'
|
||
|
|
})
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
handleBack() {
|
||
|
|
navigation.goBack('/pages/profile/index')
|
||
|
|
}
|
||
|
|
})
|