From cb17e48b03b34ed5898dc2c0b5efb5012bbf32d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BF=A0=E6=9E=97?= <170083662@qq.com> Date: Tue, 3 Feb 2026 20:06:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(home):=20=E4=BC=98=E5=8C=96=E9=A6=96?= =?UTF-8?q?=E9=A1=B5=E5=95=86=E5=93=81=E5=B1=95=E7=A4=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 recommend 字段到商品模型定义 - 重构首页标签页逻辑,支持分类参数传递 - 实现动态商品列表加载,按标签分类获取数据 - 更新订水跳转链接指向新商品ID - 优化标签页切换逻辑,使用键值对映射 - 添加错误处理机制,防止商品列表加载失败 --- src/api/shop/shopGoods/model/index.ts | 1 + src/pages/index/index.tsx | 43 +++++++++++++++++++-------- src/shop/gift/index.config.ts | 4 +++ src/shop/gift/index.tsx | 0 4 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 src/shop/gift/index.config.ts create mode 100644 src/shop/gift/index.tsx diff --git a/src/api/shop/shopGoods/model/index.ts b/src/api/shop/shopGoods/model/index.ts index ce4328f..39e372e 100644 --- a/src/api/shop/shopGoods/model/index.ts +++ b/src/api/shop/shopGoods/model/index.ts @@ -146,4 +146,5 @@ export interface ShopGoodsParam extends PageParam { isShow?: number; stock?: number; keywords?: string; + recommend?: number; } diff --git a/src/pages/index/index.tsx b/src/pages/index/index.tsx index 1fa973a..2c36ff5 100644 --- a/src/pages/index/index.tsx +++ b/src/pages/index/index.tsx @@ -7,11 +7,11 @@ import { Cart, Coupon, Gift, Ticket } from '@nutui/icons-react-taro' import { getShopInfo } from '@/api/layout' import { checkAndHandleInviteRelation, hasPendingInvite } from '@/utils/invite' import { pageShopGoods } from '@/api/shop/shopGoods' -import type { ShopGoods } from '@/api/shop/shopGoods/model' +import type { ShopGoods, ShopGoodsParam } from '@/api/shop/shopGoods/model' import './index.scss' function Home() { - const [activeTab, setActiveTab] = useState('推荐') + const [activeTabKey, setActiveTabKey] = useState('recommend') const [goodsList, setGoodsList] = useState([]) useShareAppMessage(() => { @@ -96,10 +96,6 @@ function Home() { }) - pageShopGoods({}).then(res => { - setGoodsList(res?.list || []) - }) - // 检查是否有待处理的邀请关系 - 异步处理,不阻塞页面加载 if (hasPendingInvite()) { console.log('检测到待处理的邀请关系') @@ -153,7 +149,30 @@ function Home() { }); }, []); - const tabs = useMemo(() => ['推荐', '桶装水', '优惠组合', '购机套餐', '饮水设备'], []) + const tabs = useMemo< + Array<{ key: string; title: string; params: Partial }> + >( + () => [ + { key: 'recommend', title: '推荐', params: { recommend: 1 } }, + { key: '4476', title: '桶装水', params: { categoryId: 4476 } }, + { key: '4556', title: '优惠组合', params: { categoryId: 4556 } }, + { key: '4557', title: '购机套餐', params: { categoryId: 4557 } }, + { key: '4477', title: '饮水设备', params: { categoryId: 4477 } }, + ], + [] + ) + + useEffect(() => { + const tab = tabs.find((t) => t.key === activeTabKey) || tabs[0] + if (!tab) return + + pageShopGoods({ ...tab.params }) + .then((res) => setGoodsList(res?.list || [])) + .catch((err) => { + console.error('首页商品列表加载失败:', err) + setGoodsList([]) + }) + }, [activeTabKey, tabs]) const shortcuts = useMemo< Array<{ key: string; title: string; icon: ReactNode; onClick: () => void }> @@ -169,7 +188,7 @@ function Home() { key: 'order', title: '立即订水', icon: , - onClick: () => Taro.navigateTo({ url: '/shop/goodsDetail/index?id=10072' }), + onClick: () => Taro.navigateTo({ url: '/shop/goodsDetail/index?id=10074' }), }, { key: 'invite', @@ -232,14 +251,14 @@ function Home() { {tabs.map((tab) => { - const active = tab === activeTab + const active = tab.key === activeTabKey return ( setActiveTab(tab)} + onClick={() => setActiveTabKey(tab.key)} > - {tab} + {tab.title} ) })} diff --git a/src/shop/gift/index.config.ts b/src/shop/gift/index.config.ts new file mode 100644 index 0000000..3dcbab8 --- /dev/null +++ b/src/shop/gift/index.config.ts @@ -0,0 +1,4 @@ +export default definePageConfig({ + navigationBarTitleText: '立即订水', + navigationBarTextStyle: 'black' +}) diff --git a/src/shop/gift/index.tsx b/src/shop/gift/index.tsx new file mode 100644 index 0000000..e69de29