Browse Source
- 新增 useUserData 钩子用于获取用户数据 - 添加用户仪表板相关接口和类型定义 - 更新用户卡片组件,使用新的用户数据钩子 - 修改成为经销商文案为开通VIPdev
5 changed files with 300 additions and 38 deletions
@ -0,0 +1,131 @@ |
|||||
|
import request from '@/utils/request-legacy' |
||||
|
import type { ApiResult } from '@/api/index' |
||||
|
|
||||
|
// 用户余额信息
|
||||
|
export interface UserBalance { |
||||
|
balance: string |
||||
|
frozenBalance: string |
||||
|
totalIncome: string |
||||
|
totalExpense: string |
||||
|
} |
||||
|
|
||||
|
// 用户积分信息
|
||||
|
export interface UserPoints { |
||||
|
points: number |
||||
|
totalEarned: number |
||||
|
totalUsed: number |
||||
|
expiringSoon: number |
||||
|
} |
||||
|
|
||||
|
// 用户优惠券统计
|
||||
|
export interface UserCoupons { |
||||
|
count: number |
||||
|
available: number |
||||
|
used: number |
||||
|
expired: number |
||||
|
} |
||||
|
|
||||
|
// 用户礼品卡统计
|
||||
|
export interface UserGiftCards { |
||||
|
count: number |
||||
|
unused: number |
||||
|
used: number |
||||
|
expired: number |
||||
|
} |
||||
|
|
||||
|
// 用户订单统计
|
||||
|
export interface UserOrderStats { |
||||
|
pending: number |
||||
|
paid: number |
||||
|
shipped: number |
||||
|
completed: number |
||||
|
refund: number |
||||
|
total: number |
||||
|
} |
||||
|
|
||||
|
// 用户完整数据
|
||||
|
export interface UserDashboard { |
||||
|
balance: UserBalance |
||||
|
points: UserPoints |
||||
|
coupons: UserCoupons |
||||
|
giftCards: UserGiftCards |
||||
|
orders: UserOrderStats |
||||
|
lastUpdateTime: string |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取用户余额信息 |
||||
|
*/ |
||||
|
export async function getUserBalance() { |
||||
|
const res = await request.get<ApiResult<UserBalance>>('/user/balance') |
||||
|
if (res.code === 0 && res.data) { |
||||
|
return res.data |
||||
|
} |
||||
|
return Promise.reject(new Error(res.message)) |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取用户积分信息 |
||||
|
*/ |
||||
|
export async function getUserPoints() { |
||||
|
const res = await request.get<ApiResult<UserPoints>>('/user/points') |
||||
|
if (res.code === 0 && res.data) { |
||||
|
return res.data |
||||
|
} |
||||
|
return Promise.reject(new Error(res.message)) |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取用户优惠券统计 |
||||
|
*/ |
||||
|
export async function getUserCoupons() { |
||||
|
const res = await request.get<ApiResult<UserCoupons>>('/user/coupons/stats') |
||||
|
if (res.code === 0 && res.data) { |
||||
|
return res.data |
||||
|
} |
||||
|
return Promise.reject(new Error(res.message)) |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取用户礼品卡统计 |
||||
|
*/ |
||||
|
export async function getUserGiftCards() { |
||||
|
const res = await request.get<ApiResult<UserGiftCards>>('/user/gift-cards/stats') |
||||
|
if (res.code === 0 && res.data) { |
||||
|
return res.data |
||||
|
} |
||||
|
return Promise.reject(new Error(res.message)) |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取用户订单统计 |
||||
|
*/ |
||||
|
export async function getUserOrderStats() { |
||||
|
const res = await request.get<ApiResult<UserOrderStats>>('/user/orders/stats') |
||||
|
if (res.code === 0 && res.data) { |
||||
|
return res.data |
||||
|
} |
||||
|
return Promise.reject(new Error(res.message)) |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取用户完整仪表板数据(一次性获取所有数据) |
||||
|
*/ |
||||
|
export async function getUserDashboard() { |
||||
|
const res = await request.get<ApiResult<UserDashboard>>('/user/dashboard') |
||||
|
if (res.code === 0 && res.data) { |
||||
|
return res.data |
||||
|
} |
||||
|
return Promise.reject(new Error(res.message)) |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 刷新用户数据缓存 |
||||
|
*/ |
||||
|
export async function refreshUserData() { |
||||
|
const res = await request.post<ApiResult<unknown>>('/user/refresh-cache') |
||||
|
if (res.code === 0) { |
||||
|
return res.message |
||||
|
} |
||||
|
return Promise.reject(new Error(res.message)) |
||||
|
} |
@ -0,0 +1,118 @@ |
|||||
|
import { useState, useEffect, useCallback } from 'react' |
||||
|
import {pageShopUserCoupon} from "@/api/shop/shopUserCoupon"; |
||||
|
import {pageShopGift} from "@/api/shop/shopGift"; |
||||
|
import {useUser} from "@/hooks/useUser"; |
||||
|
|
||||
|
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: string) => void |
||||
|
updatePoints: (newPoints: number) => void |
||||
|
} |
||||
|
|
||||
|
export const useUserData = (): UseUserDataReturn => { |
||||
|
const {user} = useUser() |
||||
|
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) |
||||
|
|
||||
|
// 并发请求所有数据
|
||||
|
const [couponsRes, giftCardsRes] = await Promise.all([ |
||||
|
pageShopUserCoupon({ page: 1, limit: 1, userId: user?.userId}), |
||||
|
pageShopGift({ page: 1, limit: 1, userId: user?.userId, status: 0}) |
||||
|
]) |
||||
|
|
||||
|
const newData: UserData = { |
||||
|
balance: user?.balance || 0.00, |
||||
|
points: user?.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]) |
||||
|
|
||||
|
// 初始化加载
|
||||
|
useEffect(() => { |
||||
|
fetchUserData().then() |
||||
|
}, [fetchUserData]) |
||||
|
|
||||
|
return { |
||||
|
data, |
||||
|
loading, |
||||
|
error, |
||||
|
refresh: fetchUserData |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 轻量级版本 - 只获取基础数据
|
||||
|
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() |
||||
|
}, [fetchBasicData]) |
||||
|
|
||||
|
return { |
||||
|
balance, |
||||
|
points, |
||||
|
loading, |
||||
|
refresh: fetchBasicData, |
||||
|
updateBalance: setBalance, |
||||
|
updatePoints: setPoints |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue