editOrg.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <div class="system-edit-menu-container">
  3. <el-dialog title="修改部门" v-model="state.isShowDialog" width="769px" draggable>
  4. <el-form :model="state.ruleForm" ref="ruleFormRef" label-width="80px">
  5. <el-row :gutter="35">
  6. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  7. <el-form-item label="部门名称" prop="orgName" :rules="[{ required: true, message: '请输入部门名称', trigger: 'blur' }]">
  8. <el-input v-model="state.ruleForm.orgName" placeholder="请输入部门名称" clearable></el-input>
  9. </el-form-item>
  10. </el-col>
  11. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  12. <el-form-item label="上级部门" prop="parentId" :rules="[{ required: true, message: '请选择上级部门', trigger: 'change' }]">
  13. <el-cascader
  14. :options="state.orgData"
  15. filterable
  16. :props="{ checkStrictly: true, value: 'id', label: 'orgName', emitPath: false }"
  17. placeholder="请选择上级部门"
  18. clearable
  19. class="w100"
  20. v-model="state.ruleForm.parentId"
  21. ref="cascaderRef"
  22. @change="getselKnowledgeList"
  23. >
  24. <template #default="{ node, data }">
  25. <span>{{ data.orgName }}</span>
  26. <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
  27. </template>
  28. </el-cascader>
  29. </el-form-item>
  30. </el-col>
  31. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  32. <el-form-item label="是否启用" prop="name" :rules="[{ required: false, message: '请输入角色状态', trigger: 'blur' }]">
  33. <el-switch v-model="state.ruleForm.isEnable" inline-prompt active-text="启" inactive-text="禁"></el-switch>
  34. </el-form-item>
  35. </el-col>
  36. </el-row>
  37. </el-form>
  38. <template #footer>
  39. <span class="dialog-footer">
  40. <el-button @click="onCancel" class="default-button">取 消</el-button>
  41. <el-button type="primary" @click="onSubmit(ruleFormRef)" v-waves="'light'" :loading="state.loading">修 改 </el-button>
  42. </span>
  43. </template>
  44. </el-dialog>
  45. </div>
  46. </template>
  47. <script setup lang="ts" name="systemEditOrg">
  48. import { reactive, ref } from 'vue';
  49. import { ElMessage, FormInstance } from 'element-plus';
  50. import { auth } from '/@/utils/authFunction';
  51. import { updateOrg, getOrgById } from '/@/api/system/organize';
  52. // 定义接口来定义对象的类型
  53. interface OrgFormState {
  54. isShowDialog: boolean;
  55. ruleForm: {
  56. orgName: string;
  57. orgCode: string;
  58. parentId: string;
  59. parentName: string;
  60. isEnable: boolean;
  61. children: Array<any>;
  62. };
  63. orgData: Array<any>;
  64. loading: boolean;
  65. }
  66. // 定义子组件向父组件传值/事件
  67. const emit = defineEmits(['updateList']);
  68. // 定义变量内容
  69. const ruleFormRef = ref<FormInstance>();
  70. const state = reactive<OrgFormState>({
  71. isShowDialog: false,
  72. ruleForm: {
  73. orgName: '',
  74. orgCode: '',
  75. parentId: '',
  76. parentName: '',
  77. isEnable: true,
  78. children: [],
  79. },
  80. orgData: [], // 上级部门数据
  81. loading: false,
  82. });
  83. // 格式化数据 排除自己
  84. const formatData = (arr: Array<any>, id: string) => {
  85. if (!arr) return [];
  86. arr.forEach((v: any, i: number) => {
  87. if (v.id === id) arr.splice(i, 1);
  88. if (v.children && v.children.length) {
  89. formatData(v.children, id);
  90. }
  91. });
  92. return arr;
  93. };
  94. // 打开弹窗
  95. const openDialog = async (row: any, orgData: any) => {
  96. if (!auth('100503')) ElMessage.warning('抱歉,您没有获取部门详情的权限!');
  97. else {
  98. ruleFormRef.value?.resetFields();
  99. try {
  100. state.orgData = JSON.parse(JSON.stringify(orgData));
  101. state.orgData = formatData(state.orgData, row.id);
  102. const res: any = await getOrgById(row.id);
  103. state.ruleForm = res.result;
  104. state.isShowDialog = true;
  105. } catch (error) {
  106. console.log(error);
  107. }
  108. }
  109. };
  110. // 关闭弹窗
  111. const closeDialog = () => {
  112. state.isShowDialog = false;
  113. };
  114. // 取消
  115. const onCancel = () => {
  116. closeDialog();
  117. };
  118. const cascaderRef = ref();
  119. // 获取选择name值
  120. const getselKnowledgeList = () => {
  121. let currentNode = cascaderRef.value.getCheckedNodes();
  122. state.ruleForm.parentName = currentNode[0]?.label ?? '';
  123. };
  124. // 修改
  125. const onSubmit = async (formEl: FormInstance | undefined) => {
  126. if (!formEl) return;
  127. await formEl.validate((valid, fields) => {
  128. if (valid) {
  129. updateOrg(state.ruleForm).then(() => {
  130. emit('updateList');
  131. closeDialog(); // 关闭弹窗
  132. ElMessage.success('修改成功');
  133. });
  134. } else {
  135. console.log('error submit!', fields);
  136. }
  137. });
  138. // setBackEndControlRefreshRoutes() // 刷新菜单,未进行后端接口测试
  139. };
  140. // 暴露变量
  141. defineExpose({
  142. openDialog,
  143. closeDialog,
  144. });
  145. </script>