import { useState } from 'react' import { View, Text, ScrollView, Swiper, SwiperItem, Image } from '@tarojs/components' import Taro from '@tarojs/taro' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { publicAdvertisementClient, wechatCouponStockClient } from '@/api' import { Button } from '@/components/ui/button' import { Card, CardContent, CardHeader } from '@/components/ui/card' import { cn } from '@/utils/cn' import { receiveCoupon } from '@/utils/coupon-api' import { Navbar } from '@/components/ui/navbar' import { TabBarLayout } from '@/layouts/tab-bar-layout' import { useAuth } from '@/utils/auth' import type { InferResponseType } from 'hono/client' // 使用RPC类型安全提取广告响应类型 type AdvertisementResponse = InferResponseType type Advertisement = AdvertisementResponse['data'][0] interface WechatCouponStock { id: number stockName: string stockDescription: string stockType: string couponAmount: number couponTotal: number couponRemaining: number beginTime: string endTime: string status: number coverImage: string } export default function IndexPage() { const queryClient = useQueryClient() const { user, isLoggedIn } = useAuth() // 获取广告列表 const { data: advertisements, isLoading: adsLoading } = useQuery({ queryKey: ['advertisements'], queryFn: async () => { const response = await publicAdvertisementClient.$get({ query: { page: 1, pageSize: 10, filters: JSON.stringify({ isEnabled: 1 }), }, }) if (response.status !== 200) throw new Error('获取广告失败') const result = await response.json() return result.data }, }) // 获取代金券批次列表 const { data: stocks, isLoading: stocksLoading } = useQuery({ queryKey: ['wechat-coupon-stocks'], queryFn: async () => { const response = await wechatCouponStockClient.$get({ query: { page: 1, pageSize: 20, filters: JSON.stringify({ status: 1 }), }, }) if (response.status !== 200) throw new Error('获取批次失败') const result = await response.json() return result.data as WechatCouponStock[] }, }) // 领取代金券 const receiveMutation = useMutation({ mutationFn: async (stockId: number) => { if (!user?.id) { throw new Error('请先登录') } const result = await receiveCoupon({ stockId, userId: user.id }) if (!result.success) { throw new Error(result.message) } return result }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['wechat-coupon-stocks'] }) Taro.showToast({ title: '领取成功', icon: 'success', }) }, onError: (error) => { if (error instanceof Error && error.message === '请先登录') { Taro.showModal({ title: '提示', content: '请先登录后再领取优惠券', success: (res) => { if (res.confirm) { Taro.navigateTo({ url: '/pages/login/index' }) } } }) } else { Taro.showToast({ title: error instanceof Error ? error.message : '领取失败', icon: 'none', }) } } }) const handleReceiveCoupon = (stockId: number) => { if (!isLoggedIn) { Taro.showModal({ title: '提示', content: '请先登录后再领取优惠券', success: (res) => { if (res.confirm) { Taro.navigateTo({ url: '/pages/login/index' }) } } }) return } receiveMutation.mutate(stockId) } const handleAdClick = (ad: Advertisement) => { if (ad.linkUrl) { Taro.navigateTo({ url: `/pages/webview/index?url=${encodeURIComponent(ad.linkUrl)}`, }) } } return ( {/* 广告轮播 */} {adsLoading ? ( 加载中... ) : advertisements && advertisements.length > 0 ? ( {advertisements.map((ad) => ( handleAdClick(ad)}> ))} ) : ( 暂无广告 )} {/* 代金券批次列表 */} 热门券包 限时领取,先到先得 {stocksLoading ? ( 加载中... ) : stocks && stocks.length > 0 ? ( {stocks.map((stock) => ( {stock.stockName} ¥{stock.couponAmount} {stock.stockDescription} 剩余: {stock.couponRemaining}/{stock.couponTotal} {new Date(stock.endTime).toLocaleDateString()} 截止 ))} ) : ( 暂无券包 )} ) }