张卓
2022-09-20 5aead44ba1be31db948dfd8362c2bfcbedbbce29
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<template>
<div class="aside">
    <a-menu mode="inline" :inlineCollapsed="isCollapse"
    v-model="currentRouteName"
    @openChange="handleOpenChange"
    :openKeys="openMenus">
        <template v-for="menuitem in menuData">
            <a-menu-item :key="menuitem.name" v-if="menuitem.children==null||menuitem.children.length===1" v-show="!menuitem.meta.showFlag">
                <router-link :to="menuitem.name">
                    <a-icon :type="menuitem.meta.icon" />
                    <span>{{displayMenuTitle(menuitem)}}</span>
                </router-link>
            </a-menu-item>
            <a-sub-menu :key="menuitem.name" v-else-if="menuitem.children!==null" >
                <template slot="title">
                    <a-icon :type="menuitem.meta.icon" />
                    <span>
                        {{displayMenuTitle(menuitem)}}
                    </span>
                </template>
                <a-menu-item :key="child.name" v-for="child in menuitem.children">
                    <router-link :to="child.name">
                        {{displayMenuTitle(child)}}
                    </router-link>
                </a-menu-item>
            </a-sub-menu>
        </template>
 
    </a-menu>
 
</div>
</template>
 
<script lang="ts">
import {
    Component,
    Prop,
    Vue,
    Watch,
} from 'vue-property-decorator';
import {
    State,
    Mutation,
    namespace,
} from 'vuex-class';
 
import VueRouter from 'vue-router';
import router from "@/route/router";
 
import { appModule, aclModule} from '@/store';
 
import * as _ from 'lodash';
 
import aclService from '@/core/AclService';
import { menuPemission } from '@/core';
 
@Component({
    components: {},
})
export default class AdminSidebar extends Vue {
 
    @appModule.State('isCollapse')
    private isCollapse!: boolean;
 
    @aclModule.State('permission')
    private permission!: string[];
 
    private rootSubmenuKeys: any[] = [];
 
    private currentRouteName: any[] = [];
    private openMenus: any[] = [];
    private rout: any = null
 
    constructor() {
        super();
    }
 
    get menuData() {
        let router: any = this.$router;
        this.rout = this.$router
        let routeList = _.cloneDeep(router.options.routes);
        let menu = menuPemission(routeList, this.permission);
      return menu;
    }
    // @Watch('rout', {
    //   deep: true,
    //   immediate: true
    // })
    // private menuChange(newRouter: any, oldRouter: any) {
    //   console.log(oldRouter);
    //   console.log(newRouter);
    // }
    private parentMenuName(name: any): string {
        const menuList = this.menuData;
        const childList = _.map(menuList, (item: any) => {
            return _.map(item.children, (citem: any) => {
                citem.parentName = item.name;
                return citem;
            });
        });
        const list = _.flatten(childList);
        const menu = _.find(list, (o: any) => o.name === name);
        return menu.parentName;
    }
 
    private mounted() {
      this.currentRouteName = [this.$route.name];
        const roots = _.filter(this.menuData, (item: any) => item.children != null );
        this.rootSubmenuKeys = _.map(roots, 'name');
        this.initOpenMenus();
    }
 
    private handleOpenChange(openKeys: any) {
        const latestOpenKey = openKeys.find((key: any) => this.openMenus.indexOf(key) === -1);
        if (this.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
            this.openMenus = openKeys;
        } else {
            this.openMenus = latestOpenKey ? [latestOpenKey] : [];
        }
    }
 
    private initOpenMenus() {
        const name = this.$route.name;
        const openKey = this.parentMenuName(name);
        this.handleOpenChange([openKey]);
    }
 
    private displayMenuTitle(menu: any) {
        if (menu.meta.i18n) {
            return this.$t(menu.meta.i18n);
        }
        return menu.meta.title;
    }
 
    @Watch('$route')
    private watchRouteName(newVal: any, oldVal: any) {
        this.currentRouteName = [newVal.name];
    }
 
    @Watch('isCollapse')
    private watchisCollapse(newVal: any, oldVal: any) {
        if (newVal === true) {
            this.openMenus = [];
        } else {
            this.initOpenMenus();
        }
    }
}
</script>
 
<style lang="less">
.ant-menu-submenu-selected{
    background-color:#fafafa;
    /*
    &:after{
        border-right: 3px solid #1890ff;
        content: "";
        position: relative;
    }*/
 
    .ant-menu.ant-menu-inline.ant-menu-sub{
        background-color:#fafafa;
        // border-right: 3px solid #1890ff;
    }
 
    i{
        color: #1890ff;
        ::before{
            color: #1890ff;
        }
        ::after{
            color: #1890ff;
        }
    }
 
    span{
        color: #1890ff;
    }
 
}
</style>