index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 lang="ts">
  20. import Fuse from 'fuse.js'
  21. import { getNormalPath } from '@/utils/ruoyi'
  22. import { isHttp } from '@/utils/validate'
  23. import usePermissionStore from '@/store/modules/permission'
  24. import { RouteOption } from 'vue-router'
  25. type Router = Array<{
  26. path: string;
  27. title: string[];
  28. }>
  29. const search = ref('');
  30. const options = ref<any>([]);
  31. const searchPool = ref<Router>([]);
  32. const show = ref(false);
  33. const fuse = ref();
  34. const headerSearchSelectRef = ref(ElSelect);
  35. const router = useRouter();
  36. const routes = computed(() => usePermissionStore().routes);
  37. const click = () => {
  38. show.value = !show.value
  39. if (show.value) {
  40. headerSearchSelectRef.value && headerSearchSelectRef.value.focus()
  41. }
  42. };
  43. const close = () => {
  44. headerSearchSelectRef.value && headerSearchSelectRef.value.blur()
  45. options.value = []
  46. show.value = false
  47. }
  48. const change = (val: any) => {
  49. const path = val.path;
  50. if (isHttp(path)) {
  51. // http(s):// 路径新窗口打开
  52. const pindex = path.indexOf("http");
  53. window.open(path.substr(pindex, path.length), "_blank");
  54. } else {
  55. router.push(path)
  56. }
  57. search.value = ''
  58. options.value = []
  59. nextTick(() => {
  60. show.value = false
  61. })
  62. }
  63. const initFuse = (list: Router) => {
  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. const generateRoutes = (routes: RouteOption[], basePath = '', prefixTitle: string[] = []) => {
  82. let res: Router = []
  83. routes.forEach(r => {
  84. // skip hidden router
  85. if (!r.hidden) {
  86. const p = r.path.length > 0 && r.path[0] === '/' ? r.path : '/' + r.path;
  87. const data = {
  88. path: !isHttp(r.path) ? getNormalPath(basePath + p) : r.path,
  89. title: [...prefixTitle]
  90. }
  91. if (r.meta && r.meta.title) {
  92. data.title = [...data.title, r.meta.title];
  93. if (r.redirect !== 'noRedirect') {
  94. // only push the routes with title
  95. // special case: need to exclude parent router without redirect
  96. res.push(data);
  97. }
  98. }
  99. // recursive child routes
  100. if (r.children) {
  101. const tempRoutes = generateRoutes(r.children, data.path, data.title);
  102. if (tempRoutes.length >= 1) {
  103. res = [...res, ...tempRoutes];
  104. }
  105. }
  106. }
  107. })
  108. return res;
  109. }
  110. const querySearch = (query: string) => {
  111. if (query !== '') {
  112. options.value = fuse.value.search(query)
  113. } else {
  114. options.value = []
  115. }
  116. }
  117. onMounted(() => {
  118. searchPool.value = generateRoutes(routes.value);
  119. })
  120. watchEffect(() => {
  121. searchPool.value = generateRoutes(routes.value)
  122. })
  123. watch(show, (value) => {
  124. if (value) {
  125. document.body.addEventListener('click', close)
  126. } else {
  127. document.body.removeEventListener('click', close)
  128. }
  129. })
  130. watch(searchPool, (list) => {
  131. initFuse(list)
  132. })
  133. </script>
  134. <style lang="scss" scoped>
  135. .header-search {
  136. font-size: 0 !important;
  137. .search-icon {
  138. cursor: pointer;
  139. font-size: 18px;
  140. vertical-align: middle;
  141. }
  142. .header-search-select {
  143. font-size: 18px;
  144. transition: width 0.2s;
  145. width: 0;
  146. overflow: hidden;
  147. background: transparent;
  148. border-radius: 0;
  149. display: inline-block;
  150. vertical-align: middle;
  151. :deep(.el-input__inner) {
  152. border-radius: 0;
  153. border: 0;
  154. padding-left: 0;
  155. padding-right: 0;
  156. box-shadow: none !important;
  157. border-bottom: 1px solid #d9d9d9;
  158. vertical-align: middle;
  159. }
  160. }
  161. &.show {
  162. .header-search-select {
  163. width: 210px;
  164. margin-left: 10px;
  165. }
  166. }
  167. }
  168. </style>