addOrg.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <div class="system-add-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="orgShortName" :rules="[{ required: true, message: '请输入部门简称', trigger: 'blur' }]">
  13. <el-input v-model="state.ruleForm.orgShortName" placeholder="请输入部门简称" clearable></el-input>
  14. </el-form-item>
  15. </el-col>
  16. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  17. <el-form-item label="部门类别" prop="orgType" :rules="[{ required: true, message: '请选择部门类别', trigger: 'change' }]">
  18. <el-select v-model="state.ruleForm.orgType" placeholder="请选择部门类别" class="w100">
  19. <el-option v-for="item in state.orgType" :key="item.key" :label="item.value" :value="item.key" />
  20. </el-select>
  21. </el-form-item>
  22. </el-col>
  23. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  24. <el-form-item label="是否启用" prop="isEnable">
  25. <el-switch v-model="state.ruleForm.isEnable" inline-prompt active-text="启" inactive-text="禁"></el-switch>
  26. </el-form-item>
  27. </el-col>
  28. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
  29. <el-form-item label="上级部门" prop="parentId" :rules="[{ required: false, message: '请选择上级部门', trigger: 'change' }]">
  30. <el-cascader
  31. :options="state.orgData"
  32. filterable
  33. :props="{ checkStrictly: true, value: 'id', label: 'orgName', emitPath: false }"
  34. placeholder="请选择上级部门"
  35. clearable
  36. class="w100"
  37. v-model="state.ruleForm.parentId"
  38. ref="cascaderRef"
  39. @change="getselKnowledgeList"
  40. :disabled="state.disabled"
  41. >
  42. <template #default="{ node, data }">
  43. <span>{{ data.orgName }}</span>
  44. <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
  45. </template>
  46. </el-cascader>
  47. </el-form-item>
  48. </el-col>
  49. </el-row>
  50. </el-form>
  51. <template #footer>
  52. <span class="dialog-footer">
  53. <el-button @click="onCancel" class="default-button">取 消</el-button>
  54. <el-button type="primary" @click="onSubmit(ruleFormRef)" v-waves="'light'" :loading="state.loading">确 定 </el-button>
  55. </span>
  56. </template>
  57. </el-dialog>
  58. </div>
  59. </template>
  60. <script setup lang="ts" name="systemAddOrg">
  61. import { reactive, ref } from 'vue';
  62. import { ElMessage, FormInstance } from 'element-plus';
  63. import { addOrg } from '/@/api/system/organize';
  64. import mittBus from '/@/utils/mitt';
  65. // 定义子组件向父组件传值/事件
  66. const emit = defineEmits(['updateList']);
  67. // 定义变量内容
  68. const ruleFormRef = ref<FormInstance>();
  69. const state = reactive<any>({
  70. isShowDialog: false,
  71. ruleForm: {
  72. orgName: '',
  73. orgCode: '',
  74. parentId: '',
  75. parentName: '',
  76. orgType: '',
  77. isEnable: true,
  78. children: [],
  79. },
  80. orgData: [], // 上级部门数据
  81. orgType: [], // 部门类型
  82. loading: false,
  83. disabled: false,
  84. telsList: [],
  85. });
  86. // 打开弹窗
  87. const openDialog = (orgData: any, orgType: any, row?: any) => {
  88. state.orgData = orgData ?? [];
  89. state.orgType = orgType ?? [];
  90. ruleFormRef.value?.clearValidate();
  91. ruleFormRef.value?.resetFields();
  92. if (row.id) {
  93. state.ruleForm.parentId = row.id;
  94. state.ruleForm.parentName = row.orgName;
  95. state.disabled = true;
  96. } else {
  97. state.ruleForm.parentId = '';
  98. state.ruleForm.parentName = '';
  99. state.disabled = false;
  100. }
  101. state.isShowDialog = true;
  102. };
  103. // 关闭弹窗
  104. const closeDialog = () => {
  105. state.isShowDialog = false;
  106. };
  107. // 取消
  108. const onCancel = () => {
  109. closeDialog();
  110. };
  111. const cascaderRef = ref();
  112. // 获取选择name值
  113. const getselKnowledgeList = () => {
  114. let currentNode = cascaderRef.value.getCheckedNodes();
  115. state.ruleForm.parentName = currentNode[0]?.label ?? '';
  116. };
  117. // 新增
  118. const onSubmit = async (formEl: FormInstance | undefined) => {
  119. if (!formEl) return;
  120. await formEl.validate((valid) => {
  121. if (valid) {
  122. state.loading = true;
  123. addOrg(state.ruleForm)
  124. .then(() => {
  125. emit('updateList');
  126. closeDialog(); // 关闭弹窗
  127. ElMessage.success('新增成功');
  128. mittBus.emit('clearCache','systemUser')
  129. state.loading = false;
  130. })
  131. .catch(() => {
  132. state.loading = false;
  133. });
  134. }
  135. });
  136. };
  137. // 暴露变量
  138. defineExpose({
  139. openDialog,
  140. closeDialog,
  141. });
  142. </script>