- 将图片显示统一替换为 getCompressedImageUrl 函数处理压缩 - 覆盖 35 个文件,包括 4 个组件和 31 个页面 - 主要组件:LazyImage、ProductCard、OrderCard、SkuSelector 内部自动压缩图片 - 主要页面:购物车、订单、积分、活动、拼团、秒杀、收藏、浏览历史等 - 跳过用户头像、二维码、商品详情大图及所有 Taro.previewImage 调用 - 调整压缩默认宽度为 300,保持质量90,启用压缩功能
98 lines
2.9 KiB
TypeScript
98 lines
2.9 KiB
TypeScript
import React, { useState, useEffect } from 'react'
|
|
import { View, Text, Image, ScrollView } from '@tarojs/components'
|
|
import Taro from '@tarojs/taro'
|
|
import { listShopGoodsFavorite } from '@/api/shop/shopGoodsFavorite'
|
|
import type { ShopGoodsFavorite } from '@/api/shop/shopGoodsFavorite/model'
|
|
import EmptyState from '@/components/common/EmptyState'
|
|
import LoadMore from '@/components/common/LoadMore'
|
|
import { getCompressedImageUrl } from '@/utils/image'
|
|
|
|
definePageConfig({
|
|
navigationBarTitleText: '我的收藏',
|
|
})
|
|
|
|
const FavoriteListPage: React.FC = () => {
|
|
const [list, setList] = useState<ShopGoodsFavorite[]>([])
|
|
const [loading, setLoading] = useState(false)
|
|
const [page, setPage] = useState(1)
|
|
const [finished, setFinished] = useState(false)
|
|
|
|
useEffect(() => {
|
|
loadFavorites(1)
|
|
}, [])
|
|
|
|
const loadFavorites = async (p: number) => {
|
|
if (loading) return
|
|
setLoading(true)
|
|
try {
|
|
const res = await listShopGoodsFavorite({ page: p, limit: 20 })
|
|
const newList = res || []
|
|
if (p === 1) {
|
|
setList(newList)
|
|
} else {
|
|
setList(prev => [...prev, ...newList])
|
|
}
|
|
setFinished(newList.length < 20)
|
|
setPage(p)
|
|
} catch {
|
|
Taro.showToast({ title: '加载失败', icon: 'none' })
|
|
}
|
|
setLoading(false)
|
|
}
|
|
|
|
const handleItemClick = (goodsId: number) => {
|
|
Taro.navigateTo({ url: `/pages/shop/product-detail?id=${goodsId}` })
|
|
}
|
|
|
|
const handleLoadMore = () => {
|
|
if (!finished && !loading) {
|
|
loadFavorites(page + 1)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<View className='min-h-screen bg-gray-50'>
|
|
<ScrollView
|
|
scrollY
|
|
className='h-screen'
|
|
onScrollToLower={handleLoadMore}
|
|
lowerThreshold={100}
|
|
>
|
|
<View className='p-3'>
|
|
{list.length > 0 ? (
|
|
<View className='grid grid-cols-2 gap-3'>
|
|
{list.map(item => (
|
|
<View
|
|
key={item.favoriteId}
|
|
className='bg-white rounded-lg overflow-hidden'
|
|
onClick={() => handleItemClick(item.goodsId!)}
|
|
>
|
|
<Image
|
|
className='w-full'
|
|
style={{ height: '160px' }}
|
|
src={getCompressedImageUrl(item.goodsImage)}
|
|
mode='aspectFill'
|
|
/>
|
|
<View className='p-2'>
|
|
<Text className='text-sm text-gray-800 line-clamp-2 block'>
|
|
{item.goodsName}
|
|
</Text>
|
|
<Text className='text-red-500 text-sm font-medium mt-1 block'>
|
|
¥{item.salePrice || '0'}
|
|
</Text>
|
|
</View>
|
|
</View>
|
|
))}
|
|
</View>
|
|
) : (
|
|
!loading && <EmptyState text='暂无收藏' />
|
|
)}
|
|
<LoadMore loading={loading} finished={finished} />
|
|
</View>
|
|
</ScrollView>
|
|
</View>
|
|
)
|
|
}
|
|
|
|
export default FavoriteListPage
|