feat(pages): 添加管理页面功能和配置
- 创建 .editorconfig 文件统一代码风格配置 - 配置 .eslintrc 使用 taro/react 规则集 - 完善 .gitignore 忽略编译产物和敏感文件 - 添加 admin/article/add 页面实现文章管理功能 - 添加 dealer/apply/add 页面实现经销商申请功能 - 添加 dealer/bank/add 页面实现银行卡管理功能 - 添加 dealer/customer/add 页面实现客户管理功能 - 添加 user/address/add 页面实现用户地址管理功能 - 添加 user/chat/message/add 页面实现消息功能 - 添加 user/gift/add 页面实现礼品管理功能 - 配置各页面导航栏标题和样式 - 实现表单验证和数据提交功能 - 集成图片上传和头像选择功能 - 添加日期选择和数据校验逻辑 - 实现编辑和新增模式切换 - 集成用户权限和角色管理功能
This commit is contained in:
140
src/hooks/useUserData.ts
Normal file
140
src/hooks/useUserData.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import {pageShopUserCoupon} from "@/api/shop/shopUserCoupon";
|
||||
import {pageShopGift} from "@/api/shop/shopGift";
|
||||
import Taro from '@tarojs/taro'
|
||||
import {getUserInfo} from "@/api/layout";
|
||||
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: 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 () => {
|
||||
// 检查用户ID是否存在(更直接的登录状态检查)
|
||||
const userId = Taro.getStorageSync('UserId')
|
||||
if (!userId) {
|
||||
setLoading(false)
|
||||
setData(null)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
setLoading(true)
|
||||
setError(null)
|
||||
|
||||
// 并发请求所有数据
|
||||
const [userDataRes, couponsRes, giftCardsRes] = await Promise.all([
|
||||
getUserInfo(),
|
||||
pageShopUserCoupon({ page: 1, limit: 1, userId, status: 0}),
|
||||
pageShopGift({ page: 1, limit: 1, 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user