Files
RuoYi-Vue/ruoyi-ui/src/components/HeaderSearch/index.vue

232 lines
5.4 KiB
Vue
Raw Normal View History

2019-10-08 09:14:38 +08:00
<template>
2025-04-21 13:22:00 +08:00
<div class="header-search">
2019-10-08 09:14:38 +08:00
<svg-icon class-name="search-icon" icon-class="search" @click.stop="click" />
2025-04-21 13:22:00 +08:00
<el-dialog
:visible.sync="show"
width="600px"
@close="close"
:show-close="false"
append-to-body
2019-10-08 09:14:38 +08:00
>
2025-04-21 13:22:00 +08:00
<el-input
v-model="search"
ref="headerSearchSelectRef"
size="large"
@input="querySearch"
prefix-icon="Search"
placeholder="菜单搜索支持标题、URL模糊查询"
>
</el-input>
<el-scrollbar wrap-class="right-scrollbar-wrapper">
<div class="result-wrap">
<div class="search-item" v-for="item in options" :key="item.path">
<div class="left">
<svg-icon class="menu-icon" :icon-class="item.icon" />
</div>
<div class="search-info" @click="change(item)">
<div class="menu-title">
{{ item.title.join(" / ") }}
</div>
<div class="menu-path">
{{ item.path }}
</div>
</div>
</div>
</div>
</el-scrollbar>
</el-dialog>
2019-10-08 09:14:38 +08:00
</div>
</template>
<script>
2021-01-05 09:44:08 +08:00
import Fuse from 'fuse.js/dist/fuse.min.js'
2019-10-08 09:14:38 +08:00
import path from 'path'
2024-11-20 10:42:41 +08:00
import { isHttp } from '@/utils/validate'
2019-10-08 09:14:38 +08:00
export default {
name: 'HeaderSearch',
data() {
return {
search: '',
options: [],
searchPool: [],
show: false,
fuse: undefined
}
},
computed: {
routes() {
return this.$store.getters.defaultRoutes
2019-10-08 09:14:38 +08:00
}
},
watch: {
routes() {
this.searchPool = this.generateRoutes(this.routes)
},
searchPool(list) {
this.initFuse(list)
}
},
mounted() {
this.searchPool = this.generateRoutes(this.routes)
},
methods: {
click() {
this.show = !this.show
if (this.show) {
this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.focus()
2025-04-21 13:22:00 +08:00
this.options = this.searchPool
2019-10-08 09:14:38 +08:00
}
},
close() {
this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur()
2025-04-21 13:22:00 +08:00
this.search = ''
2019-10-08 09:14:38 +08:00
this.options = []
this.show = false
},
change(val) {
2025-04-21 13:22:00 +08:00
const path = val.path
const query = val.query
2024-11-20 10:42:41 +08:00
if(isHttp(val.path)) {
// http(s):// 路径新窗口打开
2021-07-11 16:31:10 +08:00
const pindex = path.indexOf("http");
2025-04-21 13:22:00 +08:00
window.open(path.substr(pindex, path.length), "_blank")
} else {
if (query) {
2025-04-21 13:22:00 +08:00
this.$router.push({ path: path, query: JSON.parse(query) })
} else {
this.$router.push(path)
}
}
2019-10-08 09:14:38 +08:00
this.search = ''
this.options = []
this.$nextTick(() => {
this.show = false
})
},
initFuse(list) {
this.fuse = 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
}]
})
},
// Filter out the routes that can be displayed in the sidebar
// And generate the internationalized title
2023-10-09 21:26:40 +08:00
generateRoutes(routes, basePath = '/', prefixTitle = []) {
2019-10-08 09:14:38 +08:00
let res = []
for (const router of routes) {
// skip hidden router
if (router.hidden) { continue }
const data = {
2024-11-20 10:42:41 +08:00
path: !isHttp(router.path) ? path.resolve(basePath, router.path) : router.path,
2025-04-21 13:22:00 +08:00
title: [...prefixTitle],
icon: ''
2019-10-08 09:14:38 +08:00
}
if (router.meta && router.meta.title) {
data.title = [...data.title, router.meta.title]
2025-04-21 13:22:00 +08:00
data.icon = router.meta.icon
2019-10-08 09:14:38 +08:00
if (router.redirect !== 'noRedirect') {
// only push the routes with title
// special case: need to exclude parent router without redirect
res.push(data)
}
}
if (router.query) {
data.query = router.query
}
2019-10-08 09:14:38 +08:00
// recursive child routes
if (router.children) {
2023-10-09 21:26:40 +08:00
const tempRoutes = this.generateRoutes(router.children, data.path, data.title)
2019-10-08 09:14:38 +08:00
if (tempRoutes.length >= 1) {
res = [...res, ...tempRoutes]
}
}
}
return res
},
querySearch(query) {
if (query !== '') {
2025-04-21 13:22:00 +08:00
this.options = this.fuse.search(query).map((item) => item.item) ?? this.searchPool
2019-10-08 09:14:38 +08:00
} else {
2025-04-21 13:22:00 +08:00
this.options = this.searchPool
2019-10-08 09:14:38 +08:00
}
}
}
}
</script>
2025-04-21 13:22:00 +08:00
<style lang='scss' scoped>
::v-deep {
.el-dialog__header {
padding: 0 !important;
}
}
2019-10-08 09:14:38 +08:00
2025-04-21 13:22:00 +08:00
.header-search {
2019-10-08 09:14:38 +08:00
.search-icon {
cursor: pointer;
font-size: 18px;
vertical-align: middle;
}
2025-04-21 13:22:00 +08:00
}
2019-10-08 09:14:38 +08:00
2025-04-21 13:22:00 +08:00
.result-wrap {
height: 280px;
margin: 12px 0;
2019-10-08 09:14:38 +08:00
2025-04-21 13:22:00 +08:00
.search-item {
display: flex;
height: 48px;
.left {
width: 60px;
text-align: center;
.menu-icon {
width: 18px;
height: 18px;
margin-top: 5px;
}
2019-10-08 09:14:38 +08:00
}
2025-04-21 13:22:00 +08:00
.search-info {
padding-left: 5px;
width: 100%;
display: flex;
flex-direction: column;
justify-content: flex-start;
.menu-title,
.menu-path {
height: 20px;
}
.menu-path {
color: #ccc;
font-size: 10px;
}
2019-10-08 09:14:38 +08:00
}
}
2025-04-21 13:22:00 +08:00
.search-item:hover {
cursor: pointer;
}
2019-10-08 09:14:38 +08:00
}
</style>
2025-04-21 13:22:00 +08:00