feat(user): 添加角色权限控制与显示逻辑

- 引入 useUser hook 获取用户信息
- 实现 hasRole 方法检查用户角色
- 根据角色动态显示用户身份标签
- 限制特定角色操作按钮的显示条件
- 移除旧的角色名称存储逻辑- 更新用户卡片中的角色显示方式为动态获取
This commit is contained in:
2025-10-27 15:40:24 +08:00
parent 09e75bcaad
commit 78f94c0afa
3 changed files with 62 additions and 15 deletions

View File

@@ -57,8 +57,11 @@ const Location = () => {
getHjmCarByCode(keywords).then(data => {
console.log('执行搜索', data)
setItem(data)
setLatitude(data.latitude)
setLongitude(data.longitude)
// setLatitude(data.latitude)
// setLongitude(data.longitude)
// wgs48坐标转gcj02坐标代
setKeywords(data.code)
if (data.fence) {
// 方法2使用实际的 fence 数据(如果是字符串格式)

View File

@@ -8,7 +8,6 @@ import navTo from "@/utils/common";
function UserCard() {
const [IsLogin, setIsLogin] = useState<boolean>(false)
const [userInfo, setUserInfo] = useState<User>()
const [roleName, setRoleName] = useState<string>('注册用户')
useEffect(() => {
@@ -51,11 +50,6 @@ function UserCard() {
}
})
}
// 判断身份
const roleName = Taro.getStorageSync('RoleName');
if(roleName){
setRoleName(roleName)
}
}
}).catch(() => {
console.log('未登录')
@@ -98,6 +92,44 @@ function UserCard() {
});
};
// 获取用户显示的角色(同步版本)
const getRoleName = () => {
if(hasRole('superAdmin')){
return '超级管理员';
}
if(hasRole('admin')){
return '管理员';
}
if(hasRole('jiaojing')){
return '交警';
}
if(hasRole('youzheng')){
return '邮政协会/管局';
}
if(hasRole('Installer')){
return '安装人员';
}
if(hasRole('kuaidi')){
return '快递公司';
}
if(hasRole('zhandian')){
return '快递站点';
}
if(hasRole('kuaidiyuan')){
return '快递员';
}
return '注册用户';
}
// 检查是否有特定角色
const hasRole = (roleCode: string) => {
if (!userInfo || !userInfo.roles) {
return false;
}
return userInfo.roles.some(role => role.roleCode === roleCode);
};
return (
<>
<div className={'p-4'}>
@@ -127,7 +159,7 @@ function UserCard() {
{IsLogin ? (
<Space className={'grade text-xs py-1'}>
<Tag type="success" round>
<div className={'p-1'}>{roleName || '注册用户'}</div>
<div className={'p-1'}>{getRoleName()}</div>
</Tag>
{/*{*/}
{/* userInfo?.organizationName && (*/}

View File

@@ -13,6 +13,7 @@ import {UserVerify} from "@/api/system/userVerify/model";
import {pageUserVerify, updateUserVerify} from "@/api/system/userVerify";
import {listUserRole, updateUserRole} from "@/api/system/userRole";
import {listOrganizations} from "@/api/system/organization";
import {useUser} from "@/hooks/useUser";
/**
@@ -24,7 +25,9 @@ const UserVerifyAdmin: React.FC = () => {
const [keywords, setKeywords] = useState<string>('')
// const [showTextArea, setShowTextArea] = useState<boolean>(false)
const [refreshing, setRefreshing] = useState<boolean>(false)
const {user} = useUser()
console.log(refreshing)
// 获取状态显示
const getStatusDisplay = (status?: number) => {
switch (status) {
@@ -92,6 +95,15 @@ const UserVerifyAdmin: React.FC = () => {
setKeywords(value)
}
// 检查是否有特定角色
const hasRole = (roleCode: string) => {
if (!user || !user.roles) {
return false;
}
return user.roles.some(role => role.roleCode === roleCode);
};
const onPass = async (item: UserVerify) => {
const role = await listUserRole({roleId: 1701,userId: item.userId})
const userRole = role[0];
@@ -305,12 +317,12 @@ const UserVerifyAdmin: React.FC = () => {
}
</div>
)}
<Space className={'pt-4 flex justify-end'}>
<Button type="success" onClick={() => onPass(item)}></Button>
<Button type="warning" onClick={() => onReject(item)}></Button>
</Space>
{Taro.getStorageSync('kuaidi') && (
<Space className={'pt-4 flex justify-end'}>
<Button type="success" onClick={() => onPass(item)}></Button>
<Button type="warning" onClick={() => onReject(item)}></Button>
</Space>
)}
{/*{showTextArea && (*/}
{/* <TextArea*/}