index.vue 5.9 KB

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