From 020315838ba0b5cfd2973e1e94fec8fdc2b9a22a Mon Sep 17 00:00:00 2001 From: pcn_ Date: Sat, 13 Sep 2025 03:42:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E6=BB=9A=E5=8A=A8=E5=9B=BE?= =?UTF-8?q?=E8=BD=AE=E6=92=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/index/BestSellers.tsx | 185 +++++++++++++++++++++++--------- 1 file changed, 132 insertions(+), 53 deletions(-) diff --git a/src/pages/index/BestSellers.tsx b/src/pages/index/BestSellers.tsx index b10a93f..8d20d94 100644 --- a/src/pages/index/BestSellers.tsx +++ b/src/pages/index/BestSellers.tsx @@ -1,21 +1,55 @@ -import {useEffect, useState} from "react"; -import {Image} from '@nutui/nutui-react-taro' -import {Share} from '@nutui/icons-react-taro' -import {View, Text} from '@tarojs/components'; -import Taro, {useShareAppMessage} from "@tarojs/taro"; -import {ShopGoods} from "@/api/shop/shopGoods/model"; -import {pageShopGoods} from "@/api/shop/shopGoods"; +import { useEffect, useState } from "react"; +import { Image, Swiper, SwiperItem } from '@nutui/nutui-react-taro' +import { Share } from '@nutui/icons-react-taro' +import { View, Text } from '@tarojs/components'; +import Taro from "@tarojs/taro"; +import { ShopGoods } from "@/api/shop/shopGoods/model"; +import { pageShopGoods } from "@/api/shop/shopGoods"; import './BestSellers.scss' const BestSellers = () => { const [list, setList] = useState([]) - const [goods, setGoods] = useState() + const [goods, setGoods] = useState(null) + // 轮播图固定高度,可根据需求调整 + const SWIPER_HEIGHT = 180; const reload = () => { pageShopGoods({}).then(res => { - setList(res?.list || []); - }) + const processGoodsItem = (item: ShopGoods) => { + const pics: string[] = []; + // 添加主图 + if (item.image) { + pics.push(item.image); + } + // 处理附加图片 + if (item.files) { + try { + // 解析文件字符串为对象 + const files = typeof item.files === "string" + ? JSON.parse(item.files) + : item.files; + + // 收集所有图片URL + Object.values(files).forEach(file => { + if (file?.url) { + pics.push(file.url); + } + }); + } catch (error) { + console.error('解析文件失败:', error); + } + } + // 返回新对象,避免直接修改原对象 + return { ...item, pics }; + }; + + // 处理商品列表 + const goods = (res?.list || []).map(processGoodsItem); + setList(goods); + }).catch(err => { + console.error('获取商品列表失败:', err); + }); } // 处理分享点击 @@ -27,11 +61,9 @@ const BestSellers = () => { itemList: ['分享给好友'], success: (res) => { if (res.tapIndex === 0) { - // 分享给好友 - 触发转发 Taro.showShareMenu({ withShareTicket: true, success: () => { - // 提示用户点击右上角分享 Taro.showToast({ title: '请点击右上角分享给好友', icon: 'none', @@ -48,56 +80,103 @@ const BestSellers = () => { } useEffect(() => { - reload() - }, []) + reload(); + }, []); - // 注意:不在这里配置分享,避免与首页分享冲突 - // 商品分享应该在商品详情页处理,首页分享应该分享首页本身 + // 配置分享内容 + Taro.useShareAppMessage(() => { + if (goods) { + return { + title: goods.name, + path: `/shop/goodsDetail/index?id=${goods.goodsId}`, + imageUrl: goods.image || '' + }; + } + return { + title: '热销商品', + path: '/pages/index/index' + }; + }); return ( - <> - - - {list?.map((item, index) => { - return ( - - Taro.navigateTo({url: '/shop/goodsDetail/index?id=' + item.goodsId})}/> - - - {item.name} - - {item.comments} - 已售 {item.sales} - - - - - {item.price} - - - - handleShare(item)} - > - - - - Taro.navigateTo({url: '/shop/goodsDetail/index?id=' + item.goodsId})}>购买 - + + + {list?.map((item) => ( + + {/* 轮播图组件 */} + {item.pics && item.pics.length > 0 ? ( + + {item.pics.map((pic, picIndex) => ( + + Taro.navigateTo({ + url: `/shop/goodsDetail/index?id=${item.goodsId}` + })} + className="swiper-image" + /> + + ))} + + ) : ( + // 没有图片时显示占位图 + + 暂无图片 + + )} + + + + {item.name} + + {item.comments} + 已售 {item.sales} + + + + + {item.price} + + + + handleShare(item)} + > + + Taro.navigateTo({ + url: `/shop/goodsDetail/index?id=${item.goodsId}` + })} + > + 购买 + - ) - })} - + + + ))} - + ) } + export default BestSellers