index.vue 5.0 KB

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