- 添加邀请记录、统计、来源统计、小程序码等数据模型 - 实现小程序码生成、邀请关系绑定、邀请场景处理等接口 - 新增扫码登录相关接口,支持生成二维码、检查状态、确认登录等操作 - 实现二维码内容解析和设备信息获取工具函数 - 添加礼品卡核销相关接口和解密工具函数 - 集成环境配置管理,支持开发、生产、测试环境切换 - 在过期时间页面集成登录二维码和核销二维码处理逻辑 - 添加邀请参数解析工具函数,支持从小程序启动参数中提取邀请信息
137 lines
3.3 KiB
TypeScript
137 lines
3.3 KiB
TypeScript
import { useState, useEffect, useCallback } from 'react'
|
|
import {pageShopUserCoupon} from "@/api/shop/shopUserCoupon";
|
|
import {pageShopGift} from "@/api/shop/shopGift";
|
|
import {useUser} from "@/hooks/useUser";
|
|
import Taro from '@tarojs/taro'
|
|
import {getUserInfo} from "@/api/layout";
|
|
|
|
interface UserData {
|
|
balance: number
|
|
points: number
|
|
coupons: number
|
|
giftCards: number
|
|
orders: {
|
|
pending: number
|
|
paid: number
|
|
shipped: number
|
|
completed: number
|
|
refund: number
|
|
}
|
|
}
|
|
|
|
interface UseUserDataReturn {
|
|
data: UserData | null
|
|
loading: boolean
|
|
error: string | null
|
|
refresh: () => Promise<void>
|
|
updateBalance: (newBalance: number) => void
|
|
updatePoints: (newPoints: number) => void
|
|
}
|
|
|
|
export const useUserData = (): UseUserDataReturn => {
|
|
const [data, setData] = useState<UserData | null>(null)
|
|
const [loading, setLoading] = useState(true)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
// 获取用户数据
|
|
const fetchUserData = useCallback(async () => {
|
|
try {
|
|
setLoading(true)
|
|
setError(null)
|
|
|
|
if(!Taro.getStorageSync('UserId')){
|
|
return;
|
|
}
|
|
|
|
// 并发请求所有数据
|
|
const [userDataRes, couponsRes, giftCardsRes] = await Promise.all([
|
|
getUserInfo(),
|
|
pageShopUserCoupon({ page: 1, limit: 1, userId: Taro.getStorageSync('UserId'), status: 0}),
|
|
pageShopGift({ page: 1, limit: 1, userId: Taro.getStorageSync('UserId'), status: 0})
|
|
])
|
|
|
|
const newData: UserData = {
|
|
balance: userDataRes?.balance || 0.00,
|
|
points: userDataRes?.points || 0,
|
|
coupons: couponsRes?.count || 0,
|
|
giftCards: giftCardsRes?.count || 0,
|
|
orders: {
|
|
pending: 0,
|
|
paid: 0,
|
|
shipped: 0,
|
|
completed: 0,
|
|
refund: 0
|
|
}
|
|
}
|
|
|
|
setData(newData)
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : '获取用户数据失败')
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}, [])
|
|
|
|
// 刷新数据
|
|
const refresh = useCallback(async () => {
|
|
await fetchUserData()
|
|
}, [fetchUserData])
|
|
|
|
// 更新余额(本地更新,避免频繁请求)
|
|
const updateBalance = useCallback((newBalance: number) => {
|
|
setData(prev => prev ? { ...prev, balance: newBalance } : null)
|
|
}, [])
|
|
|
|
// 更新积分
|
|
const updatePoints = useCallback((newPoints: number) => {
|
|
setData(prev => prev ? { ...prev, points: newPoints } : null)
|
|
}, [])
|
|
|
|
// 初始化加载
|
|
useEffect(() => {
|
|
fetchUserData().then()
|
|
}, [fetchUserData])
|
|
|
|
return {
|
|
data,
|
|
loading,
|
|
error,
|
|
refresh,
|
|
updateBalance,
|
|
updatePoints
|
|
}
|
|
}
|
|
|
|
// 轻量级版本 - 只获取基础数据
|
|
export const useUserBasicData = () => {
|
|
const {user} = useUser()
|
|
const [balance, setBalance] = useState<number>(0)
|
|
const [points, setPoints] = useState<number>(0)
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
const fetchBasicData = useCallback(async () => {
|
|
setLoading(true)
|
|
try {
|
|
setBalance(user?.balance || 0)
|
|
setPoints(user?.points || 0)
|
|
} catch (error) {
|
|
console.error('获取基础数据失败:', error)
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
fetchBasicData().then()
|
|
}, [fetchBasicData])
|
|
|
|
return {
|
|
balance,
|
|
points,
|
|
loading,
|
|
refresh: fetchBasicData,
|
|
updateBalance: setBalance,
|
|
updatePoints: setPoints
|
|
}
|
|
}
|