App.vue 11 KB
<template>
    <a-config-provider :locale="locale" :theme="themeJson" :transformCellText="transformCellText">
        <a-layout class="admin-layout">
            <a-layout-header class="header">
                <a-flex justify="space-between">
                    <div class="logo">
                        <img src="/assets/images/logo.png"/>
                        <h3>广联二手车</h3>

                        <breadcrumb-bar></breadcrumb-bar>
                    </div>
                    <div>
                        <a-dropdown class="select-dropdown">
                            <a-space>
                                <a-avatar v-if="this.authenticity&&this.authenticity.avatar" :src="this.authenticity.avatar" :size="32"/>
                                <a-avatar v-else :size="32" title="用户头像">
                                    <template #icon>
                                        <UserOutlined/>
                                    </template>
                                </a-avatar>
                                <span class="user-name" style="cursor: pointer">{{ loginUserName }}</span>
                                <DownOutlined/>
                            </a-space>
                            <template #overlay>
                                <a-menu>
                                    <a-menu-item key="0">
                                        <router-link to="/security/profile">
                                            <UserOutlined class="mr-8"/>
                                            个人中心
                                        </router-link>
                                    </a-menu-item>
                                    <a-menu-item key="4">
                                        <a @click="logout" rel="noopener noreferrer">
                                            <LogoutOutlined class="mr-8"/>
                                            退出
                                        </a>
                                    </a-menu-item>
                                </a-menu>
                            </template>
                        </a-dropdown>
                    </div>
                </a-flex>
            </a-layout-header>
            <a-layout>
                <a-layout-sider class="menu-sidebar" v-model:collapsed="collapsed" theme="light" :trigger="null" width="200" collapsible>
                    <a-menu v-model:selectedKeys="selectedKeys" v-model:openKeys="openKeys" mode="inline" theme="light" class="main-menu">
                        <a-menu-item key="dashboard">
                            <template #icon>
                                <img v-if="selectedKeys.includes('dashboard')" src="/assets/icons/home_on.png" alt="" class="menu-item-icon">
                                <img v-else src="/assets/icons/home_on.png" alt="" class="menu-item-icon">
                            </template>
                            <router-link to="/dashboard">
                                首页
                            </router-link>
                        </a-menu-item>
                        <privilege-menu :privileges="privileges" :open-keys="openKeys"></privilege-menu>
                        <a-menu-item key="profile">
                            <template #icon>
                                <img v-if="selectedKeys.includes('profile')" src="/assets/icons/profile_on.png" alt="" class="menu-item-icon">
                                <img v-else src="/assets/icons/profile_on.png" alt="" class="menu-item-icon">
                            </template>
                            <router-link to="/security/profile">
                                个人中心
                            </router-link>
                        </a-menu-item>
                    </a-menu>
                    <div style="position: absolute;right: -10px;top:260px;box-shadow: 0px 4px 8px  #00000019;border: 1px solid #E1E3E6FF;padding:2px 6px 4px 6px;border-radius: 14px;">
                        <RightOutlined v-if="collapsed" class="trigger" @click="() => (collapsed = !collapsed)" style="font-size: 10px;color: #777C85;"/>
                        <LeftOutlined v-else class="trigger" @click="() => (collapsed = !collapsed)" style="font-size: 10px;color: #777C85;"/>
                    </div>
                </a-layout-sider>
                <a-layout-content class="inner-main">
                    <div class="main" :class="{greyBg:(this.$route.path==='/dashboard'||this.$route.path.includes('/finance/dashboard'))}">
                        <router-view/>
                    </div>
                </a-layout-content>
            </a-layout>
        </a-layout>
    </a-config-provider>
</template>
<script lang="ts">
import {Component, Vue} from "vue-facing-decorator";
import {
    BarChartOutlined,
    CodepenCircleOutlined,
    CommentOutlined,
    ContainerOutlined,
    CustomerServiceOutlined,
    DatabaseOutlined, DownOutlined, ExclamationCircleOutlined,
    FileDoneOutlined,
    FileOutlined,
    HomeOutlined,
    LaptopOutlined, LeftCircleOutlined, LeftOutlined, LogoutOutlined,
    MessageOutlined,
    NotificationOutlined,
    QuestionCircleOutlined, RightCircleOutlined, RightOutlined,
    SearchOutlined,
    SettingOutlined,
    TeamOutlined,
    UserOutlined
} from '@ant-design/icons-vue';
import 'dayjs/locale/zh-cn';
import zhCN from 'ant-design-vue/es/locale/zh_CN';
import lightTheme from '@/assets/style/theme/light';
import {Privilege} from "lib/src/core/security/Privilege";
import BreadcrumbBar from "@/components/BreadcrumbBar.vue";
import {Getter} from "vuex-facing-decorator";
import PrivilegeMenu from "@/views/PrivilegeMenu.vue";
import {createVNode} from "vue";
import {message, Modal} from "ant-design-vue";
import Authenticity from "lib/src/core/security/Authenticity";

@Component({
    components: {
        BreadcrumbBar, PrivilegeMenu, LeftCircleOutlined,
        RightCircleOutlined, RightOutlined, LeftOutlined,
        UserOutlined, LaptopOutlined, SearchOutlined, FileOutlined, ContainerOutlined, NotificationOutlined, DatabaseOutlined,
        FileDoneOutlined, CodepenCircleOutlined, SettingOutlined, QuestionCircleOutlined, TeamOutlined, BarChartOutlined,
        MessageOutlined, CustomerServiceOutlined, CommentOutlined, HomeOutlined, LogoutOutlined, DownOutlined
    }
})
export default class App extends Vue {
    locale = zhCN;
    themeJson: any = lightTheme;
    selectedKeys = ['dashboard'];
    openKeys = [];

    collapsed = false;

    theme = '';
    body;
    loginUserName = "";
    // 底部菜单权限
    privileges: Privilege[] = [];

    authenticity: Authenticity = null;

    @Getter("security/getPrivileges") getPrivileges;

// 全局组件配置:针对 Table 列设置默认渲染
    transformCellText = ({text}) => {
        // 空值判断逻辑(和之前一致,覆盖所有空值场景)
        const isEmpty =
            text === undefined ||
            text === null ||
            text === '' ||
            (typeof text === 'string' && text.trim() === '') ||
            (Array.isArray(text) && text.length === 0) ||
            (typeof text === 'object' && text !== null && Object.keys(text).length === 0);
        return isEmpty ? '-' : text;
    }

    created(): void {
        // this.$router.replace('/dashboard');
        this.authenticity = this.$auth.getAuthentication();
        this.loginUserName = this.$auth.getAuthentication().fullName;
        // 底部菜单权限
        this.privileges = this.getPrivileges;
        // 初始化菜单选中状态
        this.initMenuSelected();
    }

    mounted(): void {
        // 监听路由变化
        this.$router.afterEach(() => {
            this.initMenuSelected();
        });
    }

    /**
     * 初始化菜单选中状态
     */
    initMenuSelected(): void {
        const currentPath = this.$route.path;
        const menuKey = this.findMenuKeyByPath(this.privileges, currentPath);
        if (menuKey) {
            this.selectedKeys = [menuKey];
        } else if (currentPath === '/dashboard') {
            this.selectedKeys = ['dashboard'];
        } else if (currentPath === '/security/profile') {
            this.selectedKeys = ['profile'];
        }
    }

    /**
     * 根据路由路径查找对应的菜单键
     * @param privileges 权限菜单列表
     * @param path 当前路由路径
     * @returns 对应的菜单键
     */
    findMenuKeyByPath(privileges: any[], path: string): string | null {
        for (const item of privileges) {
            if (item.value === path) {
                return item.code;
            }
            if (item.children && item.children.length > 0) {
                const foundKey = this.findMenuKeyByPath(item.children, path);
                if (foundKey) {
                    return foundKey;
                }
            }
        }
        return null;
    }

    logout() {
        Modal.confirm({
            title: '提示',
            icon: createVNode(ExclamationCircleOutlined),
            content: createVNode('p', {}, '你确认退出吗?'),
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
                this.signOut();
            },
            onCancel: () => {
                console.log('Cancel');
            },
        });
    }

    /**
     * 退出登录
     */
    signOut(): void {
        this.$auth.logout();
        localStorage.removeItem("Authorization");
        localStorage.removeItem("token");
        message.success('你已经成功退出');
        window.location.href = '/admin/logout';
    }

}
</script>
<style lang="less" scoped>


.admin-layout {
    padding: 0;
    margin: 0 auto;
    height: 100vh;

    .header {
        height: 56px;
        line-height: 56px;
        //background: linear-gradient(120deg, #2452B5 0%, #83aef5 100%);
        background-color: #fff;
        padding: 0 24px;
        border-bottom: 1px solid #F2F4F7;


        .logo {
            display: flex;
            align-items: center;

            img {
                width: 48px;
                height: 24px;
                margin-left: 8px;
                margin-right: 8px;
            }

            h3 {
                font-family: "Microsoft YaHei", "SimHei", "Heiti SC", sans-serif;
                font-size: 20px;
                font-weight: 600;
                color: #0D162A !important;
            }
        }

        .user-name {
            color: #000;
        }
    }


    .menu-sidebar {
        //height: calc(100vh - 70px);
        height: 100%;
        //background: #2755c2;
        background-color: #F7F8F9;

        .main-menu {
            height: calc(100vh - 90px);
            //border-right: 0;
            overflow: hidden auto;
            background-color: rgba(247, 248, 249, 1);
        }

        ::v-deep(.ul-height) {
            height: calc(100vh - 96px) !important;
            overflow: hidden auto;

            &::-webkit-scrollbar {
                display: none;
            }
        }
    }

    .greyBg {
        background: #F7F8F9FF !important;
    }

    .menu-item-icon {
        width: 18px;
    }
}


</style>