|
@@ -1,4 +1,5 @@
|
|
|
-import { addRoleMenu, delRoleMenu, updateRoleMenu } from '@/services/permission'
|
|
|
+import { addRoleMenu, delRoleMenuByRoleID, updateRoleMenu } from '@/services/permission'
|
|
|
+import { isNullOrUnDef } from '@/utils/is'
|
|
|
import {
|
|
|
DeleteOutlined,
|
|
|
FormOutlined,
|
|
@@ -6,25 +7,46 @@ import {
|
|
|
QuestionCircleOutlined
|
|
|
} from '@ant-design/icons'
|
|
|
import { ModalForm, ProFormText } from '@ant-design/pro-form'
|
|
|
+import type { ProFormInstance } from '@ant-design/pro-form'
|
|
|
+import type { TreeDataNode } from 'antd'
|
|
|
import { Button, Input, Tree, message, Popconfirm } from 'antd'
|
|
|
-import React, { useEffect, useState } from 'react'
|
|
|
+import { useRef, useState } from 'react'
|
|
|
import { useRequest } from 'umi'
|
|
|
-import './index.less'
|
|
|
const { DirectoryTree } = Tree
|
|
|
+import './index.less'
|
|
|
+
|
|
|
+export enum RoleType {
|
|
|
+ SYSTEM = 'system', // 超级管理员
|
|
|
+ SYSTEM_NORMAL = 'systemNormal', // 普通用户
|
|
|
+ NORMAL = 'normal' // 手动添加的角色
|
|
|
+}
|
|
|
+
|
|
|
+type RoleLeftMenuProps = {
|
|
|
+ defaultActiveRole?: string
|
|
|
+ onSelect: (...args) => void
|
|
|
+ showDelIcon: boolean
|
|
|
+ onReloadStaff: () => void
|
|
|
+}
|
|
|
+
|
|
|
+const RoleLeftMenu: React.FC<RoleLeftMenuProps> = ({
|
|
|
+ defaultActiveRole,
|
|
|
+ onSelect,
|
|
|
+ menuRoles,
|
|
|
+ showDelIcon,
|
|
|
+ onReloadStaff
|
|
|
+}) => {
|
|
|
+ const formRef = useRef<ProFormInstance>(null)
|
|
|
+ const [activeID, setActiveID] = useState<Nullable<string>>(null)
|
|
|
|
|
|
-const RoleLeftMenu = ({ onSelect, menuRoles, onReloadStaff }) => {
|
|
|
- const [state, setState] = useState({
|
|
|
- value: ''
|
|
|
- })
|
|
|
const { run: tryAddRole } = useRequest((params: API.CreateRoleParams) => addRoleMenu(params), {
|
|
|
manual: true,
|
|
|
- onSuccess: () => {}
|
|
|
+ onSuccess: () => {
|
|
|
+ onReloadStaff()
|
|
|
+ }
|
|
|
})
|
|
|
- const onChangeName = value => {
|
|
|
- setState({ ...state, name: value })
|
|
|
- }
|
|
|
+
|
|
|
const { run: tryUpdateRole } = useRequest(
|
|
|
- (ID: string, name: string) => updateRoleMenu({ ID, name }),
|
|
|
+ (params: Partial<API.UpdateRoleParams>) => updateRoleMenu(params),
|
|
|
{
|
|
|
manual: true,
|
|
|
onSuccess: () => {
|
|
@@ -33,45 +55,63 @@ const RoleLeftMenu = ({ onSelect, menuRoles, onReloadStaff }) => {
|
|
|
}
|
|
|
}
|
|
|
)
|
|
|
- const { run: tryDelRole } = useRequest((ID: string) => delRoleMenu({ ID }), {
|
|
|
+
|
|
|
+ const { run: tryDelRole } = useRequest((ID: string) => delRoleMenuByRoleID({ ID }), {
|
|
|
manual: true,
|
|
|
onSuccess: () => {
|
|
|
message.success('删除成功')
|
|
|
onReloadStaff()
|
|
|
}
|
|
|
})
|
|
|
+ const handleOnFocus = async (
|
|
|
+ e: React.FocusEvent<HTMLInputElement> | React.KeyboardEvent<HTMLElement>,
|
|
|
+ oldTitle: string,
|
|
|
+ ID: string
|
|
|
+ ) => {
|
|
|
+ const val = e.currentTarget.value || e.currentTarget.nodeValue
|
|
|
+ if (val !== oldTitle) {
|
|
|
+ await tryUpdateRole({ ID, name: val })
|
|
|
+ }
|
|
|
+ setActiveID(null)
|
|
|
+ }
|
|
|
+
|
|
|
function renderTreeNode(tree) {
|
|
|
- return tree.map(item => {
|
|
|
+ return tree.map((item: TreeDataNode & { roleType: string }) => {
|
|
|
+ // 系统管理员,系统用户不能删除、编辑
|
|
|
const newItem = {
|
|
|
...item,
|
|
|
- title: (
|
|
|
+ title: [RoleType.SYSTEM, RoleType.SYSTEM_NORMAL].includes(item.roleType) ? (
|
|
|
+ <span className="department-node py-1">{item.title}</span>
|
|
|
+ ) : (
|
|
|
<div className="department-node py-1">
|
|
|
- <div>{item.title}</div>
|
|
|
+ {item.key === activeID ? (
|
|
|
+ <Input
|
|
|
+ autoFocus
|
|
|
+ defaultValue={item.title}
|
|
|
+ bordered={false}
|
|
|
+ size="small"
|
|
|
+ style={{ width: '70%', backgroundColor: 'white' }}
|
|
|
+ onBlur={e => handleOnFocus(e, item.title, item.key)}
|
|
|
+ onPressEnter={e => handleOnFocus(e, item.title, item.key)}
|
|
|
+ />
|
|
|
+ ) : (
|
|
|
+ item.title
|
|
|
+ )}
|
|
|
<div className="extra">
|
|
|
- {item.roleType !== 'system' ? (
|
|
|
- <>
|
|
|
- <Popconfirm
|
|
|
- title={
|
|
|
- <Input
|
|
|
- placeholder="角色名称"
|
|
|
- onChange={e => onChangeName(e.currentTarget.value)}
|
|
|
- />
|
|
|
- }
|
|
|
- okText="确定"
|
|
|
- cancelText="取消"
|
|
|
- onConfirm={() => tryUpdateRole(item.key, state.name)}>
|
|
|
- <FormOutlined className="pr-2" />
|
|
|
- </Popconfirm>
|
|
|
- <Popconfirm
|
|
|
- title="确认删除吗?"
|
|
|
- onText="确认"
|
|
|
- cancelText="取消"
|
|
|
- onConfirm={() => tryDelRole(item.key)}
|
|
|
- icon={<QuestionCircleOutlined style={{ color: 'red' }} />}>
|
|
|
- <DeleteOutlined />
|
|
|
- </Popconfirm>
|
|
|
- </>
|
|
|
- ) : null}
|
|
|
+ <FormOutlined className="pr-2" onClick={() => setActiveID(item.key)} />
|
|
|
+ <Popconfirm
|
|
|
+ disabled={!showDelIcon}
|
|
|
+ title="确认删除吗?"
|
|
|
+ onText="确认"
|
|
|
+ cancelText="取消"
|
|
|
+ onConfirm={() => tryDelRole(item.key)}
|
|
|
+ icon={<QuestionCircleOutlined style={{ color: 'red' }} />}>
|
|
|
+ <DeleteOutlined
|
|
|
+ onClick={() => {
|
|
|
+ !showDelIcon && message.warning('请先移除该角色下的所有用户')
|
|
|
+ }}
|
|
|
+ />
|
|
|
+ </Popconfirm>
|
|
|
</div>
|
|
|
</div>
|
|
|
)
|
|
@@ -82,22 +122,20 @@ const RoleLeftMenu = ({ onSelect, menuRoles, onReloadStaff }) => {
|
|
|
return newItem
|
|
|
})
|
|
|
}
|
|
|
- useEffect(() => {
|
|
|
- onReloadStaff()
|
|
|
- }, [])
|
|
|
|
|
|
+ if (!menuRoles?.length || isNullOrUnDef(defaultActiveRole)) return null
|
|
|
return (
|
|
|
<div
|
|
|
className="w-216px rounded-20px"
|
|
|
style={{ height: 'calc(100vh - 122px)', background: '#ffffff' }}>
|
|
|
- <div className="p-5 menu-title flex items-center justify-around">
|
|
|
- <span className="text-16px text-opacity-85">角色列表</span>
|
|
|
+ <div className="menu-title flex items-center justify-around">
|
|
|
+ <span className="py-5 text-16px text-opacity-85">角色列表</span>
|
|
|
<ModalForm
|
|
|
title="新建角色"
|
|
|
- width="500px"
|
|
|
- labelCol={{ span: 4 }}
|
|
|
- wrapperCol={{ span: 16 }}
|
|
|
+ width="30%"
|
|
|
+ onVisibleChange={visible => !visible && formRef.current?.resetFields()}
|
|
|
layout="horizontal"
|
|
|
+ isKeyPressSubmit
|
|
|
trigger={
|
|
|
<Button size="small" type="primary" ghost>
|
|
|
<PlusOutlined />
|
|
@@ -109,7 +147,6 @@ const RoleLeftMenu = ({ onSelect, menuRoles, onReloadStaff }) => {
|
|
|
message.success('添加成功')
|
|
|
return true
|
|
|
}}>
|
|
|
- <ProFormText name="" hidden />
|
|
|
<ProFormText
|
|
|
name="name"
|
|
|
label="角色名称"
|