index.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import {
  2. addRoleMenu,
  3. delRoleMenu,
  4. fetchRoleListByMenu,
  5. updateRoleMenu
  6. } from '@/services/permission'
  7. import {
  8. DeleteOutlined,
  9. FormOutlined,
  10. PlusOutlined,
  11. QuestionCircleOutlined
  12. } from '@ant-design/icons'
  13. import { ModalForm, ProFormText } from '@ant-design/pro-form'
  14. import { Button, Input, message, Popconfirm, Popover } from 'antd'
  15. import React, { useEffect, useState } from 'react'
  16. import { useRequest } from 'umi'
  17. import './index.less'
  18. const RoleLeftMenu = ({ onSelect }) => {
  19. const [state, setState] = useState({
  20. value: ''
  21. })
  22. const [activeId, setActiveId] = useState('')
  23. const [menuRoles, setMenuRoles] = useState<API.MenuRoleItem[]>([])
  24. const { run: tryFetchRoleListByMenu } = useRequest(() => fetchRoleListByMenu(), {
  25. manual: true,
  26. onSuccess: result => {
  27. setMenuRoles(result)
  28. // setActiveId(result[0]['ID'])
  29. }
  30. })
  31. const { run: tryAddRole } = useRequest((params: API.CreateRoleParams) => addRoleMenu(params), {
  32. manual: true,
  33. onSuccess: () => {
  34. tryFetchRoleListByMenu()
  35. }
  36. })
  37. const onChangeName = value => {
  38. setState({ ...state, name: value })
  39. }
  40. const { run: tryUpdateRole } = useRequest(
  41. (ID: string, name: string) => updateRoleMenu({ ID, name }),
  42. {
  43. manual: true,
  44. onSuccess: () => {
  45. message.success('修改成功')
  46. tryFetchRoleListByMenu()
  47. }
  48. }
  49. )
  50. const { run: tryDelRole } = useRequest((ID: string) => delRoleMenu({ ID }), {
  51. manual: true,
  52. onSuccess: () => {
  53. message.success('删除成功')
  54. tryFetchRoleListByMenu()
  55. }
  56. })
  57. useEffect(() => {
  58. tryFetchRoleListByMenu()
  59. }, [])
  60. const handleItemClick = (id: string) => {
  61. setActiveId(id)
  62. if (onSelect) {
  63. onSelect(id)
  64. }
  65. }
  66. return (
  67. <div
  68. className="w-216px rounded-20px"
  69. style={{ height: 'calc(100vh - 122px)', background: '#ffffff' }}>
  70. <div className="p-5 menu-title flex items-center justify-around">
  71. <span className="text-16px text-opacity-85">角色列表</span>
  72. <ModalForm
  73. title="新建角色"
  74. width="500px"
  75. labelCol={{ span: 4 }}
  76. wrapperCol={{ span: 16 }}
  77. layout="horizontal"
  78. trigger={
  79. <Button size="small" type="primary" ghost>
  80. <PlusOutlined />
  81. 添加
  82. </Button>
  83. }
  84. onFinish={async values => {
  85. await tryAddRole(values)
  86. message.success('添加成功')
  87. return true
  88. }}>
  89. <ProFormText name="" hidden />
  90. <ProFormText
  91. name="name"
  92. label="角色名称"
  93. rules={[{ required: true, message: '请输入角色名' }]}
  94. />
  95. </ModalForm>
  96. </div>
  97. <div className="p-4 bg-white" style={{ height: 'calc(100% - 1rem*2 - 20px)' }}>
  98. <ul className="p-0 m-0 list-none text-primary flex flex-col flex-1">
  99. {menuRoles.map(item => (
  100. <Popover
  101. key={item.id}
  102. placement="right"
  103. content={
  104. <div>
  105. {item.roleType !== 'system' ? (
  106. <span>
  107. <Popconfirm
  108. title={
  109. <Input
  110. placeholder="角色名称"
  111. name="name"
  112. onChange={e => onChangeName(e.currentTarget.value)}
  113. />
  114. }
  115. okText="确定"
  116. cancelText="取消"
  117. onConfirm={() => tryUpdateRole(item.ID, state.name)}
  118. icon="">
  119. <FormOutlined className="pr-1" />
  120. </Popconfirm>
  121. <Popconfirm
  122. title="确定删除该角色"
  123. okText="确定"
  124. cancelText="取消"
  125. onConfirm={() => tryDelRole(item.ID)}
  126. icon={<QuestionCircleOutlined style={{ color: 'red' }} />}>
  127. <DeleteOutlined />
  128. </Popconfirm>
  129. </span>
  130. ) : null}
  131. </div>
  132. }>
  133. <li
  134. key={item.id}
  135. className={[
  136. 'flex justify-between items-center py-2 px-5 cursor-pointer',
  137. item.ID === activeId ? 'scale-up-center' : ''
  138. ].join(' ')}
  139. onClick={() => handleItemClick(item.ID)}>
  140. <span>{item.name}</span>
  141. </li>
  142. </Popover>
  143. ))}
  144. </ul>
  145. </div>
  146. </div>
  147. )
  148. }
  149. export default RoleLeftMenu