index.vue 4.9 KB

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