index.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import React, { useEffect } from 'react'
  2. import { Button, message, Tree, TreeSelect, Form, Popconfirm } from 'antd'
  3. import { DeleteOutlined, PlusOutlined } from '@ant-design/icons'
  4. import consts from '@/consts'
  5. import { addDepartment, delDepartment } from '@/services/hr'
  6. import { connect, useAccess } from 'umi'
  7. import { ModalForm, ProFormText } from '@ant-design/pro-form'
  8. import './index.less'
  9. const { DirectoryTree } = Tree
  10. const RoleMenu = props => {
  11. const { dispatch, departments = [], onSelect, showType } = props
  12. const hasPerm = useAccess()?.validatePermByType('employee_del_department')
  13. const newList = [...departments]
  14. newList.unshift({
  15. children: [],
  16. id: '0',
  17. key: '0',
  18. isLeaf: false,
  19. name: '总全公司',
  20. title: '总全公司'
  21. })
  22. const initData = () => {
  23. dispatch({
  24. type: 'staff/fetchDepartments'
  25. })
  26. }
  27. const handleDelDepartment = async id => {
  28. const { code = -1 } = await delDepartment(id)
  29. if (code === consts.RET_CODE.SUCCESS) {
  30. message.success('删除成功')
  31. initData()
  32. }
  33. }
  34. function renderTreeNode(tree) {
  35. if (!hasPerm) return tree
  36. return tree.map(item => {
  37. const newItem = {
  38. ...item,
  39. title: (
  40. <div className="department-node">
  41. <div>{item.title}</div>
  42. <div className="extra">
  43. {showType ? (
  44. <Popconfirm
  45. title="确认删除吗?"
  46. onText="确认"
  47. cancelText="取消"
  48. onConfirm={() => handleDelDepartment(item.id)}>
  49. <DeleteOutlined />
  50. </Popconfirm>
  51. ) : null}
  52. </div>
  53. </div>
  54. )
  55. }
  56. if (newItem.children?.length) {
  57. newItem.children = renderTreeNode(newItem.children)
  58. }
  59. return newItem
  60. })
  61. }
  62. const handleAddDepartment = async values => {
  63. const { code = -1 } = await addDepartment({ ...values })
  64. if (code === consts.RET_CODE.SUCCESS) {
  65. initData()
  66. }
  67. }
  68. useEffect(() => {
  69. if (!departments.length) {
  70. initData()
  71. }
  72. }, [])
  73. const saveActiveId = id => {
  74. if (onSelect) {
  75. onSelect(id)
  76. }
  77. }
  78. return (
  79. <div
  80. className="w-12rem rounded-4px"
  81. style={{
  82. height: `calc(100vh - 96px)`
  83. }}>
  84. <div className="p-4 pb-3 border-b-1 border-solid border-black border-opacity-10 bg-[#f7f9fa] flex justify-around items-center">
  85. <span className="text-[0.9375rem]">组织架构</span>
  86. {hasPerm && showType ? (
  87. <ModalForm
  88. title="创建部门"
  89. width="500px"
  90. trigger={
  91. <Button size="small" type="primary" ghost>
  92. <PlusOutlined />
  93. 创建部门
  94. </Button>
  95. }
  96. onFinish={async values => {
  97. await handleAddDepartment(values)
  98. message.success('添加成功')
  99. return true
  100. }}>
  101. <ProFormText name="backstageMenuId" hidden />
  102. <ProFormText name="name" label="新增部门" rules={[{ message: '请输入部门名称' }]} />
  103. <Form.Item label="上级部门" name="parentId">
  104. <TreeSelect
  105. style={{ width: '100%' }}
  106. placeholder="请选择上司"
  107. treeDefaultExpandAll
  108. treeData={departments}
  109. allowClear
  110. />
  111. </Form.Item>
  112. </ModalForm>
  113. ) : null}
  114. </div>
  115. <div className="py-4 bg-white" style={{ height: 'calc(100% - 44px)' }}>
  116. {departments.length ? (
  117. <DirectoryTree
  118. multiple
  119. defaultExpandAll
  120. defaultSelectedKeys={['0']}
  121. onSelect={keys => saveActiveId(keys[0])}
  122. showIcon={false}
  123. treeData={renderTreeNode(newList)}
  124. />
  125. ) : null}
  126. </div>
  127. </div>
  128. )
  129. }
  130. export default connect(({ staff }) => ({
  131. departments: staff.departments
  132. }))(RoleMenu)