index.jsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import React, { useEffect } from 'react'
  2. import { Button, message, Tree, TreeSelect, Form } from 'antd'
  3. import { PlusOutlined } from '@ant-design/icons'
  4. import consts from '@/consts'
  5. import { apiaddDepartment } from '@/services/department'
  6. import { connect, useAccess } from 'umi'
  7. import { ProFormText } from '@ant-design/pro-form'
  8. import ModalDragForm from '@/components/Modal/src/components/ModalDragForm'
  9. const RoleMenu = props => {
  10. const { dispatch, departments = [], onSelect } = props
  11. const newList = [...departments]
  12. newList.unshift({
  13. children: [],
  14. id: '0',
  15. key: '0',
  16. isLeaf: false,
  17. name: '总全公司',
  18. title: '总全公司'
  19. })
  20. const { DirectoryTree } = Tree
  21. const initData = () => {
  22. dispatch({
  23. type: 'staff/fetchDepartments'
  24. })
  25. }
  26. const handleAddDepartment = async values => {
  27. const { code = -1 } = await apiaddDepartment({ ...values })
  28. if (code === consts.RET_CODE.SUCCESS) {
  29. // message.success('新增成功')
  30. initData()
  31. }
  32. }
  33. useEffect(() => {
  34. if (!departments.length) {
  35. initData()
  36. }
  37. }, [])
  38. const saveActiveId = id => {
  39. // console.log(keys)
  40. if (onSelect) {
  41. onSelect(id)
  42. }
  43. }
  44. const onExpand = () => {}
  45. const hasPerm = useAccess()?.validatePermByType('employee_add_department')
  46. return (
  47. <div className="h-full w-12rem rounded-4px">
  48. <div className="p-4 pb-3 border-b-1 border-solid border-black border-opacity-10 bg-[#f7f9fa] flex justify-around items-center">
  49. <span className="text-[0.9375rem]">组织架构</span>
  50. {hasPerm && (
  51. <ModalDragForm
  52. title="创建部门"
  53. width="500px"
  54. trigger={
  55. <Button size="small" type="primary" ghost>
  56. <PlusOutlined />
  57. 创建部门
  58. </Button>
  59. }
  60. onFinish={async values => {
  61. await handleAddDepartment(values)
  62. message.success('添加成功')
  63. return true
  64. }}
  65. >
  66. <ProFormText name="backstageMenuId" hidden />
  67. <ProFormText name="name" label="新增部门" rules={[{ message: '请输入部门名称' }]} />
  68. <Form.Item label="上级部门" name="parentId">
  69. <TreeSelect
  70. style={{ width: '100%' }}
  71. placeholder="请选择上司"
  72. treeDefaultExpandAll
  73. treeData={departments}
  74. allowClear
  75. />
  76. </Form.Item>
  77. {/* <ProFormTreeSelect
  78. width="md"
  79. options={departments.map(item => ({ label: item.name, value: item.id }))}
  80. name="parentId"
  81. label="上级部门"
  82. /> */}
  83. </ModalDragForm>
  84. )}
  85. </div>
  86. <div className="py-4 pt-4 bg-white" style={{ height: 'calc(100% - 1rem*2 - 20px)' }}>
  87. {departments.length ? (
  88. <DirectoryTree
  89. multiple
  90. defaultExpandAll
  91. defaultSelectedKeys={['0']}
  92. onSelect={keys => saveActiveId(keys[0])}
  93. onExpand={onExpand}
  94. showIcon={false}
  95. treeData={newList}
  96. />
  97. ) : null}
  98. </div>
  99. </div>
  100. )
  101. }
  102. // export default RoleMenu
  103. export default connect(({ staff }) => ({
  104. departments: staff.departments
  105. }))(RoleMenu)