import { useEffect, useState } from 'react'; import { View, Text } from '@tarojs/components'; import Taro, { useDidShow } from '@tarojs/taro'; import PageShell from '../../components/PageShell'; import TabMainHeader from '../../components/TabMainHeader'; import CouponBadge from '../../components/CouponBadge'; import ProductCarousel from '../../components/ProductCarousel'; import UserTabBar, { shouldRenderPageTabBar, syncTabBarSelected } from '../../components/UserTabBar'; import { request, toast } from '../../lib/api'; import { getProductImages } from '../../lib/product-images'; import { getCityCodeForCatalog, resolveUserCity } from '../../lib/user-location'; type Product = { id: string; name: string; subtitle?: string; price: number; benefitDisplay?: number; mainImageUrl?: string | null; carouselUrls?: string[] | null; aromaType: string; }; const AROMA_TABS = [ { key: 'QINGXIANG', label: '清香型', open: true }, { key: 'JIANGXIANG', label: '酱香型', open: false }, { key: 'NONGXIANG', label: '浓香型', open: false }, ] as const; export default function HomePage() { const [tab, setTab] = useState('QINGXIANG'); const [products, setProducts] = useState([]); const [loading, setLoading] = useState(true); const [displayCity, setDisplayCity] = useState('郑州市'); const [cityCode, setCityCode] = useState('410100'); useDidShow(() => { syncTabBarSelected(0); void resolveUserCity().then((resolved) => { setDisplayCity(resolved.displayCity); setCityCode(getCityCodeForCatalog(resolved)); }); }); useEffect(() => { setLoading(true); request(`/catalog/products?cityCode=${encodeURIComponent(cityCode)}`) .then((list) => setProducts(Array.isArray(list) ? list : [])) .catch((e) => toast(e instanceof Error ? e.message : '加载失败')) .finally(() => setLoading(false)); }, [cityCode]); function onAromaTabClick(key: string, open: boolean) { if (!open) { toast('暂未开放'); return; } setTab(key); } function openProductDetail(id: string) { Taro.navigateTo({ url: `/pages/product-detail/index?id=${id}` }); } const filtered = products.filter((p) => p.aromaType === tab); const onSale = tab === 'QINGXIANG'; return ( {AROMA_TABS.map((t) => ( onAromaTabClick(t.key, t.open)} > {t.label} ))} {displayCity} {loading ? 加载中… : null} {!loading && !onSale ? 该香型暂未上线,敬请期待 : null} {!loading && onSale && filtered.length === 0 ? ( 暂无商品 ) : null} {!loading && onSale && filtered.map((p) => { const images = getProductImages(p); return ( openProductDetail(p.id)}> {p.name} ¥{Number(p.price).toFixed(2)} {p.subtitle ? {p.subtitle} : null} openProductDetail(p.id)}> 立即购买 ); })} {shouldRenderPageTabBar() ? : null} ); }