You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
4.4 KiB
168 lines
4.4 KiB
12 months ago
|
<template>
|
||
|
<ElDialog v-if="isModal" v-model="showSearch" :show-close="false" title="菜单搜索">
|
||
|
<!-- <el-select
|
||
|
filterable
|
||
|
:reserve-keyword="false"
|
||
|
remote
|
||
|
placeholder="请输入菜单内容"
|
||
|
:remote-method="remoteMethod"
|
||
|
style="width: 100%"
|
||
|
@change="handleChange"
|
||
|
>
|
||
|
<el-option
|
||
|
v-for="item in options"
|
||
|
:key="item.value"
|
||
|
:label="item.label"
|
||
|
:value="item.value"
|
||
|
/>
|
||
|
</el-select> -->
|
||
|
</ElDialog>
|
||
|
<div v-else class="custom-hover" @click.stop="showTopSearch = !showTopSearch">
|
||
|
<Icon icon="ep:search" />
|
||
|
<el-select
|
||
|
filterable
|
||
|
:reserve-keyword="false"
|
||
|
remote
|
||
|
placeholder="请输入菜单内容"
|
||
|
:remote-method="remoteMethod"
|
||
|
class="overflow-hidden transition-all-600"
|
||
|
:class="showTopSearch ? 'w-220px ml2' : 'w-0'"
|
||
|
@change="handleChange"
|
||
|
>
|
||
|
<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
|
||
|
</el-select>
|
||
|
</div>
|
||
|
</template>
|
||
|
|
||
|
<script lang="ts" setup>
|
||
|
import { usePermissionStore } from '@/store/modules/permission'
|
||
|
import { filterBreadcrumb } from '@/layout/components/Breadcrumb/src/helper'
|
||
|
import { filter, treeToList } from '@/utils/tree'
|
||
|
import type { RouteLocationNormalizedLoaded, RouteMeta } from 'vue-router'
|
||
|
|
||
|
defineProps({
|
||
|
isModal: {
|
||
|
type: Boolean,
|
||
|
default: true
|
||
|
}
|
||
|
})
|
||
|
|
||
|
const permissionStore = usePermissionStore()
|
||
|
const routersa = permissionStore.getRouters
|
||
|
// 解析路由 展开菜单路由
|
||
|
const analyzeRouters = (routers:any) => {
|
||
|
return recursion(routers, [])
|
||
|
}
|
||
|
// 递归 查询菜单路由
|
||
|
const recursion = (routers:any, rs) => {
|
||
|
routers.forEach((item) => {
|
||
|
if (item.children?.length > 0) {
|
||
|
recursion(item.children, rs)
|
||
|
} else {
|
||
|
rs.push(item)
|
||
|
}
|
||
|
})
|
||
|
return rs
|
||
|
}
|
||
|
|
||
|
// 递归 查询上级目录名称
|
||
|
const recursionParentName = (routers:any, rs) => {
|
||
|
let routerParent = router.getRoutes().find(itemRouter =>
|
||
|
itemRouter.path == routers.path.substring(0,routers.path.lastIndexOf('/'))
|
||
|
)
|
||
|
if (routerParent) {
|
||
|
recursionParentName(routerParent, rs)
|
||
|
} else {
|
||
|
rs.push(routers.meta.title)
|
||
|
}
|
||
|
return rs
|
||
|
}
|
||
|
|
||
|
const router = useRouter() // 路由对象
|
||
|
const showSearch = ref(false) // 是否显示弹框
|
||
|
const showTopSearch = ref(false) // 是否显示顶部搜索框
|
||
|
const value: Ref = ref('') // 用户输入的值
|
||
|
const levelList = ref<AppRouteRecordRaw[]>([])
|
||
|
const menuRouters = computed(() => {
|
||
|
const routers = permissionStore.getRouters
|
||
|
return filterBreadcrumb(routers)
|
||
|
})
|
||
|
// 20240111 cxm 修改 根据当前登录人获取有效路由 并解析展开所有菜单路由过滤目录路由
|
||
|
const routers = analyzeRouters(filterBreadcrumb(routersa))
|
||
|
const options = computed(() => {
|
||
|
// 提示选项
|
||
|
if (!value.value) {
|
||
|
return []
|
||
|
}
|
||
|
|
||
|
let list = routers.filter((item: any) => {
|
||
|
if (item.meta.title?.indexOf(value.value) > -1 || item.path.indexOf(value.value) > -1) {
|
||
|
return true
|
||
|
}
|
||
|
})
|
||
|
|
||
|
return list.map((item) => {
|
||
|
let menuName = ''
|
||
|
levelList.value = filter<AppRouteRecordRaw>(unref(menuRouters), (node: AppRouteRecordRaw) => {
|
||
|
return node.path === item.path
|
||
|
})
|
||
|
const breadcrumbList = treeToList<AppRouteRecordRaw[]>(unref(levelList))
|
||
|
breadcrumbList.map((v) => {
|
||
|
menuName += v?.meta?.title + ' / '
|
||
|
})
|
||
|
return {
|
||
|
label: `${item.meta.title} ----> ${menuName.substring(0,menuName.length-3)}`,
|
||
|
value: item.path
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
|
||
|
const remoteMethod = (data) => {
|
||
|
// 这里可以执行相应的操作(例如打开搜索框等)
|
||
|
value.value = data
|
||
|
}
|
||
|
|
||
|
const handleChange = (path) => {
|
||
|
console.log(126,path)
|
||
|
router.push({ path })
|
||
|
console.log(128,11111)
|
||
|
hiddenTopSearch();
|
||
|
console.log(130,222222)
|
||
|
}
|
||
|
|
||
|
const hiddenTopSearch = () => {
|
||
|
showTopSearch.value = false
|
||
|
}
|
||
|
|
||
|
onMounted(() => {
|
||
|
window.addEventListener('keydown', listenKey)
|
||
|
window.addEventListener('click', hiddenTopSearch)
|
||
|
})
|
||
|
|
||
|
onUnmounted(() => {
|
||
|
window.removeEventListener('keydown', listenKey)
|
||
|
window.removeEventListener('click', hiddenTopSearch)
|
||
|
})
|
||
|
|
||
|
// 监听 ctrl + k
|
||
|
const listenKey = (event) => {
|
||
|
if ((event.ctrlKey || event.metaKey) && event.key === 'k') {
|
||
|
showSearch.value = !showSearch.value
|
||
|
// 这里可以执行相应的操作(例如打开搜索框等)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
defineExpose({
|
||
|
openSearch: () => {
|
||
|
showSearch.value = true
|
||
|
}
|
||
|
})
|
||
|
</script>
|
||
|
<style lang="scss" scoped>
|
||
|
.w-0 {
|
||
|
width: 0 !important;
|
||
|
}
|
||
|
.w-220px {
|
||
|
width: 220px !important;
|
||
|
}
|
||
|
</style>
|