123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <template>
- <div class="header-search">
- <svg-icon class-name="search-icon" icon-class="search" @click.stop="click" />
- <el-dialog
- :visible.sync="show"
- width="600px"
- @close="close"
- :show-close="false"
- append-to-body
- >
- <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>
- </div>
- </template>
- <script>
- import Fuse from 'fuse.js/dist/fuse.min.js'
- import path from 'path'
- import { isHttp } from '@/utils/validate'
- export default {
- name: 'HeaderSearch',
- data() {
- return {
- search: '',
- options: [],
- searchPool: [],
- show: false,
- fuse: undefined
- }
- },
- computed: {
- routes() {
- return this.$store.getters.defaultRoutes
- }
- },
- 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()
- this.options = this.searchPool
- }
- },
- close() {
- this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur()
- this.search = ''
- this.options = []
- this.show = false
- },
- change(val) {
- const path = val.path
- const query = val.query
- if(isHttp(val.path)) {
- // http(s):// 路径新窗口打开
- const pindex = path.indexOf("http");
- window.open(path.substr(pindex, path.length), "_blank")
- } else {
- if (query) {
- this.$router.push({ path: path, query: JSON.parse(query) })
- } else {
- this.$router.push(path)
- }
- }
- 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
- generateRoutes(routes, basePath = '/', prefixTitle = []) {
- let res = []
- for (const router of routes) {
- // skip hidden router
- if (router.hidden) { continue }
- const data = {
- path: !isHttp(router.path) ? path.resolve(basePath, router.path) : router.path,
- title: [...prefixTitle],
- icon: ''
- }
- if (router.meta && router.meta.title) {
- data.title = [...data.title, router.meta.title]
- data.icon = router.meta.icon
- 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
- }
- // recursive child routes
- if (router.children) {
- const tempRoutes = this.generateRoutes(router.children, data.path, data.title)
- if (tempRoutes.length >= 1) {
- res = [...res, ...tempRoutes]
- }
- }
- }
- return res
- },
- querySearch(query) {
- if (query !== '') {
- this.options = this.fuse.search(query).map((item) => item.item) ?? this.searchPool
- } else {
- this.options = this.searchPool
- }
- }
- }
- }
- </script>
- <style lang='scss' scoped>
- ::v-deep {
- .el-dialog__header {
- padding: 0 !important;
- }
- }
- .header-search {
- .search-icon {
- cursor: pointer;
- font-size: 18px;
- vertical-align: middle;
- }
- }
- .result-wrap {
- height: 280px;
- margin: 12px 0;
- .search-item {
- display: flex;
- height: 48px;
- .left {
- width: 60px;
- text-align: center;
- .menu-icon {
- width: 18px;
- height: 18px;
- margin-top: 5px;
- }
- }
- .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;
- }
- }
- }
- .search-item:hover {
- cursor: pointer;
- }
- }
- </style>
|