feat(user): 实现 useUser Hook 并更新相关组件

- 新增 useUser Hook 用于全局用户状态管理
- 更新 UserCard 和 UserCell 组件,集成 useUser 功能
- 添加 UserProfile 组件示例
- 更新 API 引用,统一使用 useUser
This commit is contained in:
2025-08-14 18:08:00 +08:00
parent 2c864ce770
commit 745040d254
6 changed files with 666 additions and 31 deletions

View File

@@ -8,16 +8,16 @@ import navTo from "@/utils/common";
import {TenantId} from "@/config/app";
import {getUserCouponCount} from "@/api/user/coupon";
import {getUserPointsStats} from "@/api/user/points";
import {useUser} from "@/hooks/useUser";
function UserCard() {
const {getDisplayName, getRoleName} = useUser();
const [IsLogin, setIsLogin] = useState<boolean>(false)
const [userInfo, setUserInfo] = useState<User>()
const [roleName, setRoleName] = useState<string>('注册用户')
const [couponCount, setCouponCount] = useState(0)
const [pointsCount, setPointsCount] = useState(0)
const [giftCount, setGiftCount] = useState(0)
useEffect(() => {
// Taro.getSetting获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限。
Taro.getSetting({
@@ -89,11 +89,6 @@ function UserCard() {
}
})
}
// 判断身份
const roleName = Taro.getStorageSync('RoleName');
if (roleName) {
setRoleName(roleName)
}
}
}).catch(() => {
console.log('未登录')
@@ -206,11 +201,13 @@ function UserCard() {
)
}
<div className={'user-info flex flex-col px-2'}>
<div className={'py-1 text-black font-bold'}>{IsLogin ? userInfo?.nickname : '请点击头像登录'}</div>
<div className={'py-1 text-black font-bold'}>{getDisplayName()}</div>
{IsLogin ? (
<div className={'grade text-xs py-1'}>
<Tag type="success" round>
<div className={'p-1'}>{roleName || '注册用户'}</div>
<div className={'p-1'}>
{getRoleName()}
</div>
</Tag>
</div>
) : ''}