2023-04-03 00:26:04 +08:00
|
|
|
<template>
|
|
|
|
<div :class="{ 'show': show }" class="header-search">
|
2023-10-09 11:57:22 +08:00
|
|
|
<svg-icon class-name="search-icon" icon-class="search" @click.stop="click"/>
|
2023-04-03 00:26:04 +08:00
|
|
|
<el-select
|
|
|
|
ref="headerSearchSelectRef"
|
|
|
|
v-model="search"
|
|
|
|
:remote-method="querySearch"
|
|
|
|
filterable
|
|
|
|
default-first-option
|
|
|
|
remote
|
|
|
|
placeholder="Search"
|
|
|
|
class="header-search-select"
|
|
|
|
@change="change"
|
|
|
|
>
|
2023-10-09 11:57:22 +08:00
|
|
|
<el-option v-for="option in options" :key="option.item.path" :value="option.item"
|
|
|
|
:label="option.item.title.join(' > ')"/>
|
2023-04-03 00:26:04 +08:00
|
|
|
</el-select>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2023-04-19 23:25:34 +08:00
|
|
|
<script setup lang="ts" name="HeaderSearch">
|
2023-04-24 13:50:25 +08:00
|
|
|
import Fuse from 'fuse.js';
|
2023-10-09 11:57:22 +08:00
|
|
|
import {getNormalPath} from '@/utils/ruoyi';
|
|
|
|
import {isHttp} from '@/utils/validate';
|
2023-04-24 13:50:25 +08:00
|
|
|
import usePermissionStore from '@/store/modules/permission';
|
2023-10-09 11:57:22 +08:00
|
|
|
import {RouteOption} from 'vue-router';
|
2023-04-02 01:01:56 +08:00
|
|
|
|
|
|
|
type Router = Array<{
|
2023-10-09 11:57:22 +08:00
|
|
|
path: string;
|
|
|
|
title: string[];
|
2023-04-02 01:01:56 +08:00
|
|
|
}>
|
2023-03-15 15:59:21 +08:00
|
|
|
|
|
|
|
const search = ref('');
|
2023-04-02 01:01:56 +08:00
|
|
|
const options = ref<any>([]);
|
|
|
|
const searchPool = ref<Router>([]);
|
2023-03-15 15:59:21 +08:00
|
|
|
const show = ref(false);
|
2023-04-02 01:01:56 +08:00
|
|
|
const fuse = ref();
|
2023-06-06 22:52:24 +08:00
|
|
|
const headerSearchSelectRef = ref<ElSelectInstance>();
|
2023-03-15 15:59:21 +08:00
|
|
|
const router = useRouter();
|
|
|
|
const routes = computed(() => usePermissionStore().routes);
|
|
|
|
|
2023-04-02 01:01:56 +08:00
|
|
|
const click = () => {
|
2023-10-09 11:57:22 +08:00
|
|
|
show.value = !show.value
|
|
|
|
if (show.value) {
|
|
|
|
headerSearchSelectRef.value && headerSearchSelectRef.value.focus()
|
|
|
|
}
|
2023-03-15 15:59:21 +08:00
|
|
|
};
|
2023-04-02 01:01:56 +08:00
|
|
|
const close = () => {
|
2023-10-09 11:57:22 +08:00
|
|
|
headerSearchSelectRef.value && headerSearchSelectRef.value.blur()
|
|
|
|
options.value = []
|
|
|
|
show.value = false
|
2023-03-15 15:59:21 +08:00
|
|
|
}
|
2023-04-02 01:01:56 +08:00
|
|
|
const change = (val: any) => {
|
2023-10-09 11:57:22 +08:00
|
|
|
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");
|
|
|
|
} else {
|
|
|
|
if (query) {
|
|
|
|
router.push({ path: path, query: JSON.parse(query) });
|
2023-04-03 00:26:04 +08:00
|
|
|
} else {
|
2023-10-09 11:57:22 +08:00
|
|
|
router.push(path)
|
2023-04-03 00:26:04 +08:00
|
|
|
}
|
2023-10-09 11:57:22 +08:00
|
|
|
}
|
|
|
|
search.value = ''
|
|
|
|
options.value = []
|
|
|
|
nextTick(() => {
|
|
|
|
show.value = false
|
|
|
|
})
|
2023-03-15 15:59:21 +08:00
|
|
|
}
|
2023-04-02 01:01:56 +08:00
|
|
|
const initFuse = (list: Router) => {
|
2023-10-09 11:57:22 +08:00
|
|
|
fuse.value = new Fuse(list, {
|
|
|
|
shouldSort: true,
|
|
|
|
threshold: 0.4,
|
|
|
|
location: 0,
|
|
|
|
distance: 100,
|
|
|
|
minMatchCharLength: 1,
|
|
|
|
keys: [{
|
|
|
|
name: 'title',
|
|
|
|
weight: 0.7
|
|
|
|
}, {
|
|
|
|
name: 'path',
|
|
|
|
weight: 0.3
|
|
|
|
}]
|
|
|
|
})
|
2023-03-15 15:59:21 +08:00
|
|
|
}
|
|
|
|
// Filter out the routes that can be displayed in the sidebar
|
|
|
|
// And generate the internationalized title
|
2023-10-25 23:05:32 +08:00
|
|
|
const generateRoutes = (routes: RouteOption[], basePath = '', prefixTitle: string[] = []) => {
|
2023-10-09 11:57:22 +08:00
|
|
|
let res: Router = []
|
|
|
|
routes.forEach(r => {
|
|
|
|
// skip hidden router
|
|
|
|
if (!r.hidden) {
|
|
|
|
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],
|
|
|
|
query: ''
|
|
|
|
}
|
|
|
|
if (r.meta && r.meta.title) {
|
|
|
|
data.title = [...data.title, r.meta.title];
|
|
|
|
if (r.redirect !== 'noRedirect') {
|
|
|
|
// only push the routes with title
|
|
|
|
// special case: need to exclude parent router without redirect
|
|
|
|
res.push(data);
|
2023-04-02 01:01:56 +08:00
|
|
|
}
|
2023-10-09 11:57:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r.query) {
|
|
|
|
data.query = r.query
|
|
|
|
}
|
|
|
|
|
|
|
|
// recursive child routes
|
|
|
|
if (r.children) {
|
2023-10-25 23:05:32 +08:00
|
|
|
const tempRoutes = generateRoutes(r.children, data.path, data.title);
|
2023-10-09 11:57:22 +08:00
|
|
|
if (tempRoutes.length >= 1) {
|
|
|
|
res = [...res, ...tempRoutes];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return res;
|
2023-03-15 15:59:21 +08:00
|
|
|
}
|
2023-04-02 01:01:56 +08:00
|
|
|
const querySearch = (query: string) => {
|
2023-10-09 11:57:22 +08:00
|
|
|
if (query !== '') {
|
|
|
|
options.value = fuse.value.search(query)
|
|
|
|
} else {
|
|
|
|
options.value = []
|
|
|
|
}
|
2023-03-15 15:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(() => {
|
2023-10-09 11:57:22 +08:00
|
|
|
searchPool.value = generateRoutes(routes.value);
|
2023-03-15 15:59:21 +08:00
|
|
|
})
|
|
|
|
|
2023-04-19 23:25:34 +08:00
|
|
|
// watchEffect(() => {
|
|
|
|
// searchPool.value = generateRoutes(routes.value)
|
|
|
|
// })
|
2023-03-15 15:59:21 +08:00
|
|
|
|
|
|
|
watch(show, (value) => {
|
2023-10-09 11:57:22 +08:00
|
|
|
if (value) {
|
|
|
|
document.body.addEventListener('click', close)
|
|
|
|
} else {
|
|
|
|
document.body.removeEventListener('click', close)
|
|
|
|
}
|
2023-03-15 15:59:21 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
watch(searchPool, (list) => {
|
2023-10-09 11:57:22 +08:00
|
|
|
initFuse(list)
|
2023-03-15 15:59:21 +08:00
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
2023-04-02 01:01:56 +08:00
|
|
|
<style lang="scss" scoped>
|
2023-03-15 15:59:21 +08:00
|
|
|
.header-search {
|
2023-10-09 11:57:22 +08:00
|
|
|
font-size: 0 !important;
|
2023-03-15 15:59:21 +08:00
|
|
|
|
2023-10-09 11:57:22 +08:00
|
|
|
.search-icon {
|
|
|
|
cursor: pointer;
|
|
|
|
font-size: 18px;
|
|
|
|
vertical-align: middle;
|
|
|
|
}
|
2023-03-15 15:59:21 +08:00
|
|
|
|
2023-10-09 11:57:22 +08:00
|
|
|
.header-search-select {
|
|
|
|
font-size: 18px;
|
|
|
|
transition: width 0.2s;
|
|
|
|
width: 0;
|
|
|
|
overflow: hidden;
|
|
|
|
background: transparent;
|
|
|
|
border-radius: 0;
|
|
|
|
display: inline-block;
|
|
|
|
vertical-align: middle;
|
2023-03-15 15:59:21 +08:00
|
|
|
|
2023-10-09 11:57:22 +08:00
|
|
|
:deep(.el-input__inner) {
|
|
|
|
border-radius: 0;
|
|
|
|
border: 0;
|
|
|
|
padding-left: 0;
|
|
|
|
padding-right: 0;
|
|
|
|
box-shadow: none !important;
|
|
|
|
border-bottom: 1px solid #d9d9d9;
|
|
|
|
vertical-align: middle;
|
2023-03-15 15:59:21 +08:00
|
|
|
}
|
2023-10-09 11:57:22 +08:00
|
|
|
}
|
2023-03-15 15:59:21 +08:00
|
|
|
|
2023-10-09 11:57:22 +08:00
|
|
|
&.show {
|
|
|
|
.header-search-select {
|
|
|
|
width: 210px;
|
|
|
|
margin-left: 10px;
|
2023-03-15 15:59:21 +08:00
|
|
|
}
|
2023-10-09 11:57:22 +08:00
|
|
|
}
|
2023-03-15 15:59:21 +08:00
|
|
|
}
|
2023-04-02 01:01:56 +08:00
|
|
|
</style>
|