inspectorAdd.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <div class="common-dialog">
  3. <div class="common-dialog-content">
  4. <div class="common-dialog-title-box">
  5. <h3 class="common-dialog-title">新建巡查人员</h3>
  6. </div>
  7. <div class="common-dialog-box">
  8. <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
  9. <el-form-item label="姓名:" prop="nick_name">
  10. <el-tree-select
  11. v-model="formData.uuid"
  12. :data="treeData"
  13. :props="{ label: 'label', value: 'id', children: 'children' }"
  14. :render-after-expand="false"
  15. style="width: 468px"
  16. @change="onUserNameChange"
  17. />
  18. </el-form-item>
  19. <el-form-item label="粤政易组织:" prop="ancestors_names">
  20. <el-input v-model="formData.ancestors_names" style="width: 468px !important" />
  21. </el-form-item>
  22. <el-form-item label="联系方式:" prop="phonenumber">
  23. <el-input v-model="formData.phonenumber" style="width: 468px !important" />
  24. </el-form-item>
  25. <el-form-item label="责任区划:" prop="area">
  26. <el-tree-select
  27. v-model="divisionSelectedId"
  28. :data="formattedDivisionData"
  29. :props="{ label: 'label', value: 'code', children: 'children' }"
  30. :render-after-expand="false"
  31. style="width: 468px"
  32. @change="onDivisionChange"
  33. />
  34. </el-form-item>
  35. </el-form>
  36. <div class="common-dialog-footer">
  37. <el-button @click="closeDialog">取消</el-button>
  38. <el-button type="primary" @click="submitForm">确定</el-button>
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. </template>
  44. <script setup lang="ts">
  45. import { inspectorAdd, phoneList, userList, inspectorDivision } from '@/api/inspectionWork/inspector';
  46. import { ref, watch } from 'vue';
  47. import { ElMessage } from 'element-plus';
  48. import { useRouter } from 'vue-router';
  49. const emits = defineEmits(['close']);
  50. const props = defineProps<{
  51. // eventId: string | number;
  52. }>();
  53. const formData = ref({
  54. user_id: '',
  55. nick_name: '',
  56. phonenumber: '',
  57. uuid: ''
  58. });
  59. const rules = ref({
  60. // id: [{ required: true, message: '主键不能为空', trigger: 'blur' }],
  61. });
  62. const treeData = ref([]);
  63. const rawDivisionData = ref([]);
  64. const formattedDivisionData = ref([]);
  65. const divisionSelectedId = ref(null);
  66. const closeDialog = () => {
  67. emits('close');
  68. };
  69. const router = useRouter();
  70. // 更新表单数据
  71. const updateFormData = (userData) => {
  72. formData.value = {
  73. ...formData.value,
  74. user_id: userData.rows[0].user_id,
  75. dept_id: userData.rows[0].dept_id,
  76. dept_name: userData.rows[0].dept_name,
  77. ancestors_names: userData.rows[0].ancestors_names,
  78. user_name: userData.rows[0].user_name,
  79. nick_name: userData.rows[0].nick_name,
  80. phonenumber: userData.rows[0].phonenumber || ''
  81. };
  82. };
  83. // 监听姓名变化,一旦发生变化就请求用户列表
  84. const onUserNameChange = async (value) => {
  85. if (value) {
  86. const response = await userList({ userId: value });
  87. if (response.code === 200 && response.rows.length > 0) {
  88. updateFormData(response);
  89. } else {
  90. ElMessage.error(response.msg);
  91. }
  92. }
  93. };
  94. // 提交表单
  95. const submitForm = async () => {
  96. try {
  97. let areaCode = '';
  98. // 确保在提交前有选中的区划
  99. if (divisionSelectedId.value) {
  100. const selectedDivision = findNode(formattedDivisionData.value, divisionSelectedId.value);
  101. if (selectedDivision) {
  102. areaCode = selectedDivision.code;
  103. } else {
  104. ElMessage.error('请选择责任区划。');
  105. return;
  106. }
  107. }
  108. if (!areaCode) {
  109. ElMessage.error('请选择责任区划。');
  110. return;
  111. }
  112. const response = await inspectorAdd({
  113. user_id: formData.value.user_id,
  114. area_code: areaCode
  115. });
  116. if (response.code === 200) {
  117. ElMessage.success('提交成功');
  118. closeDialog(); // 关闭对话框
  119. emits('refresh');
  120. } else {
  121. ElMessage.error(response.msg);
  122. }
  123. } catch (error) {
  124. ElMessage.error('提交失败,请检查输入信息。');
  125. }
  126. };
  127. const fetchPhoneList = async () => {
  128. const response = await phoneList();
  129. treeData.value = response.data;
  130. };
  131. const fetchDivisionData = async () => {
  132. const response = await inspectorDivision();
  133. if (response.code === 200) {
  134. rawDivisionData.value = response.data;
  135. formattedDivisionData.value = formatDivisionData(rawDivisionData.value);
  136. console.log('Formatted Division Data:', formattedDivisionData.value);
  137. } else {
  138. ElMessage.error(response.msg);
  139. }
  140. };
  141. const onDivisionChange = (value) => {
  142. // 确认选中的项存在
  143. const selectedDivision = findNode(formattedDivisionData.value, value);
  144. if (!selectedDivision) {
  145. ElMessage.warning('未找到匹配的责任区划。');
  146. // 将选中的值重置为 null 或空字符串,防止后续逻辑错误
  147. divisionSelectedId.value = null;
  148. } else {
  149. console.log('Selected Division:', selectedDivision); // 调试输出
  150. }
  151. };
  152. // 递归查找节点
  153. const findNode = (nodes, code) => {
  154. for (let i = 0; i < nodes.length; i++) {
  155. const node = nodes[i];
  156. if (node.code === code) {
  157. return node;
  158. }
  159. if (node.children && node.children.length > 0) {
  160. const found = findNode(node.children, code);
  161. if (found) {
  162. return found;
  163. }
  164. }
  165. }
  166. return null;
  167. };
  168. // 格式化责任区划数据
  169. const formatDivisionData = (data) => {
  170. return data.map((item) => ({
  171. id: item.id,
  172. label: item.label,
  173. code: item.code,
  174. children: item.children ? formatDivisionData(item.children) : []
  175. }));
  176. };
  177. onMounted(() => {
  178. fetchPhoneList();
  179. fetchDivisionData();
  180. });
  181. </script>
  182. <style scoped></style>