index.vue 4.9 KB

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