index.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <template>
  2. <div class="header-search">
  3. <svg-icon class-name="search-icon" icon-class="search" @click.stop="click" />
  4. <el-dialog
  5. :visible.sync="show"
  6. width="600px"
  7. @close="close"
  8. :show-close="false"
  9. append-to-body
  10. >
  11. <el-input
  12. v-model="search"
  13. ref="headerSearchSelectRef"
  14. size="large"
  15. @input="querySearch"
  16. prefix-icon="Search"
  17. placeholder="菜单搜索,支持标题、URL模糊查询"
  18. clearable
  19. >
  20. </el-input>
  21. <el-scrollbar wrap-class="right-scrollbar-wrapper">
  22. <div class="result-wrap">
  23. <div class="search-item" v-for="item in options" :key="item.path">
  24. <div class="left">
  25. <svg-icon class="menu-icon" :icon-class="item.icon" />
  26. </div>
  27. <div class="search-info" @click="change(item)">
  28. <div class="menu-title">
  29. {{ item.title.join(" / ") }}
  30. </div>
  31. <div class="menu-path">
  32. {{ item.path }}
  33. </div>
  34. </div>
  35. </div>
  36. </div>
  37. </el-scrollbar>
  38. </el-dialog>
  39. </div>
  40. </template>
  41. <script>
  42. import Fuse from 'fuse.js/dist/fuse.min.js'
  43. import path from 'path'
  44. import { isHttp } from '@/utils/validate'
  45. export default {
  46. name: 'HeaderSearch',
  47. data() {
  48. return {
  49. search: '',
  50. options: [],
  51. searchPool: [],
  52. show: false,
  53. fuse: undefined
  54. }
  55. },
  56. computed: {
  57. routes() {
  58. return this.$store.getters.defaultRoutes
  59. }
  60. },
  61. watch: {
  62. routes() {
  63. this.searchPool = this.generateRoutes(this.routes)
  64. },
  65. searchPool(list) {
  66. this.initFuse(list)
  67. }
  68. },
  69. mounted() {
  70. this.searchPool = this.generateRoutes(this.routes)
  71. },
  72. methods: {
  73. click() {
  74. this.show = !this.show
  75. if (this.show) {
  76. this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.focus()
  77. this.options = this.searchPool
  78. }
  79. },
  80. close() {
  81. this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur()
  82. this.search = ''
  83. this.options = []
  84. this.show = false
  85. },
  86. change(val) {
  87. const path = val.path
  88. const query = val.query
  89. if(isHttp(val.path)) {
  90. // http(s):// 路径新窗口打开
  91. const pindex = path.indexOf("http");
  92. window.open(path.substr(pindex, path.length), "_blank")
  93. } else {
  94. if (query) {
  95. this.$router.push({ path: path, query: JSON.parse(query) })
  96. } else {
  97. this.$router.push(path)
  98. }
  99. }
  100. this.search = ''
  101. this.options = []
  102. this.$nextTick(() => {
  103. this.show = false
  104. })
  105. },
  106. initFuse(list) {
  107. this.fuse = new Fuse(list, {
  108. shouldSort: true,
  109. threshold: 0.4,
  110. location: 0,
  111. distance: 100,
  112. minMatchCharLength: 1,
  113. keys: [{
  114. name: 'title',
  115. weight: 0.7
  116. }, {
  117. name: 'path',
  118. weight: 0.3
  119. }]
  120. })
  121. },
  122. // Filter out the routes that can be displayed in the sidebar
  123. // And generate the internationalized title
  124. generateRoutes(routes, basePath = '/', prefixTitle = []) {
  125. let res = []
  126. for (const router of routes) {
  127. // skip hidden router
  128. if (router.hidden) { continue }
  129. const data = {
  130. path: !isHttp(router.path) ? path.resolve(basePath, router.path) : router.path,
  131. title: [...prefixTitle],
  132. icon: ''
  133. }
  134. if (router.meta && router.meta.title) {
  135. data.title = [...data.title, router.meta.title]
  136. data.icon = router.meta.icon
  137. if (router.redirect !== 'noRedirect') {
  138. // only push the routes with title
  139. // special case: need to exclude parent router without redirect
  140. res.push(data)
  141. }
  142. }
  143. if (router.query) {
  144. data.query = router.query
  145. }
  146. // recursive child routes
  147. if (router.children) {
  148. const tempRoutes = this.generateRoutes(router.children, data.path, data.title)
  149. if (tempRoutes.length >= 1) {
  150. res = [...res, ...tempRoutes]
  151. }
  152. }
  153. }
  154. return res
  155. },
  156. querySearch(query) {
  157. if (query !== '') {
  158. this.options = this.fuse.search(query).map((item) => item.item) ?? this.searchPool
  159. } else {
  160. this.options = this.searchPool
  161. }
  162. }
  163. }
  164. }
  165. </script>
  166. <style lang='scss' scoped>
  167. ::v-deep {
  168. .el-dialog__header {
  169. padding: 0 !important;
  170. }
  171. }
  172. .header-search {
  173. .search-icon {
  174. cursor: pointer;
  175. font-size: 18px;
  176. vertical-align: middle;
  177. }
  178. }
  179. .result-wrap {
  180. height: 280px;
  181. margin: 12px 0;
  182. .search-item {
  183. display: flex;
  184. height: 48px;
  185. .left {
  186. width: 60px;
  187. text-align: center;
  188. .menu-icon {
  189. width: 18px;
  190. height: 18px;
  191. margin-top: 5px;
  192. }
  193. }
  194. .search-info {
  195. padding-left: 5px;
  196. width: 100%;
  197. display: flex;
  198. flex-direction: column;
  199. justify-content: flex-start;
  200. .menu-title,
  201. .menu-path {
  202. height: 20px;
  203. }
  204. .menu-path {
  205. color: #ccc;
  206. font-size: 10px;
  207. }
  208. }
  209. }
  210. .search-item:hover {
  211. cursor: pointer;
  212. }
  213. }
  214. </style>