index.vue 4.1 KB

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