index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <div class="header-search show" :style="{borderBottom:isHeader?'1px solid #0138c6': ''}">
  3. <svg-icon class-name="search-icon" icon-class="search" :style="{fill:isHeader?'#0138c6':'#c8c4cc'}"/>
  4. <el-select
  5. ref="headerSearchSelect"
  6. v-model="search"
  7. :remote-method="querySearch"
  8. filterable
  9. default-first-option
  10. @blur="isHeader = false"
  11. @focus="isHeader = true"
  12. remote
  13. placeholder="菜单搜索"
  14. size="mini"
  15. class="header-search-select"
  16. @change="change"
  17. >
  18. <el-option v-for="option in options" :key="option.item.path" :value="option.item"
  19. :label="option.item.title.join(' > ')"
  20. />
  21. </el-select>
  22. </div>
  23. </template>
  24. <script>
  25. // fuse is a lightweight fuzzy-search module
  26. // make search results more in line with expectations
  27. import Fuse from 'fuse.js/dist/fuse.min.js'
  28. import path from 'path'
  29. export default {
  30. name: 'HeaderSearch',
  31. data() {
  32. return {
  33. isHeader: false,
  34. search: '',
  35. options: [],
  36. searchPool: [],
  37. fuse: undefined
  38. }
  39. },
  40. computed: {
  41. routes() {
  42. return this.$store.getters.permission_routes
  43. }
  44. },
  45. watch: {
  46. routes() {
  47. this.searchPool = this.generateRoutes(this.routes)
  48. },
  49. searchPool(list) {
  50. this.initFuse(list)
  51. }
  52. },
  53. mounted() {
  54. this.searchPool = this.generateRoutes(this.routes)
  55. },
  56. methods: {
  57. close() {
  58. this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur()
  59. this.options = []
  60. },
  61. change(val) {
  62. const path = val.path
  63. const query = val.query
  64. if (this.ishttp(val.path)) {
  65. // http(s):// 路径新窗口打开
  66. const pindex = path.indexOf('http')
  67. window.open(path.substr(pindex, path.length), '_blank')
  68. } else {
  69. if (query) {
  70. this.$router.push({ path: path, query: JSON.parse(query) })
  71. } else {
  72. this.$router.push(path)
  73. }
  74. }
  75. this.search = ''
  76. this.options = []
  77. },
  78. initFuse(list) {
  79. this.fuse = new Fuse(list, {
  80. shouldSort: true,
  81. threshold: 0.4,
  82. location: 0,
  83. distance: 100,
  84. minMatchCharLength: 1,
  85. keys: [{
  86. name: 'title',
  87. weight: 0.7
  88. }, {
  89. name: 'path',
  90. weight: 0.3
  91. }]
  92. })
  93. },
  94. // Filter out the routes that can be displayed in the sidebar
  95. // And generate the internationalized title
  96. generateRoutes(routes, basePath = '/', prefixTitle = [], query = {}) {
  97. let res = []
  98. for (const router of routes) {
  99. // skip hidden router
  100. if (router.hidden) {
  101. continue
  102. }
  103. const data = {
  104. path: !this.ishttp(router.path) ? path.resolve(basePath, router.path) : router.path,
  105. title: [...prefixTitle]
  106. }
  107. if (router.meta && router.meta.title) {
  108. data.title = [...data.title, router.meta.title]
  109. if (router.redirect !== 'noRedirect') {
  110. // only push the routes with title
  111. // special case: need to exclude parent router without redirect
  112. res.push(data)
  113. }
  114. }
  115. if (router.query) {
  116. data.query = router.query
  117. }
  118. // recursive child routes
  119. if (router.children) {
  120. const tempRoutes = this.generateRoutes(router.children, data.path, data.title, data.query)
  121. if (tempRoutes.length >= 1) {
  122. res = [...res, ...tempRoutes]
  123. }
  124. }
  125. }
  126. return res
  127. },
  128. querySearch(query) {
  129. if (query !== '') {
  130. this.options = this.fuse.search(query)
  131. } else {
  132. this.options = []
  133. }
  134. },
  135. ishttp(url) {
  136. return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
  137. }
  138. }
  139. }
  140. </script>
  141. <style lang="scss" scoped>
  142. .header-search {
  143. font-size: 0 !important;
  144. background-color: #07074d50;
  145. border-radius: 5px;
  146. height: 30px !important;
  147. line-height: 30px !important;
  148. .search-icon {
  149. fill: #FFF;
  150. cursor: pointer;
  151. font-size: 14px;
  152. vertical-align: middle;
  153. }
  154. .header-search-select {
  155. font-size: 18px;
  156. transition: width 0.2s;
  157. width: 0;
  158. overflow: hidden;
  159. background: transparent;
  160. border-radius: 0;
  161. display: inline-block;
  162. vertical-align: middle;
  163. ::v-deep .el-input__inner {
  164. color: #FFF;
  165. background: transparent;
  166. border-radius: 0;
  167. border: 0;
  168. padding-left: 0;
  169. padding-right: 0;
  170. box-shadow: none !important;
  171. }
  172. }
  173. &.show {
  174. .header-search-select {
  175. width: 210px;
  176. margin-left: 10px;
  177. }
  178. }
  179. }
  180. </style>