Browse Source

refactor(portraitSearchBar): 重构搜索栏默认节点查找逻辑

替换原有硬编码id映射和递归查找函数,改用按部门类型查找首个匹配节点的方式,简化代码逻辑并提升可读性
huoyi@samsundot.com 1 week ago
parent
commit
2631a3bbc9
1 changed files with 35 additions and 46 deletions
  1. 35 46
      src/views/portraitManagement/components/SearchBar.vue

+ 35 - 46
src/views/portraitManagement/components/SearchBar.vue

@@ -74,22 +74,12 @@ const treeRef = ref(null)
74 74
 const currentKey = ref(null)
75 75
 const curQuery = ref({})
76 76
 
77
-const defaultIdMap = {
78
-  user: 13,
79
-  BRIGADE: 103,
80
-  STATION: 100,
81
-  MANAGER: 104,
82
-  TEAMS: 110
83
-}
84
-
85
-const findNodeById = (nodes, id) => {
86
-  if (!nodes) return null
87
-  for (const node of nodes) {
88
-    if (node.id === id || node.deptId === id) return node
89
-    if (node.children) {
90
-      const found = findNodeById(node.children, id)
91
-      if (found) return found
92
-    }
77
+const findFirstNodeByDeptType = (nodes, targetType) => {
78
+  if (!nodes || nodes.length === 0) return null
79
+  const first = nodes[0]
80
+  if (first.deptType === targetType || first.nodeType === targetType) return first
81
+  if (first.children && first.children.length > 0) {
82
+    return findFirstNodeByDeptType(first.children, targetType)
93 83
   }
94 84
   return null
95 85
 }
@@ -105,13 +95,13 @@ const buildPathForNode = (nodes, targetId, path = []) => {
105 95
       const name = node.nickName || node.deptName || node.name || node.label
106 96
       return [...path, name]
107 97
     }
108
-    if (node.children) {
98
+    
109 99
       const name = node.deptName || node.name || node.label
110 100
       const found = buildPathForNode(node.children, targetId, [...path, name])
111 101
       if (found.length > path.length) {
112 102
         return found
113 103
       }
114
-    }
104
+    
115 105
   }
116 106
   return path
117 107
 }
@@ -274,36 +264,35 @@ onMounted(async () => {
274 264
     departments.value = filterDeptTree(res.data, props.deptType)
275 265
   }
276 266
 
277
-  const defaultId = defaultIdMap[props.deptType]
278
-  if (defaultId) {
279
-    const defaultNode = findNodeById(departments.value, defaultId)
280
-    if (defaultNode) {
281
-      currentKey.value = defaultId
282
-      if (props.deptType === 'user') {
283
-        const path = buildPathForNode(departments.value, defaultNode.deptId || defaultNode.id)
284
-        const name = defaultNode.label
285
-        personName.value = path.length > 0 ? `${path.join(' / ')} / ${name}` : name
286
-        curQuery.value = { personName: defaultNode.label, id: defaultNode.id }
287
-        searchHandler(curQuery.value)
288
-      } else {
289
-        const path = buildPathForNode(departments.value, defaultNode.deptId || defaultNode.id)
290
-        const name = defaultNode.deptName || defaultNode.name || defaultNode.label
291
-        personName.value = path.length > 0 ? path.join(' / ') : name
292
-        if (props.deptType === 'BRIGADE' || props.deptType === 'STATION') {
293
-          curQuery.value = { deptId: defaultNode.deptId || defaultNode.id }
294
-        }
295
-        if (props.deptType === 'MANAGER') {
296
-          curQuery.value = { teamId: defaultNode.deptId || defaultNode.id, deptId: defaultNode.deptId || defaultNode.id }
297
-        }
298
-        if (props.deptType === 'TEAMS') {
299
-          curQuery.value = { groupId: defaultNode.deptId || defaultNode.id, deptId: defaultNode.deptId || defaultNode.id }
300
-        }
301
-        searchHandler(curQuery.value)
267
+  const targetType = props.deptType === 'user' ? 'user' : props.deptType
268
+  const defaultNode = findFirstNodeByDeptType(departments.value, targetType)
269
+  if (defaultNode) {
270
+    const nodeId = defaultNode.deptId || defaultNode.id
271
+    currentKey.value = nodeId
272
+    if (props.deptType === 'user') {
273
+      const path = buildPathForNode(departments.value, nodeId)
274
+      const name = defaultNode.label
275
+      personName.value = path.length > 0 ? `${path.join(' / ')} / ${name}` : name
276
+      curQuery.value = { personName: defaultNode.label, id: defaultNode.id }
277
+      searchHandler(curQuery.value)
278
+    } else {
279
+      const path = buildPathForNode(departments.value, nodeId)
280
+      const name = defaultNode.deptName || defaultNode.name || defaultNode.label
281
+      personName.value = path.length > 0 ? path.join(' / ') : name
282
+      if (props.deptType === 'BRIGADE' || props.deptType === 'STATION') {
283
+        curQuery.value = { deptId: nodeId }
284
+      }
285
+      if (props.deptType === 'MANAGER') {
286
+        curQuery.value = { teamId: nodeId, deptId: nodeId }
302 287
       }
303
-      nextTick(() => {
304
-        treeRef.value?.setCurrentKey(defaultId)
305
-      })
288
+      if (props.deptType === 'TEAMS') {
289
+        curQuery.value = { groupId: nodeId, deptId: nodeId }
290
+      }
291
+      searchHandler(curQuery.value)
306 292
     }
293
+    nextTick(() => {
294
+      treeRef.value?.setCurrentKey(nodeId)
295
+    })
307 296
   }
308 297
 })
309 298