index.vue 4.6 KB

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