inspectorAdd.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 treeData = ref([]);
  60. const rawDivisionData = ref([]);
  61. const formattedDivisionData = ref([]);
  62. const divisionSelectedId = ref(null);
  63. const closeDialog = () => {
  64. emits('close');
  65. };
  66. const router = useRouter();
  67. // 更新表单数据
  68. const updateFormData = (userData) => {
  69. formData.value = {
  70. ...formData.value,
  71. user_id: userData.rows[0].user_id,
  72. dept_id: userData.rows[0].dept_id,
  73. dept_name: userData.rows[0].dept_name,
  74. ancestors_names: userData.rows[0].ancestors_names,
  75. user_name: userData.rows[0].user_name,
  76. nick_name: userData.rows[0].nick_name,
  77. phonenumber: userData.rows[0].phonenumber || ''
  78. };
  79. };
  80. // 表单验证规则
  81. const rules = ref({
  82. nick_name: [{ required: true, message: '请选择姓名', trigger: 'change' }],
  83. ancestors_names: [{ required: true, message: '请输入粤政易组织', trigger: 'blur' }],
  84. phonenumber: [{ required: true, message: '请输入联系方式', trigger: 'blur' }],
  85. area: [{ required: true, message: '请选择责任区划', trigger: 'change' }]
  86. });
  87. // 监听姓名变化,一旦发生变化就请求用户列表
  88. const onUserNameChange = async (value) => {
  89. if (value) {
  90. const response = await userList({ userId: value });
  91. if (response.code === 200 && response.rows.length > 0) {
  92. updateFormData(response);
  93. } else {
  94. ElMessage.error(response.msg);
  95. }
  96. }
  97. };
  98. // 提交表单
  99. const submitForm = async () => {
  100. try {
  101. let areaCode = '';
  102. // 确保在提交前有选中的区划
  103. if (divisionSelectedId.value) {
  104. const selectedDivision = findNode(formattedDivisionData.value, divisionSelectedId.value);
  105. if (selectedDivision) {
  106. areaCode = selectedDivision.code;
  107. } else {
  108. ElMessage.error('请选择责任区划。');
  109. return;
  110. }
  111. }
  112. if (!areaCode) {
  113. ElMessage.error('请选择责任区划。');
  114. return;
  115. }
  116. const response = await inspectorAdd({
  117. user_id: formData.value.user_id,
  118. area_code: areaCode
  119. });
  120. if (response.code === 200) {
  121. ElMessage.success('提交成功');
  122. closeDialog(); // 关闭对话框
  123. emits('refresh');
  124. } else {
  125. ElMessage.error(response.msg);
  126. }
  127. } catch (error) {
  128. ElMessage.error('提交失败,请检查输入信息。');
  129. }
  130. };
  131. const fetchPhoneList = async () => {
  132. const response = await phoneList();
  133. treeData.value = response.data;
  134. };
  135. const fetchDivisionData = async () => {
  136. const response = await inspectorDivision();
  137. if (response.code === 200) {
  138. rawDivisionData.value = response.data;
  139. formattedDivisionData.value = formatDivisionData(rawDivisionData.value);
  140. console.log('Formatted Division Data:', formattedDivisionData.value);
  141. } else {
  142. ElMessage.error(response.msg);
  143. }
  144. };
  145. const onDivisionChange = (value) => {
  146. // 确认选中的项存在
  147. const selectedDivision = findNode(formattedDivisionData.value, value);
  148. if (!selectedDivision) {
  149. ElMessage.warning('未找到匹配的责任区划。');
  150. // 将选中的值重置为 null 或空字符串,防止后续逻辑错误
  151. divisionSelectedId.value = null;
  152. } else {
  153. console.log('Selected Division:', selectedDivision); // 调试输出
  154. }
  155. };
  156. // 递归查找节点
  157. const findNode = (nodes, code) => {
  158. for (let i = 0; i < nodes.length; i++) {
  159. const node = nodes[i];
  160. if (node.code === code) {
  161. return node;
  162. }
  163. if (node.children && node.children.length > 0) {
  164. const found = findNode(node.children, code);
  165. if (found) {
  166. return found;
  167. }
  168. }
  169. }
  170. return null;
  171. };
  172. // 格式化责任区划数据
  173. const formatDivisionData = (data) => {
  174. return data.map((item) => ({
  175. id: item.id,
  176. label: item.label,
  177. code: item.code,
  178. children: item.children ? formatDivisionData(item.children) : []
  179. }));
  180. };
  181. onMounted(() => {
  182. fetchPhoneList();
  183. fetchDivisionData();
  184. });
  185. </script>
  186. <style scoped></style>