edit.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <el-form ref="form" :model="bindDept" label-width="84px" class="camera-from">
  3. <el-form-item label="组织名称" class="mandatory">
  4. <el-input
  5. v-model="bindDept.name"
  6. maxlength="50"
  7. placeholder="请输入组织名称"
  8. ></el-input>
  9. </el-form-item>
  10. <el-form-item label="组织类型" class="mandatory">
  11. <el-radio-group v-model="bindDept.deptType">
  12. <el-radio
  13. v-for="option in deptTypeOptions"
  14. :label="option.value"
  15. :value="option.value"
  16. :key="option.value"
  17. >
  18. {{ option.label }}
  19. </el-radio>
  20. </el-radio-group>
  21. </el-form-item>
  22. <el-form-item label="上级组织" class="mandatory">
  23. <el-cascader
  24. ref="myCascader"
  25. style="width: 100%"
  26. :modelValue="superiorValue"
  27. @update:modelValue="(val: string[]) => updateSuperiorValue(val)"
  28. :options="organTrees"
  29. :props="{
  30. checkStrictly: true,
  31. label: 'name',
  32. value: 'id',
  33. disabled: 'disabled',
  34. }"
  35. />
  36. </el-form-item>
  37. <el-form-item label="负责人">
  38. <el-input
  39. v-model="bindDept.leader"
  40. maxlength="30"
  41. placeholder="请输入"
  42. ></el-input>
  43. </el-form-item>
  44. <el-form-item label="联系电话">
  45. <el-input
  46. v-model.trim="bindDept.phone"
  47. maxlength="50"
  48. placeholder="请输入联系电话"
  49. ></el-input>
  50. </el-form-item>
  51. <el-form-item label="备注">
  52. <el-input
  53. v-model="bindDept.remark"
  54. maxlength="500"
  55. type="textarea"
  56. placeholder="请输入备注"
  57. ></el-input>
  58. </el-form-item>
  59. </el-form>
  60. </template>
  61. <script setup lang="ts">
  62. import { deptTypeOptions } from "@/constant/organization";
  63. import {
  64. Organization,
  65. addOrganization,
  66. getOrganizationTree,
  67. setOrganization,
  68. } from "@/store/organization";
  69. import { computed, onMounted, ref } from "vue";
  70. import { ElMessage } from "element-plus";
  71. import { PHONE } from "@/constant/REG";
  72. import { QuiskExpose } from "@/helper/mount";
  73. const props = defineProps<{ dept?: Organization }>();
  74. const queryPathById = (
  75. id: string,
  76. current: Organization[]
  77. ): string[] | null => {
  78. for (const item of current) {
  79. if (item.id === id) {
  80. return [item.id];
  81. } else if (item.children) {
  82. const cItem = queryPathById(id, item.children);
  83. if (cItem) {
  84. return [item.id, ...cItem];
  85. }
  86. }
  87. }
  88. return null;
  89. };
  90. const bindDept = ref<Organization>(
  91. (props.dept ? { ...props.dept } : {}) as Organization
  92. );
  93. const organTrees = ref<Organization[]>([]);
  94. const superiorValue = computed(() => {
  95. if (bindDept.value.parentId) {
  96. return queryPathById(bindDept.value.parentId, organTrees.value) || [];
  97. } else {
  98. return [];
  99. }
  100. });
  101. const updateSuperiorValue = (value: string[]) => {
  102. bindDept.value.parentId = value[value.length - 1];
  103. bindDept.value.level = value.length + 1;
  104. };
  105. defineExpose<QuiskExpose>({
  106. async submit() {
  107. const { name, deptType, parentId, phone } = bindDept.value;
  108. if (!name || !name.trim()) {
  109. ElMessage.error("组织名称不能为空!");
  110. throw "组织名称不能为空!";
  111. } else if (!deptType) {
  112. ElMessage.error("组织类型不能为空!");
  113. throw "组织类型不能为空!";
  114. } else if (!parentId) {
  115. ElMessage.error("上级组织不能为空!");
  116. throw "上级组织不能为空!";
  117. } else if (phone && !PHONE.REG.test(phone)) {
  118. ElMessage.error("请输入11位正确手机号。");
  119. throw "请输入11位正确手机号。";
  120. }
  121. await (bindDept.value.id
  122. ? setOrganization(bindDept.value, superiorValue.value)
  123. : addOrganization(bindDept.value, superiorValue.value));
  124. return true;
  125. },
  126. });
  127. onMounted(async () => {
  128. const trees = await getOrganizationTree("1");
  129. const changeStatus = (trees: Organization[], fDisabled: boolean = false) =>
  130. trees.map((item) => {
  131. const disabled = fDisabled || item.id === bindDept.value.id;
  132. return {
  133. ...item,
  134. disabled,
  135. children: item.children && changeStatus(item.children, disabled),
  136. };
  137. });
  138. organTrees.value = changeStatus(trees);
  139. });
  140. </script>