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, useShareTimeline} 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 reload = () => { pageShopGoods({}).then(res => { setList(res?.list || []); }) } // 处理分享点击 const handleShare = (item: ShopGoods) => { setGoods(item); // 显示分享选项菜单 Taro.showActionSheet({ itemList: ['分享给好友', '分享到朋友圈'], success: (res) => { if (res.tapIndex === 0) { // 分享给好友 - 触发转发 Taro.showShareMenu({ withShareTicket: true, success: () => { // 提示用户点击右上角分享 Taro.showToast({ title: '请点击右上角分享给好友', icon: 'none', duration: 2000 }); } }); } else if (res.tapIndex === 1) { // 分享到朋友圈 Taro.showToast({ title: '请点击右上角分享到朋友圈', icon: 'none', duration: 2000 }); } }, fail: (err) => { console.log('显示分享菜单失败', err); } }); } useEffect(() => { reload() }, []) // 分享给好友 useShareAppMessage(() => { return { title: goods?.name || '精选商品', path: `/shop/goodsDetail/index?id=${goods?.goodsId}`, imageUrl: goods?.image, // 分享图片 success: function (res: any) { console.log('分享成功', res); Taro.showToast({ title: '分享成功', icon: 'success', duration: 2000 }); }, fail: function (res: any) { console.log('分享失败', res); Taro.showToast({ title: '分享失败', icon: 'none', duration: 2000 }); } }; }); // 分享到朋友圈 useShareTimeline(() => { return { title: `${goods?.name || '精选商品'} - 网宿小店`, path: `/shop/goodsDetail/index?id=${goods?.goodsId}`, imageUrl: goods?.image }; }); 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})}>购买 ) })} ) } export default BestSellers