index.vue 5.4 KB

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