xiangpei
2025-06-05 1fa6ac40e2ce16e1174cec9ca538d45eeb660fdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<template>
    <div class="layout-search-dialog">
        <el-dialog
            v-model="state.isShowSearch"
            destroy-on-close
            :show-close="false"
        >
            <template #footer>
                <el-autocomplete
                    ref="layoutMenuAutocompleteRef"
                    v-model="state.menuQuery"
                    :fetch-suggestions="menuSearch"
                    placeholder="搜索"
                    :fit-input-width="true"
                    @select="onHandleSelect"
                >
                    <template #prefix>
                        <svg-icon
                            class-name="search-icon"
                            icon-class="search"
                        />
                    </template>
                    <template #default="{ item }">
                        <div>
                            <svg-icon :icon-class="item.icon" class="mr5" />
                            {{ item.title }}
                        </div>
                    </template>
                </el-autocomplete>
            </template>
        </el-dialog>
    </div>
</template>
 
<script>
import { getNormalPath } from '@/utils/ruoyi';
import { isHttp } from '@/utils/validate';
import usePermissionStore from '@/store/modules/permission';
import { RouteRecordRaw } from 'vue-router';
 
export default {
    data() {
        return {
            state: {
                isShowSearch: false,
                menuQuery: '',
                menuList: []
            }
        }
    },
    created() {
        // this.openSearch()
    },
    methods: {
        // 搜索弹窗打开
        openSearch() {
            this.state.menuQuery = '';
            this.state.isShowSearch = true;
            
            this.state.menuList = this.generateRoutes(usePermissionStore.state.routes);
            this.$nextTick(() => {
                setTimeout(() => {
                    this.layoutMenuAutocompleteRef.value.focus();
                });
            });
        },
        // 搜索弹窗关闭
        closeSearch() {
            this.state.isShowSearch = false;
        },
        // 菜单搜索数据过滤
        menuSearch(queryString, cb) {
            let options = state.menuList.filter((item) => {
                return item.title.indexOf(queryString) > -1;
            });
            this.cb(options);
        },
        // Filter out the routes that can be displayed in the sidebar
        // And generate the internationalized title
        generateRoutes(routes, basePath, prefixTitle) {
            let res = [];
            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,
                        icon: r.meta?.icon,
                        title: [...prefixTitle]
                    };
                    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);
                        }
                    }
                    // recursive child routes
                    if (r.children) {
                        const tempRoutes = generateRoutes(r.children, data.path, data.title);
                        if (tempRoutes.length >= 1) {
                            res = [...res, ...tempRoutes];
                        }
                    }
                }
            });
            res.forEach((item) => {
                if (item.title instanceof Array) {
                    item.title = item.title.join('/');
                }
            });
            return res;
        },
        // 当前菜单选中时
        onHandleSelect(val) {
            const paths = val.path;
            if (isHttp(paths)) {
                // http(s):// 路径新窗口打开
                const pindex = paths.indexOf('http');
                window.open(paths.substring(pindex, paths.length), '_blank');
            } else {
                router.push(paths);
            }
            state.menuQuery = '';
            closeSearch();
        }
    }
}
</script>
 
<style scoped lang="scss">
.layout-search-dialog {
    position: relative;
    ::v-deep .el-dialog {
        padding: 0;
        .el-dialog__header,
        .el-dialog__body {
            display: none;
        }
        .el-dialog__footer {
            width: 100%;
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
            top: -53vh;
        }
    }
    ::v-deep.el-autocomplete {
        width: 560px;
        position: absolute;
        top: 150px;
        left: 50%;
        transform: translateX(-50%);
    }
}
</style>