feat(glt): 完善水票总数获取逻辑并优化用户体验
- 新增 normalizeTotal 函数处理多种数据格式的总数解析 - 支持通过 userId 参数查询指定用户的水票总数 - 添加多服务器地址尝试机制提高接口可用性 - 优化首页和用户中心页面的水票总数加载逻辑 - 修复水票页面滚动区域高度计算问题 - 移除自动跳转登录页面的定时器逻辑 - 个人中心页面支持下拉刷新统计数据 - 统一请求参数传递方式简化代码结构
This commit is contained in:
@@ -90,11 +90,14 @@ function Home() {
|
||||
|
||||
const reload = () => {
|
||||
const token = Taro.getStorageSync('access_token')
|
||||
if (!token) {
|
||||
const userIdRaw = Taro.getStorageSync('UserId')
|
||||
const userId = Number(userIdRaw)
|
||||
const hasUserId = Number.isFinite(userId) && userId > 0
|
||||
if (!token && !hasUserId) {
|
||||
setTicketTotal(0)
|
||||
return
|
||||
}
|
||||
getMyGltUserTicketTotal()
|
||||
getMyGltUserTicketTotal(hasUserId ? userId : undefined)
|
||||
.then((total) => setTicketTotal(typeof total === 'number' ? total : 0))
|
||||
.catch((err) => {
|
||||
console.error('首页水票总数加载失败:', err)
|
||||
|
||||
@@ -29,18 +29,21 @@ const UserCard = forwardRef<any, any>((_, ref) => {
|
||||
}
|
||||
|
||||
// 下拉刷新
|
||||
const handleRefresh = async () => {
|
||||
const reloadStats = async (showToast = false) => {
|
||||
await refresh()
|
||||
reloadTicketTotal()
|
||||
Taro.showToast({
|
||||
title: '刷新成功',
|
||||
icon: 'success'
|
||||
})
|
||||
if (showToast) {
|
||||
Taro.showToast({
|
||||
title: '刷新成功',
|
||||
icon: 'success'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 暴露方法给父组件
|
||||
useImperativeHandle(ref, () => ({
|
||||
handleRefresh
|
||||
handleRefresh: () => reloadStats(true),
|
||||
reloadStats
|
||||
}))
|
||||
|
||||
useEffect(() => {
|
||||
@@ -65,11 +68,14 @@ const UserCard = forwardRef<any, any>((_, ref) => {
|
||||
|
||||
const reloadTicketTotal = () => {
|
||||
const token = Taro.getStorageSync('access_token')
|
||||
if (!token) {
|
||||
const userIdRaw = Taro.getStorageSync('UserId')
|
||||
const userId = Number(userIdRaw)
|
||||
const hasUserId = Number.isFinite(userId) && userId > 0
|
||||
if (!token && !hasUserId) {
|
||||
setTicketTotal(0)
|
||||
return
|
||||
}
|
||||
getMyGltUserTicketTotal()
|
||||
getMyGltUserTicketTotal(hasUserId ? userId : undefined)
|
||||
.then((total) => setTicketTotal(typeof total === 'number' ? total : 0))
|
||||
.catch((err) => {
|
||||
console.error('个人中心水票总数加载失败:', err)
|
||||
@@ -302,7 +308,7 @@ const UserCard = forwardRef<any, any>((_, ref) => {
|
||||
<Text className={'text-xl text-white'} style={themeStyles.textColor}>{data?.coupons || 0}</Text>
|
||||
</View>
|
||||
<View className={'item flex justify-center flex-col items-center'}
|
||||
onClick={() => navTo('/user/gift/index', true)}>
|
||||
onClick={() => navTo('/user/ticket/index', true)}>
|
||||
<Text className={'text-xs text-gray-200'} style={themeStyles.textColor}>水票</Text>
|
||||
<Text className={'text-xl text-white'} style={themeStyles.textColor}>{ticketTotal}</Text>
|
||||
</View>
|
||||
|
||||
@@ -8,6 +8,7 @@ import './user.scss'
|
||||
import IsDealer from "./components/IsDealer";
|
||||
import {useThemeStyles} from "@/hooks/useTheme";
|
||||
import UserGrid from "@/pages/user/components/UserGrid";
|
||||
import { useDidShow } from '@tarojs/taro'
|
||||
|
||||
function User() {
|
||||
|
||||
@@ -24,6 +25,11 @@ function User() {
|
||||
useEffect(() => {
|
||||
}, []);
|
||||
|
||||
// 每次进入/切回个人中心都刷新一次统计(包含水票数量)
|
||||
useDidShow(() => {
|
||||
userCardRef.current?.reloadStats?.()
|
||||
})
|
||||
|
||||
return (
|
||||
<PullToRefresh
|
||||
onRefresh={handleRefresh}
|
||||
|
||||
Reference in New Issue
Block a user