Forráskód Böngészése

pms 设备动态 选中组织树部门时传递到 新增页面

zhangcl 4 napja
szülő
commit
597faa03f8

+ 45 - 10
src/views/pms/device/allotlog/ConfigDeviceAllot.vue

@@ -135,18 +135,19 @@
 </template>
 
 <script setup lang="ts">
-import { ref, computed } from 'vue'
+import { ref, computed, onMounted } from 'vue'
 import { ElMessage } from 'element-plus'
 import {defaultProps, handleTree} from "@/utils/tree";
 import * as DeptApi from "@/api/system/dept";
 import * as UserApi from "@/api/system/user";
 import {IotDeviceApi, IotDeviceVO} from "@/api/pms/device";
 import DeptTree2 from "@/views/pms/device/DeptTree2.vue";
-import { useRouter } from 'vue-router'
+import { useRouter, useRoute } from 'vue-router'
 import { useTagsViewStore } from "@/store/modules/tagsView";
 import {UserVO} from "@/api/system/user";
 
 const router = useRouter()
+const route = useRoute() // 获取路由参数
 const { t } = useI18n() // 国际化
 defineOptions({ name: 'ConfigDeviceAllot' })
 const selectedDeptId = ref<number | string>('')
@@ -395,18 +396,38 @@ onMounted(async () => {
     const deptData = await DeptApi.getSimpleDeptList()
     deptList.value = handleTree(deptData) // 转换为树形结构
 
-    // 设置默认选中的部门(转换后树的第一个节点)
-    if (deptList.value.length > 0) {
-      // 获取转换后树的第一个节点
-      const firstRootNode = deptList.value[0]
+    // 从路由参数获取部门ID
+    const routeDeptId = route.query.deptId ? Number(route.query.deptId) : null
 
-      // 设置设备部门的默认值
-      formData.value.deptId1 = firstRootNode.id
-      // 触发设备部门的节点点击事件,加载设备列表
-      await handleDeptDeviceTreeNodeClick(firstRootNode)
+    let targetDeptId: number
+
+    if (routeDeptId) {
+      // 如果路由参数有部门ID,使用路由参数中的部门ID
+      targetDeptId = routeDeptId
+    } else if (deptList.value.length > 0) {
+      // 否则使用第一个节点
+      targetDeptId = deptList.value[0].id
     } else {
       console.warn("部门树数据为空,无法设置默认值")
+      return
     }
+
+    // 设置默认选中的部门(转换后树的第一个节点)
+    // if (deptList.value.length > 0) {
+      // 获取转换后树的第一个节点
+      // const firstRootNode = deptList.value[0]
+
+    // 设置设备部门的默认值
+    formData.value.deptId = targetDeptId
+    // 触发设备部门的节点点击事件,加载设备列表
+    const targetDeptNode = findDeptNode(deptList.value, targetDeptId)
+    // 触发设备部门的节点点击事件,加载设备列表
+    if (targetDeptNode) {
+      await handleDeptDeviceTreeNodeClick(targetDeptNode)
+    }
+    // } else {
+      // console.warn("部门树数据为空,无法设置默认值")
+    // }
   } catch (error) {
     console.error("初始化部门树失败:", error)
     ElMessage.error("加载部门数据失败")
@@ -418,6 +439,20 @@ onMounted(async () => {
   })
 })
 
+// 在部门树中查找指定ID的节点
+const findDeptNode = (nodes: Tree[], deptId: number): Tree | null => {
+  for (const node of nodes) {
+    if (node.id === deptId) {
+      return node
+    }
+    if (node.children && node.children.length > 0) {
+      const found = findDeptNode(node.children, deptId)
+      if (found) return found
+    }
+  }
+  return null
+}
+
 </script>
 
 <style scoped>

+ 6 - 1
src/views/pms/device/allotlog/DeviceAllot.vue

@@ -286,7 +286,12 @@ const openForm = (type: string, id?: number) => {
     return
   }
   // 新增
-  push({ name: 'ConfigDeviceAllot', params: {} })
+  push({
+    name: 'ConfigDeviceAllot',
+    query: {
+      deptId: queryParams.deptId
+    }
+  })
 }
 
 /** 删除按钮操作 */

+ 49 - 14
src/views/pms/device/personlog/ConfigDevicePerson.vue

@@ -144,7 +144,7 @@
 </template>
 
 <script setup lang="ts">
-import { ref, computed } from 'vue'
+import { ref, computed, onMounted } from 'vue'
 import { ElMessage } from 'element-plus'
 import {defaultProps, handleTree} from "@/utils/tree";
 import * as DeptApi from "@/api/system/dept";
@@ -152,8 +152,9 @@ import {IotDeviceApi, IotDeviceVO} from "@/api/pms/device";
 import {IotDevicePersonApi, IotDevicePersonVO} from "@/api/pms/iotdeviceperson";
 import * as UserApi from "@/api/system/user";
 import {simpleUserList, UserVO} from "@/api/system/user";
-import { useRouter } from 'vue-router'
+import { useRouter, useRoute } from 'vue-router'
 const router = useRouter()
+const route = useRoute() // 获取路由参数
 const { t } = useI18n() // 国际化
 import { useTagsViewStore } from "@/store/modules/tagsView";
 
@@ -538,29 +539,63 @@ onMounted(async () => {
     const deptData = await DeptApi.getSimpleDeptList()
     deptList.value = handleTree(deptData) // 转换为树形结构
 
-    // 设置默认选中的部门(转换后树的第一个节点)
-    if (deptList.value.length > 0) {
-      // 获取转换后树的第一个节点
-      const firstRootNode = deptList.value[0]
+    // 从路由参数获取部门ID
+    const routeDeptId = route.query.deptId ? Number(route.query.deptId) : null
 
-      // 设置设备部门的默认值
-      formData.value.deptId1 = firstRootNode.id
-      // 触发设备部门的节点点击事件,加载设备列表
-      await handleDeptDeviceTreeNodeClick(firstRootNode)
+    let targetDeptId: number
 
-      // 设置责任人部门的默认值
-      formData.value.deptIds = [firstRootNode.id]
-      // 触发责任人部门的节点点击事件,加载人员列表
-      await getUserList() //handleDeptUserTreeNodeClick(firstRootNode)
+    if (routeDeptId) {
+      // 如果路由参数有部门ID,使用路由参数中的部门ID
+      targetDeptId = routeDeptId
+    } else if (deptList.value.length > 0) {
+      // 否则使用第一个节点
+      targetDeptId = deptList.value[0].id
     } else {
       console.warn("部门树数据为空,无法设置默认值")
+      return
+    }
+
+    // 设置默认选中的部门(转换后树的第一个节点)
+    // if (deptList.value.length > 0) {
+      // 获取转换后树的第一个节点
+      // const firstRootNode = deptList.value[0]
+
+    // 设置设备部门的默认值
+    formData.value.deptId1 = targetDeptId
+    // 触发设备部门的节点点击事件,加载设备列表
+    const targetDeptNode = findDeptNode(deptList.value, targetDeptId)
+    // 触发设备部门的节点点击事件,加载设备列表
+    if (targetDeptNode) {
+      await handleDeptDeviceTreeNodeClick(targetDeptNode)
     }
+
+    // 设置责任人部门的默认值
+    formData.value.deptIds = [targetDeptId]
+    // 触发责任人部门的节点点击事件,加载人员列表
+    await getUserList() //handleDeptUserTreeNodeClick(firstRootNode)
+    // } else {
+      // console.warn("部门树数据为空,无法设置默认值")
+    // }
   } catch (error) {
     console.error("初始化部门树失败:", error)
     ElMessage.error("加载部门数据失败")
   }
 })
 
+// 在部门树中查找指定ID的节点
+const findDeptNode = (nodes: Tree[], deptId: number): Tree | null => {
+  for (const node of nodes) {
+    if (node.id === deptId) {
+      return node
+    }
+    if (node.children && node.children.length > 0) {
+      const found = findDeptNode(node.children, deptId)
+      if (found) return found
+    }
+  }
+  return null
+}
+
 </script>
 
 <style scoped>

+ 7 - 2
src/views/pms/device/personlog/DevicePerson.vue

@@ -235,8 +235,13 @@ const openForm = (type: string, id?: number) => {
     push({ name: 'DeviceDetailEdit', params: { id } })
     return
   }
-  // 新增
-  push({ name: 'ConfigDevicePerson', params: {} })
+  // 新增 传递当前选中的部门ID
+  push({
+    name: 'ConfigDevicePerson',
+    query: {
+      deptId: queryParams.deptId
+    }
+  })
 }
 
 /** 删除按钮操作 */

+ 44 - 10
src/views/pms/device/statuslog/ConfigDeviceStatus.vue

@@ -126,15 +126,16 @@
 </template>
 
 <script setup lang="ts">
-import { ref, computed } from 'vue'
+import { ref, computed, onMounted } from 'vue'
 import { ElMessage } from 'element-plus'
 import {defaultProps, handleTree} from "@/utils/tree";
 import * as DeptApi from "@/api/system/dept";
 import {IotDeviceApi, IotDeviceVO} from "@/api/pms/device";
 import {DICT_TYPE, getStrDictOptions} from "@/utils/dict";
-import { useRouter } from 'vue-router'
+import { useRouter, useRoute } from 'vue-router'
 import { useTagsViewStore } from "@/store/modules/tagsView";
 const router = useRouter()
+const route = useRoute() // 获取路由参数
 
 const { t } = useI18n() // 国际化
 defineOptions({ name: 'ConfigDeviceStatus' })
@@ -349,24 +350,57 @@ onMounted(async () => {
     const deptData = await DeptApi.getSimpleDeptList()
     deptList.value = handleTree(deptData) // 转换为树形结构
 
-    // 设置默认选中的部门(转换后树的第一个节点)
-    if (deptList.value.length > 0) {
-      // 获取转换后树的第一个节点
-      const firstRootNode = deptList.value[0]
+    // 从路由参数获取部门ID
+    const routeDeptId = route.query.deptId ? Number(route.query.deptId) : null
+    let targetDeptId: number
 
-      // 设置设备部门的默认值
-      formData.value.deptId1 = firstRootNode.id
-      // 触发设备部门的节点点击事件,加载设备列表
-      await handleDeptDeviceTreeNodeClick(firstRootNode)
+    if (routeDeptId) {
+      // 如果路由参数有部门ID,使用路由参数中的部门ID
+      targetDeptId = routeDeptId
+    } else if (deptList.value.length > 0) {
+      // 否则使用第一个节点
+      targetDeptId = deptList.value[0].id
     } else {
       console.warn("部门树数据为空,无法设置默认值")
+      return
+    }
+
+    // 设置默认选中的部门(转换后树的第一个节点)
+    // if (deptList.value.length > 0) {
+      // 获取转换后树的第一个节点
+      // const firstRootNode = deptList.value[0]
+
+    // 设置设备部门的默认值
+    formData.value.deptId1 = targetDeptId
+    // 触发设备部门的节点点击事件,加载设备列表
+    const targetDeptNode = findDeptNode(deptList.value, targetDeptId)
+    // 触发设备部门的节点点击事件,加载设备列表
+    if (targetDeptNode) {
+      await handleDeptDeviceTreeNodeClick(targetDeptNode)
     }
+    // } else {
+      // console.warn("部门树数据为空,无法设置默认值")
+    // }
   } catch (error) {
     console.error("初始化部门树失败:", error)
     ElMessage.error("加载部门数据失败")
   }
 })
 
+// 在部门树中查找指定ID的节点
+const findDeptNode = (nodes: Tree[], deptId: number): Tree | null => {
+  for (const node of nodes) {
+    if (node.id === deptId) {
+      return node
+    }
+    if (node.children && node.children.length > 0) {
+      const found = findDeptNode(node.children, deptId)
+      if (found) return found
+    }
+  }
+  return null
+}
+
 </script>
 
 <style scoped>

+ 6 - 1
src/views/pms/device/statuslog/DeviceStatus.vue

@@ -316,7 +316,12 @@ const openForm = (type: string, id?: number) => {
     return
   }
   // 新增
-  push({ name: 'ConfigDeviceStatus', params: {} })
+  push({
+    name: 'ConfigDeviceStatus',
+    query: {
+      deptId: queryParams.deptId
+    }
+  })
 }
 
 /** 删除按钮操作 */