index.jsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 DepartmentMenu = props => {
  11. const { dispatch, departments = [], onSelect } = 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. <Popconfirm
  44. title="确认删除吗?"
  45. onText="确认"
  46. cancelText="取消"
  47. onConfirm={() => handleDelDepartment(item.id)}>
  48. <DeleteOutlined />
  49. </Popconfirm>
  50. </div>
  51. </div>
  52. )
  53. }
  54. if (newItem.children?.length) {
  55. newItem.children = renderTreeNode(newItem.children)
  56. }
  57. return newItem
  58. })
  59. }
  60. const handleAddDepartment = async values => {
  61. const { code = -1 } = await addDepartment({ ...values })
  62. if (code === consts.RET_CODE.SUCCESS) {
  63. initData()
  64. }
  65. }
  66. useEffect(() => {
  67. if (!departments.length) {
  68. initData()
  69. }
  70. }, [])
  71. const saveActiveId = id => {
  72. if (onSelect) {
  73. onSelect(id)
  74. }
  75. }
  76. return (
  77. <div className="min-w-12rem rounded-4px flex flex-col">
  78. <div className="w-full p-4 border-b-1 border-solid border-black border-opacity-10 bg-hex-f7f9fa flex justify-around items-center flex-row">
  79. <div className="text-[0.9375rem]">组织架构</div>
  80. <ModalForm
  81. title="创建部门"
  82. width="500px"
  83. trigger={
  84. <Button size="small" type="primary" ghost disabled={!hasPerm}>
  85. <PlusOutlined />
  86. 创建部门
  87. </Button>
  88. }
  89. onFinish={async values => {
  90. await handleAddDepartment(values)
  91. message.success('添加成功')
  92. return true
  93. }}>
  94. <ProFormText name="backstageMenuId" hidden />
  95. <ProFormText name="name" label="新增部门" rules={[{ message: '请输入部门名称' }]} />
  96. <Form.Item label="上级部门" name="parentId">
  97. <TreeSelect
  98. style={{ width: '100%' }}
  99. placeholder="请选择上司"
  100. treeDefaultExpandAll
  101. treeData={departments}
  102. allowClear
  103. />
  104. </Form.Item>
  105. </ModalForm>
  106. </div>
  107. <div className="py-4 bg-white flex-1">
  108. {departments.length ? (
  109. <DirectoryTree
  110. multiple
  111. defaultExpandAll
  112. defaultSelectedKeys={['0']}
  113. onSelect={keys => saveActiveId(keys[0])}
  114. showIcon={false}
  115. treeData={renderTreeNode(newList)}
  116. />
  117. ) : null}
  118. </div>
  119. </div>
  120. )
  121. }
  122. export default connect(({ staff }) => ({
  123. departments: staff.departments
  124. }))(DepartmentMenu)