权限
This commit is contained in:
137
src/pages/Staff/Auth/index.tsx
Normal file
137
src/pages/Staff/Auth/index.tsx
Normal file
@@ -0,0 +1,137 @@
|
||||
import { Tree } from 'antd';
|
||||
import type React from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import PageContainerPlugin from '@/components/PageContainer/PageContainerPlugin';
|
||||
import { AdminServices } from '@/services/AdminServices';
|
||||
import { useAuthStore } from '@/store/AuthStore';
|
||||
import { useRequest } from '@/utils/useRequest';
|
||||
|
||||
/** 登录管理员权限列表页面 */
|
||||
const AuthForm: React.FC = () => {
|
||||
const auth = useAuthStore().auth;
|
||||
|
||||
const [menuTree, setMenuTree] = useState<any[]>([]);
|
||||
const [checkedKeys, setCheckedKeys] = useState<React.Key[]>([]);
|
||||
const [expandedKeys, setExpandedKeys] = useState<React.Key[]>([]);
|
||||
|
||||
/** 将后端数据转换为 Tree 节点 */
|
||||
const transformToTreeNodes = (menus: any[]): any[] => {
|
||||
return menus.map((menu) => {
|
||||
const childrenNodes: any[] = [];
|
||||
|
||||
// 子菜单
|
||||
if (menu.children?.length) {
|
||||
menu.children.forEach((child: any) => {
|
||||
const childNodes: any[] = [];
|
||||
|
||||
// 子菜单下的子菜单功能
|
||||
if (child.children?.length) {
|
||||
child.children.forEach((func: any) => {
|
||||
childNodes.push({
|
||||
title: func.function_ch_name,
|
||||
key: `func_${func.function_id}`,
|
||||
isLeaf: true,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 子菜单自己的功能
|
||||
if (child.functions?.length) {
|
||||
child.functions.forEach((func: any) => {
|
||||
childNodes.push({
|
||||
title: func.function_ch_name,
|
||||
key: `func_${func.function_id}`,
|
||||
isLeaf: true,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
childrenNodes.push({
|
||||
title: child.menu_ch_name,
|
||||
key: `menu_${child.menu_id}`,
|
||||
children: childNodes.length ? childNodes : undefined,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 顶级菜单功能
|
||||
if (menu.functions?.length) {
|
||||
menu.functions.forEach((func: any) => {
|
||||
childrenNodes.push({
|
||||
title: func.function_ch_name,
|
||||
key: `func_${func.function_id}`,
|
||||
isLeaf: true,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
title: menu.menu_ch_name,
|
||||
key: `menu_${menu.menu_id}`,
|
||||
children: childrenNodes.length ? childrenNodes : undefined,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
/** 获取 Tree 所有 key(用于默认全展开) */
|
||||
const getAllTreeKeys = (nodes: any[]): React.Key[] => {
|
||||
const keys: React.Key[] = [];
|
||||
|
||||
const loop = (list: any[]) => {
|
||||
list.forEach((item) => {
|
||||
keys.push(item.key);
|
||||
if (item.children) {
|
||||
loop(item.children);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
loop(nodes);
|
||||
return keys;
|
||||
};
|
||||
|
||||
/** 获取当前管理员权限 */
|
||||
const { loading: listLoading, request: listRequest } = useRequest(AdminServices.auth, {
|
||||
onSuccessCodeZero: (res) => {
|
||||
const tree = transformToTreeNodes(res.rule);
|
||||
setMenuTree(tree);
|
||||
|
||||
setExpandedKeys(getAllTreeKeys(tree)); // ⭐ 关键:全展开
|
||||
|
||||
// 如果后端返回的是 function_id 列表,这里可以顺便做回显
|
||||
if (res?.auth) {
|
||||
//const keys = res.auth.split(',').map((id: string) => `func_${id}`);
|
||||
const keys = res.auth.map((id: number) => `func_${id}`);
|
||||
setCheckedKeys(keys);
|
||||
} else {
|
||||
setCheckedKeys([]);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (!listLoading) {
|
||||
listRequest();
|
||||
}
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<Tree
|
||||
checkable
|
||||
checkStrictly={false} // 父子联动
|
||||
treeData={menuTree}
|
||||
checkedKeys={checkedKeys}
|
||||
expandedKeys={expandedKeys}
|
||||
onExpand={(keys) => setExpandedKeys(keys)}
|
||||
onCheck={() => {}} // 只读
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
const Auth = () => (
|
||||
<PageContainerPlugin breadcrumb={['权限管理', '我的权限']}>
|
||||
<AuthForm />
|
||||
</PageContainerPlugin>
|
||||
);
|
||||
|
||||
export default Auth;
|
||||
@@ -61,23 +61,27 @@ const GrpListForm: React.FC = () => {
|
||||
<GapBox>
|
||||
{item?.group_id != 1 && (
|
||||
<>
|
||||
<Button
|
||||
type='primary'
|
||||
onClick={() => {
|
||||
AdminGrpEditModalRef.current?.show(item);
|
||||
}}
|
||||
>
|
||||
编辑
|
||||
</Button>
|
||||
<Popconfirm
|
||||
placement='bottom'
|
||||
title='确认删除该岗位角色吗?'
|
||||
onConfirm={() => {
|
||||
deleteAdminGroupRequest(stringify({ group_id: item.group_id }));
|
||||
}}
|
||||
>
|
||||
<Button danger>删除</Button>
|
||||
</Popconfirm>
|
||||
{auth.SF_ADMIN_GROUP_EDIT && (
|
||||
<Button
|
||||
type='primary'
|
||||
onClick={() => {
|
||||
AdminGrpEditModalRef.current?.show(item);
|
||||
}}
|
||||
>
|
||||
编辑
|
||||
</Button>
|
||||
)}
|
||||
{auth.SF_ADMIN_GROUP_DEL && (
|
||||
<Popconfirm
|
||||
placement='top'
|
||||
title='确认删除该岗位角色吗?'
|
||||
onConfirm={() => {
|
||||
deleteAdminGroupRequest(stringify({ group_id: item.group_id }));
|
||||
}}
|
||||
>
|
||||
<Button danger>删除</Button>
|
||||
</Popconfirm>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</GapBox>
|
||||
@@ -190,14 +194,16 @@ const GrpListForm: React.FC = () => {
|
||||
|
||||
<GapBox style={{ marginBottom: 12, justifyContent: 'space-between' }}>
|
||||
<GapBox>
|
||||
<Button
|
||||
type='primary'
|
||||
onClick={() => {
|
||||
AdminGrpEditModalRef.current?.show();
|
||||
}}
|
||||
>
|
||||
新增
|
||||
</Button>
|
||||
{auth.SF_ADMIN_GROUP_ADD && (
|
||||
<Button
|
||||
type='primary'
|
||||
onClick={() => {
|
||||
AdminGrpEditModalRef.current?.show();
|
||||
}}
|
||||
>
|
||||
新增
|
||||
</Button>
|
||||
)}
|
||||
</GapBox>
|
||||
<HeaderPagination
|
||||
style={{ marginBottom: 0, marginRight: 12 }}
|
||||
|
||||
@@ -67,23 +67,27 @@ const AdminListForm: React.FC = () => {
|
||||
<GapBox>
|
||||
{item?.admin_id != 1 && (
|
||||
<>
|
||||
<Button
|
||||
type='primary'
|
||||
onClick={() => {
|
||||
AdminEditModalRef.current?.show(item);
|
||||
}}
|
||||
>
|
||||
编辑
|
||||
</Button>
|
||||
<Popconfirm
|
||||
placement='bottom'
|
||||
title='确认删除该管理员吗?'
|
||||
onConfirm={() => {
|
||||
deleteAdminRequest(stringify({ admin_id: item.admin_id }));
|
||||
}}
|
||||
>
|
||||
<Button danger>删除</Button>
|
||||
</Popconfirm>
|
||||
{auth.SF_ADMIN_ADMIN_EDIT && (
|
||||
<Button
|
||||
type='primary'
|
||||
onClick={() => {
|
||||
AdminEditModalRef.current?.show(item);
|
||||
}}
|
||||
>
|
||||
编辑
|
||||
</Button>
|
||||
)}
|
||||
{auth.SF_ADMIN_ADMIN_DEL && (
|
||||
<Popconfirm
|
||||
placement='top'
|
||||
title='确认删除该管理员吗?'
|
||||
onConfirm={() => {
|
||||
deleteAdminRequest(stringify({ admin_id: item.admin_id }));
|
||||
}}
|
||||
>
|
||||
<Button danger>删除</Button>
|
||||
</Popconfirm>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</GapBox>
|
||||
@@ -107,7 +111,6 @@ const AdminListForm: React.FC = () => {
|
||||
function page(curr: number) {
|
||||
if (!listLoading) {
|
||||
params.curr_page = curr;
|
||||
console.log(params);
|
||||
listRequest(stringify(params));
|
||||
}
|
||||
}
|
||||
@@ -219,14 +222,16 @@ const AdminListForm: React.FC = () => {
|
||||
|
||||
<GapBox style={{ marginBottom: 12, justifyContent: 'space-between' }}>
|
||||
<GapBox>
|
||||
<Button
|
||||
type='primary'
|
||||
onClick={() => {
|
||||
AdminEditModalRef.current?.show();
|
||||
}}
|
||||
>
|
||||
新增
|
||||
</Button>
|
||||
{auth.SF_ADMIN_ADMIN_ADD && (
|
||||
<Button
|
||||
type='primary'
|
||||
onClick={() => {
|
||||
AdminEditModalRef.current?.show();
|
||||
}}
|
||||
>
|
||||
新增
|
||||
</Button>
|
||||
)}
|
||||
</GapBox>
|
||||
<HeaderPagination
|
||||
style={{ marginBottom: 0, marginRight: 12 }}
|
||||
|
||||
@@ -63,23 +63,27 @@ const DepListForm: React.FC = () => {
|
||||
<GapBox>
|
||||
{item?.admin_id != 1 && (
|
||||
<>
|
||||
<Button
|
||||
type='primary'
|
||||
onClick={() => {
|
||||
AdminDepEditModalRef.current?.show(item);
|
||||
}}
|
||||
>
|
||||
编辑
|
||||
</Button>
|
||||
<Popconfirm
|
||||
placement='bottom'
|
||||
title='确认删除该组织架构吗?'
|
||||
onConfirm={() => {
|
||||
deleteAdminDepartmentRequest(stringify({ department_id: item.department_id }));
|
||||
}}
|
||||
>
|
||||
<Button danger>删除</Button>
|
||||
</Popconfirm>
|
||||
{auth.SF_ADMIN_DEPART_EDIT && (
|
||||
<Button
|
||||
type='primary'
|
||||
onClick={() => {
|
||||
AdminDepEditModalRef.current?.show(item);
|
||||
}}
|
||||
>
|
||||
编辑
|
||||
</Button>
|
||||
)}
|
||||
{auth.SF_ADMIN_DEPART_DEL && (
|
||||
<Popconfirm
|
||||
placement='top'
|
||||
title='确认删除该组织架构吗?'
|
||||
onConfirm={() => {
|
||||
deleteAdminDepartmentRequest(stringify({ department_id: item.department_id }));
|
||||
}}
|
||||
>
|
||||
<Button danger>删除</Button>
|
||||
</Popconfirm>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</GapBox>
|
||||
@@ -99,7 +103,6 @@ const DepListForm: React.FC = () => {
|
||||
function page(curr: number) {
|
||||
if (!listLoading) {
|
||||
params.curr_page = curr;
|
||||
console.log(params);
|
||||
listRequest(stringify(params));
|
||||
}
|
||||
}
|
||||
@@ -193,14 +196,16 @@ const DepListForm: React.FC = () => {
|
||||
|
||||
<GapBox style={{ marginBottom: 12, justifyContent: 'space-between' }}>
|
||||
<GapBox>
|
||||
<Button
|
||||
type='primary'
|
||||
onClick={() => {
|
||||
AdminDepEditModalRef.current?.show();
|
||||
}}
|
||||
>
|
||||
新增
|
||||
</Button>
|
||||
{auth.SF_ADMIN_DEPART_ADD && (
|
||||
<Button
|
||||
type='primary'
|
||||
onClick={() => {
|
||||
AdminDepEditModalRef.current?.show();
|
||||
}}
|
||||
>
|
||||
新增
|
||||
</Button>
|
||||
)}
|
||||
</GapBox>
|
||||
<HeaderPagination
|
||||
style={{ marginBottom: 0, marginRight: 12 }}
|
||||
|
||||
Reference in New Issue
Block a user