From 589151d1edded867fd8d15e9249fd0dfbf3544ef Mon Sep 17 00:00:00 2001 From: RuoYi Date: Mon, 21 Apr 2025 13:23:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=8F=9C=E5=8D=95=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=9F=A5=E8=AF=A2=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/HeaderSearch/index.vue | 170 +++++++++++++++----------- 1 file changed, 100 insertions(+), 70 deletions(-) diff --git a/src/components/HeaderSearch/index.vue b/src/components/HeaderSearch/index.vue index f87ff52..89a9478 100644 --- a/src/components/HeaderSearch/index.vue +++ b/src/components/HeaderSearch/index.vue @@ -1,19 +1,41 @@ @@ -23,36 +45,40 @@ import { getNormalPath } from '@/utils/ruoyi' import { isHttp } from '@/utils/validate' import usePermissionStore from '@/store/modules/permission' -const search = ref(''); -const options = ref([]); -const searchPool = ref([]); -const show = ref(false); -const fuse = ref(undefined); -const headerSearchSelectRef = ref(null); -const router = useRouter(); -const routes = computed(() => usePermissionStore().defaultRoutes); +const search = ref('') +const options = ref([]) +const searchPool = ref([]) +const show = ref(false) +const fuse = ref(undefined) +const headerSearchSelectRef = ref(null) +const router = useRouter() +const routes = computed(() => usePermissionStore().defaultRoutes) function click() { show.value = !show.value if (show.value) { headerSearchSelectRef.value && headerSearchSelectRef.value.focus() + options.value = searchPool.value } -}; +} + function close() { headerSearchSelectRef.value && headerSearchSelectRef.value.blur() + search.value = '' options.value = [] show.value = false } + function change(val) { - const path = val.path; - const query = val.query; + const path = val.path + const query = val.query if (isHttp(path)) { // http(s):// 路径新窗口打开 - const pindex = path.indexOf("http"); - window.open(path.substr(pindex, path.length), "_blank"); + const pindex = path.indexOf("http") + window.open(path.substr(pindex, path.length), "_blank") } else { if (query) { - router.push({ path: path, query: JSON.parse(query) }); + router.push({ path: path, query: JSON.parse(query) }) } else { router.push(path) } @@ -64,6 +90,7 @@ function change(val) { show.value = false }) } + function initFuse(list) { fuse.value = new Fuse(list, { shouldSort: true, @@ -80,6 +107,7 @@ function initFuse(list) { }] }) } + // Filter out the routes that can be displayed in the sidebar // And generate the internationalized title function generateRoutes(routes, basePath = '', prefixTitle = []) { @@ -88,16 +116,17 @@ function generateRoutes(routes, basePath = '', prefixTitle = []) { for (const r of routes) { // skip hidden router if (r.hidden) { continue } - const p = r.path.length > 0 && r.path[0] === '/' ? r.path : '/' + r.path; + const p = r.path.length > 0 && r.path[0] === '/' ? r.path : '/' + r.path const data = { path: !isHttp(r.path) ? getNormalPath(basePath + p) : r.path, - title: [...prefixTitle] + title: [...prefixTitle], + icon: '' } if (r.meta && r.meta.title) { data.title = [...data.title, r.meta.title] - - if (r.redirect !== 'noRedirect') { + data.icon = r.meta.icon + if (r.redirect !== "noRedirect") { // only push the routes with title // special case: need to exclude parent router without redirect res.push(data) @@ -117,30 +146,19 @@ function generateRoutes(routes, basePath = '', prefixTitle = []) { } return res } + function querySearch(query) { if (query !== '') { - options.value = fuse.value.search(query) + options.value = fuse.value.search(query).map((item) => item.item) ?? searchPool.value } else { - options.value = [] + options.value = searchPool.value } } onMounted(() => { - searchPool.value = generateRoutes(routes.value); -}) - -watchEffect(() => { searchPool.value = generateRoutes(routes.value) }) -watch(show, (value) => { - if (value) { - document.body.addEventListener('click', close) - } else { - document.body.removeEventListener('click', close) - } -}) - watch(searchPool, (list) => { initFuse(list) }) @@ -148,40 +166,52 @@ watch(searchPool, (list) => { \ No newline at end of file +