index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <div class="organization-container">
  3. <div class="left">
  4. <TreeParts :treeData="org_tree_data" :title="'机构'" :showLock="true" :fieldNames="{
  5. key: 'deptId',
  6. title: 'deptName',
  7. children: 'children',
  8. }" @nodeClick="onNodeClick" @addBtnClick="onAddBtnClick" @deleteBtnClick="onDeleteBtnClick" @drop="onNodeDrop" />
  9. </div>
  10. <div class="right">
  11. <a-card title="详情" style="height: 100%">
  12. <OrgForm :org_form="org_form" @save="orgFromSave" @reset="orgFromReset">
  13. <template #actions>
  14. <a-button @click="orgFromSave">保存</a-button>
  15. <a-button @click="orgFromReset">重置</a-button>
  16. </template>
  17. </OrgForm>
  18. </a-card>
  19. </div>
  20. <Modal :visible.sync="org_visible" @ok="orgFromSave" @cancel="orgFromCancel" :title="'新增'">
  21. <OrgForm :org_form="org_form"></OrgForm>
  22. </Modal>
  23. </div>
  24. </template>
  25. <script setup>
  26. import { ref, reactive, onMounted } from 'vue';
  27. import { Message } from '@arco-design/web-vue';
  28. import TreeParts from '../TreeParts.vue';
  29. import OrgForm from './OrgForm.vue';
  30. import Modal from '../Modal.vue';
  31. import { QueryDeptDetail, UpdateDept, UpdateDeptParent, CreateDept, DeleteDept } from '../../../apis/permission/org';
  32. import { QueryDeptTree } from '../../../apis/permission/common';
  33. const org_visible = ref(false);
  34. const is_edit_mode = ref(true);
  35. const org_tree_data = ref([]);
  36. const org_form = reactive({
  37. parentName: '',
  38. status: 0,
  39. deptName: '',
  40. leader: '',
  41. phone: '',
  42. address: '',
  43. // 多余的参数
  44. orderNum: 0,
  45. });
  46. const getDeptTree = async () => {
  47. const {
  48. code,
  49. data: { rows }
  50. } = await QueryDeptTree();
  51. if (code == 200) {
  52. org_tree_data.value = rows;
  53. }
  54. };
  55. // 树节点操作相关
  56. const onNodeClick = async (id) => {
  57. const {
  58. data: { data }
  59. } = await QueryDeptDetail(id);
  60. org_form.deptId = id;
  61. org_form.parentName = data.parentName;
  62. org_form.status = data.status;
  63. org_form.deptName = data.deptName;
  64. org_form.leader = data.leader;
  65. org_form.phone = data.phone;
  66. org_form.address = data.address;
  67. is_edit_mode.value = true;
  68. };
  69. const onAddBtnClick = (nodeData) => {
  70. org_form.deptId = '';
  71. org_form.parentName = nodeData.deptName;
  72. org_form.parentId = nodeData.deptId;
  73. org_form.status = 0;
  74. org_form.deptName = '';
  75. org_form.leader = '';
  76. org_form.phone = '';
  77. org_form.address = '';
  78. org_visible.value = true;
  79. is_edit_mode.value = false;
  80. };
  81. const onDeleteBtnClick = async ({ deptId }) => {
  82. const { code } = await DeleteDept(deptId);
  83. if (code === 200) {
  84. Message.success({
  85. title: '删除',
  86. content: '删除成功'
  87. });
  88. }
  89. getDeptTree();
  90. };
  91. const orgFromSave = async () => {
  92. const data = {
  93. ...org_form,
  94. deptId: is_edit_mode.value && org_form.deptId ? org_form.deptId[0] : null
  95. };
  96. const apiCall = is_edit_mode.value ? UpdateDept : CreateDept;
  97. const { code } = await apiCall(data);
  98. if (code === 200) {
  99. const action = is_edit_mode.value ? '编辑' : '新增';
  100. Message.success({
  101. title: `${action}用户`,
  102. content: `${action}成功`
  103. });
  104. getDeptTree();
  105. org_visible.value = false;
  106. }
  107. };
  108. const orgFromReset = () => {
  109. org_form.deptId = null;
  110. org_form.parentName = '';
  111. org_form.status = 0;
  112. org_form.deptName = '';
  113. org_form.leader = '';
  114. org_form.phone = '';
  115. org_form.address = '';
  116. };
  117. const orgFromCancel = () => {
  118. is_edit_mode.value = true;
  119. org_visible.value = false;
  120. };
  121. const onNodeDrop = async ({ dropNode, dragNode, dropPosition }) => {
  122. const data = org_tree_data.value;
  123. UpdateDeptParent({
  124. orderNum: '0',
  125. parentId: dropNode.deptId,
  126. deptId: dragNode.deptId
  127. });
  128. const loop = (data, key, callback) => {
  129. data.some((item, index, arr) => {
  130. if (item.deptId === key) {
  131. callback(item, index, arr);
  132. return true;
  133. }
  134. if (item.children) {
  135. return loop(item.children, key, callback);
  136. }
  137. return false;
  138. });
  139. };
  140. loop(data, dragNode.deptId, (_, index, arr) => {
  141. arr.splice(index, 1);
  142. });
  143. if (dropPosition === 0) {
  144. loop(data, dropNode.deptId, (item) => {
  145. item.children = item.children || [];
  146. item.children.push(dragNode);
  147. });
  148. } else {
  149. loop(data, dropNode.deptId, (_, index, arr) => {
  150. arr.splice(dropPosition < 0 ? index : index + 1, 0, dragNode);
  151. });
  152. }
  153. };
  154. onMounted(() => {
  155. getDeptTree();
  156. });
  157. </script>
  158. <style lang="css" scoped>
  159. .organization-container {
  160. display: flex;
  161. justify-content: space-between;
  162. height: calc(100% - 40px);
  163. width: 100%;
  164. }
  165. .left {
  166. width: 30%;
  167. height: 100%;
  168. background-color: #fff;
  169. }
  170. .right {
  171. width: 69%;
  172. height: 100%;
  173. background-color: #fff;
  174. }
  175. </style>