feat(user): 优化用户权限管理与扫码功能
- 添加 isAdmin 状态检查逻辑支持多种数据类型 (true/1/'1') - 实现统一扫码按钮的管理员权限控制,仅管理员可查看 - 集成 saveStorageByLoginUser 工具函数统一处理登录用户信息存储 - 优化扫码取消操作的错误处理,区分用户主动取消与实际错误 - 同步本地存储中的用户信息以便其他钩子读取管理员标识
This commit is contained in:
@@ -13,15 +13,20 @@ import UnifiedQRButton from "@/components/UnifiedQRButton";
|
||||
import {useThemeStyles} from "@/hooks/useTheme";
|
||||
import {getRootDomain} from "@/utils/domain";
|
||||
import { getMyGltUserTicketTotal } from '@/api/glt/gltUserTicket'
|
||||
import { saveStorageByLoginUser } from '@/utils/server'
|
||||
|
||||
const UserCard = forwardRef<any, any>((_, ref) => {
|
||||
const {data, refresh} = useUserData()
|
||||
const {getDisplayName} = useUser();
|
||||
const {getDisplayName, isAdmin} = useUser();
|
||||
const [IsLogin, setIsLogin] = useState<boolean>(false)
|
||||
const [userInfo, setUserInfo] = useState<User>()
|
||||
const [ticketTotal, setTicketTotal] = useState<number>(0)
|
||||
|
||||
const themeStyles = useThemeStyles();
|
||||
const canShowScanButton = (() => {
|
||||
const v: any = (userInfo as any)?.isAdmin
|
||||
return isAdmin() || v === true || v === 1 || v === '1'
|
||||
})()
|
||||
|
||||
// 角色名称:优先取用户 roles 数组的第一个角色名称
|
||||
const getRoleName = () => {
|
||||
@@ -96,6 +101,8 @@ const UserCard = forwardRef<any, any>((_, ref) => {
|
||||
if (data) {
|
||||
setUserInfo(data)
|
||||
setIsLogin(true);
|
||||
// Keep local storage user info in sync so other hooks (e.g. unified scan) can read admin flags.
|
||||
Taro.setStorageSync('User', data)
|
||||
Taro.setStorageSync('UserId', data.userId)
|
||||
// 登录态已就绪后刷新卡片统计(余额/积分/券/水票)
|
||||
refresh().then()
|
||||
@@ -193,8 +200,7 @@ const UserCard = forwardRef<any, any>((_, ref) => {
|
||||
return false;
|
||||
}
|
||||
// 登录成功
|
||||
Taro.setStorageSync('access_token', res.data.data.access_token)
|
||||
Taro.setStorageSync('UserId', res.data.data.user.userId)
|
||||
saveStorageByLoginUser(res.data.data.access_token, res.data.data.user)
|
||||
setUserInfo(res.data.data.user)
|
||||
setIsLogin(true)
|
||||
// 登录态已就绪后刷新卡片统计(余额/积分/券/水票)
|
||||
@@ -249,47 +255,49 @@ const UserCard = forwardRef<any, any>((_, ref) => {
|
||||
</Button>
|
||||
)}
|
||||
</View>
|
||||
<Space style={{
|
||||
marginTop: '30px',
|
||||
marginRight: '10px'
|
||||
}}>
|
||||
{/*统一扫码入口 - 支持登录和核销*/}
|
||||
<UnifiedQRButton
|
||||
text="扫一扫"
|
||||
size="small"
|
||||
onSuccess={(result) => {
|
||||
console.log('统一扫码成功:', result);
|
||||
// 根据扫码类型给出不同的提示
|
||||
if (result.type === 'verification') {
|
||||
const businessType = result?.data?.businessType;
|
||||
if (businessType === 'gift' && result?.data?.gift) {
|
||||
const gift = result.data.gift;
|
||||
{/*统一扫码入口 - 仅管理员可见*/}
|
||||
{canShowScanButton && (
|
||||
<Space style={{
|
||||
marginTop: '30px',
|
||||
marginRight: '10px'
|
||||
}}>
|
||||
<UnifiedQRButton
|
||||
text="扫一扫"
|
||||
size="small"
|
||||
onSuccess={(result) => {
|
||||
console.log('统一扫码成功:', result);
|
||||
// 根据扫码类型给出不同的提示
|
||||
if (result.type === 'verification') {
|
||||
const businessType = result?.data?.businessType;
|
||||
if (businessType === 'gift' && result?.data?.gift) {
|
||||
const gift = result.data.gift;
|
||||
Taro.showModal({
|
||||
title: '核销成功',
|
||||
content: `已成功核销:${gift.goodsName || gift.name || '礼品'},面值¥${gift.faceValue}`
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (businessType === 'ticket' && result?.data?.ticket) {
|
||||
const ticket = result.data.ticket;
|
||||
const qty = result.data.qty || 1;
|
||||
Taro.showModal({
|
||||
title: '核销成功',
|
||||
content: `已成功核销:${ticket.templateName || '水票'},本次使用${qty}次,剩余可用${ticket.availableQty ?? 0}次`
|
||||
});
|
||||
return;
|
||||
}
|
||||
Taro.showModal({
|
||||
title: '核销成功',
|
||||
content: `已成功核销:${gift.goodsName || gift.name || '礼品'},面值¥${gift.faceValue}`
|
||||
content: '已成功核销'
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (businessType === 'ticket' && result?.data?.ticket) {
|
||||
const ticket = result.data.ticket;
|
||||
const qty = result.data.qty || 1;
|
||||
Taro.showModal({
|
||||
title: '核销成功',
|
||||
content: `已成功核销:${ticket.templateName || '水票'},本次使用${qty}次,剩余可用${ticket.availableQty ?? 0}次`
|
||||
});
|
||||
return;
|
||||
}
|
||||
Taro.showModal({
|
||||
title: '核销成功',
|
||||
content: '已成功核销'
|
||||
});
|
||||
}
|
||||
}}
|
||||
onError={(error) => {
|
||||
console.error('统一扫码失败:', error);
|
||||
}}
|
||||
/>
|
||||
</Space>
|
||||
}}
|
||||
onError={(error) => {
|
||||
console.error('统一扫码失败:', error);
|
||||
}}
|
||||
/>
|
||||
</Space>
|
||||
)}
|
||||
</View>
|
||||
<View className={'py-2'}>
|
||||
<View className={'flex justify-around mt-1'}>
|
||||
|
||||
Reference in New Issue
Block a user