index.vue 4.5 KB

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