瀏覽代碼

迭代八巡查人员:创建功能完成

zhangyihao 9 月之前
父節點
當前提交
2ce735f2b4
共有 3 個文件被更改,包括 150 次插入19 次删除
  1. 9 1
      src/api/inspectionWork/inspector.ts
  2. 2 2
      src/views/inspectionWork/inspector.vue
  3. 139 16
      src/views/inspectionWork/inspectorAdd.vue

+ 9 - 1
src/api/inspectionWork/inspector.ts

@@ -31,6 +31,14 @@ export function inspectorDetail(id) {
     params: id
   });
 }
+// 查询用户详情
+export function inspectorDivision(params) {
+  return request({
+    url: '/api/riskManagement/allAreas',
+    method: 'get',
+    params: params
+  });
+}
 export function inspectorUpload(data) {
   return request({
     url: '/api/riskManagement/inspection/user/update',
@@ -56,7 +64,7 @@ export function inspectorDelete(data) {
 // 获取新建用户列表
 export function userList(params) {
   return request({
-    url: '/api/riskManagement/inspection/user/',
+    url: '/api/riskManagement/system/user/list',
     method: 'get',
     params: params
   });

+ 2 - 2
src/views/inspectionWork/inspector.vue

@@ -62,7 +62,7 @@
     </el-row>
   </div>
   <InspectorEdit v-if="inspectorEditState.show" :event-id="inspectorEditState.eventId" @close="handleCancel" />
-  <InspectorAdd v-if="inspectorAddState.show" @close="handleCancel" />
+  <InspectorAdd v-if="inspectorAddState.show" @close="handleCancel" @refresh="fetchUserData" />
 </template>
 
 <script setup lang="ts">
@@ -127,7 +127,7 @@ const fetchUserData = async () => {
     id: item.id,
     nickName: item.nick_name,
     phone: item.phonenumber,
-    yzy_organization: item.yzy_account || '', // 如果 yzy_account 为空,则显示空字符串
+    yzy_organization: item.ancestors_names || '', // 如果 yzy_account 为空,则显示空字符串
     division_responsibility: item.area // 显示责任区划
   }));
   total.value = res.total;

+ 139 - 16
src/views/inspectionWork/inspectorAdd.vue

@@ -10,19 +10,27 @@
             <el-tree-select
               v-model="formData.uuid"
               :data="treeData"
-              :props="{ label: 'label', value: 'uuid', children: 'children' }"
+              :props="{ label: 'label', value: 'id', children: 'children' }"
               :render-after-expand="false"
               style="width: 468px"
+              @change="onUserNameChange"
             />
           </el-form-item>
-          <el-form-item label="粤政易组织:" prop="yzy_account">
-            <el-input v-model="formData.yzy_account" style="width: 468px !important" />
+          <el-form-item label="粤政易组织:" prop="ancestors_names">
+            <el-input v-model="formData.ancestors_names" style="width: 468px !important" />
           </el-form-item>
           <el-form-item label="联系方式:" prop="phonenumber">
             <el-input v-model="formData.phonenumber" style="width: 468px !important" />
           </el-form-item>
           <el-form-item label="责任区划:" prop="area">
-            <el-input v-model="formData.area" style="width: 468px !important" />
+            <el-tree-select
+              v-model="divisionSelectedId"
+              :data="formattedDivisionData"
+              :props="{ label: 'label', value: 'code', children: 'children' }"
+              :render-after-expand="false"
+              style="width: 468px"
+              @change="onDivisionChange"
+            />
           </el-form-item>
         </el-form>
         <div class="common-dialog-footer">
@@ -33,42 +41,157 @@
     </div>
   </div>
 </template>
+
 <script setup lang="ts">
-import { inspectorAdd, phoneList } from '@/api/inspectionWork/inspector';
-import { ref } from 'vue';
-import { ElMessage, ElTreeSelect } from 'element-plus';
+import { inspectorAdd, phoneList, userList, inspectorDivision } from '@/api/inspectionWork/inspector';
+import { ref, watch } from 'vue';
+import { ElMessage } from 'element-plus';
+import { useRouter } from 'vue-router';
+
 const emits = defineEmits(['close']);
 const props = defineProps<{
   eventId: string | number;
 }>();
+
 const formData = ref({
+  user_id: '',
   nick_name: '',
   phonenumber: '',
-  yzy_account: '',
-  area: '',
   uuid: ''
 });
+
 const treeData = ref([]);
+const rawDivisionData = ref([]);
+const formattedDivisionData = ref([]);
+
+const divisionSelectedId = ref(null);
+
 const closeDialog = () => {
   emits('close');
 };
+
+const router = useRouter();
+
+// 更新表单数据
+const updateFormData = (userData) => {
+  formData.value = {
+    ...formData.value,
+    user_id: userData.rows[0].user_id,
+    dept_id: userData.rows[0].dept_id,
+    dept_name: userData.rows[0].dept_name,
+    ancestors_names: userData.rows[0].ancestors_names,
+    user_name: userData.rows[0].user_name,
+    nick_name: userData.rows[0].nick_name,
+    phonenumber: userData.rows[0].phonenumber || ''
+  };
+};
+
+// 监听姓名变化,一旦发生变化就请求用户列表
+const onUserNameChange = async (value) => {
+  if (value) {
+    const response = await userList({ userId: value });
+    if (response.code === 200 && response.rows.length > 0) {
+      updateFormData(response);
+    } else {
+      ElMessage.error(response.msg);
+    }
+  }
+};
+
 // 提交表单
 const submitForm = async () => {
-  // 假设表单已经通过验证
-  const response = await inspectorAdd(formData.value);
-  if (response.code === 200) {
-    ElMessage.success('提交成功');
-    closeDialog(); // 关闭对话框
-  } else {
-    ElMessage.error(response.msg);
+  try {
+    let areaCode = '';
+    // 确保在提交前有选中的区划
+    if (divisionSelectedId.value) {
+      const selectedDivision = findNode(formattedDivisionData.value, divisionSelectedId.value);
+      if (selectedDivision) {
+        areaCode = selectedDivision.code;
+      } else {
+        ElMessage.error('请选择责任区划。');
+        return;
+      }
+    }
+    if (!areaCode) {
+      ElMessage.error('请选择责任区划。');
+      return;
+    }
+
+    const response = await inspectorAdd({
+      user_id: formData.value.user_id,
+      area_code: areaCode
+    });
+    if (response.code === 200) {
+      ElMessage.success('提交成功');
+      closeDialog(); // 关闭对话框
+      emits('refresh');
+    } else {
+      ElMessage.error(response.msg);
+    }
+  } catch (error) {
+    ElMessage.error('提交失败,请检查输入信息。');
   }
 };
+
 const fetchPhoneList = async () => {
   const response = await phoneList();
   treeData.value = response.data;
 };
+
+const fetchDivisionData = async () => {
+  const response = await inspectorDivision();
+  if (response.code === 200) {
+    rawDivisionData.value = response.data;
+    formattedDivisionData.value = formatDivisionData(rawDivisionData.value);
+    console.log('Formatted Division Data:', formattedDivisionData.value);
+  } else {
+    ElMessage.error(response.msg);
+  }
+};
+
+const onDivisionChange = (value) => {
+  // 确认选中的项存在
+  const selectedDivision = findNode(formattedDivisionData.value, value);
+  if (!selectedDivision) {
+    ElMessage.warning('未找到匹配的责任区划。');
+    // 将选中的值重置为 null 或空字符串,防止后续逻辑错误
+    divisionSelectedId.value = null;
+  } else {
+    console.log('Selected Division:', selectedDivision); // 调试输出
+  }
+};
+
+// 递归查找节点
+const findNode = (nodes, code) => {
+  for (let i = 0; i < nodes.length; i++) {
+    const node = nodes[i];
+    if (node.code === code) {
+      return node;
+    }
+    if (node.children && node.children.length > 0) {
+      const found = findNode(node.children, code);
+      if (found) {
+        return found;
+      }
+    }
+  }
+  return null;
+};
+
+// 格式化责任区划数据
+const formatDivisionData = (data) => {
+  return data.map((item) => ({
+    id: item.id,
+    label: item.label,
+    code: item.code,
+    children: item.children ? formatDivisionData(item.children) : []
+  }));
+};
+
 onMounted(() => {
   fetchPhoneList();
+  fetchDivisionData();
 });
 </script>
+
 <style scoped></style>