tj
2025-06-05 bba272999cc546f65781bf3d20245a3f819af67f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<template>
  <div v-if="showSidebarSearch" style="margin-top: 15px">
    <div class="search-display" v-if="!showSearchList" @click="toSearch"><i class="el-icon-search"></i></div>
    <el-select
      clearable
      filterable
      class="search"
      v-model="sidebar"
      ref="searchInput"
      v-if="showSearchList"
      :filter-method="search"
      @change="handleChange"
      placeholder="请输入关键字"
    >
      <el-option v-for="item in groups" :key="item.key" :label="item.title" :value="item.path"> </el-option>
    </el-select>
  </div>
</template>
 
<script>
import { mapGetters } from 'vuex'
import emitter from 'lin/util/emitter'
 
import Config from '@/config/index'
 
export default {
  data() {
    return {
      groups: [],
      sidebar: '',
      showSearchList: false,
      showSidebarSearch: Config.showSidebarSearch,
    }
  },
  computed: {
    ...mapGetters(['sidebarList']),
  },
  mounted() {
    emitter.on('removeSidebarSearch', () => {
      this.showSidebarSearch = false
    })
    emitter.on('showSidebarSearch', () => {
      if (Config.showSidebarSearch) {
        this.showSidebarSearch = true
      }
    })
  },
  methods: {
    handleChange(val) {
      this.groups = []
      this.sidebar = ''
      this.showSearchList = false
      this.$router.push(val)
    },
    toSearch() {
      this.showSearchList = true
      setTimeout(() => {
        this.$refs.searchInput.focus()
      }, 200)
    },
    search(val) {
      this.groups = []
 
      // 深度遍历配置树, 摘取叶子节点作为路由部分
      function deepTravel(config, fuc) {
        if (Array.isArray(config)) {
          config.forEach(subConfig => {
            deepTravel(subConfig, fuc)
          })
        } else if (config.children) {
          config.children.forEach(subConfig => {
            deepTravel(subConfig, fuc)
          })
        } else {
          fuc(config)
        }
      }
 
      deepTravel(this.sidebarList, viewConfig => {
        // 构造舞台view路由
        if (viewConfig.title.includes(val)) {
          const viewRouter = {}
          viewRouter.path = viewConfig.path
          viewRouter.title = viewConfig.title
          viewRouter.key = Math.random()
          this.groups.push(viewRouter)
        }
      })
    },
  },
}
</script>
 
<style lang="scss" scoped>
.search-display {
  position: relative;
  width: 80%;
  margin: 0 auto;
  height: 36px;
  border-bottom: 1px rgb(185, 190, 195) solid;
  cursor: pointer;
 
  .el-icon-search {
    position: absolute;
    left: 1px;
    top: 10px;
    color: rgb(185, 190, 195);
  }
}
 
.search {
  width: 80%;
}
</style>