PageLayout.vue 4.31 KB
Newer Older
wb-ct393452's avatar
wb-ct393452 committed
1 2
<template>
    <div class="page-layout">
3 4 5 6 7 8 9 10
        <page-header
            ref="pageHeader"
            :style="`margin-top: ${multiPage ? 0 : -24}px`"
            :breadcrumb="breadcrumb"
            :title="pageTitle"
            :logo="logo"
            :avatar="avatar"
        >
wb-ct393452's avatar
wb-ct393452 committed
11 12 13 14 15 16
            <slot name="action" slot="action"></slot>
            <slot slot="content" name="headerContent"></slot>
            <div slot="content" v-if="!this.$slots.headerContent && desc">
                <p>{{ desc }}</p>
                <div v-if="this.linkList" class="link">
                    <template v-for="(link, index) in linkList">
17
                        <a :key="index" :href="link.href"> <a-icon :type="link.icon" />{{ link.title }} </a>
wb-ct393452's avatar
wb-ct393452 committed
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
                    </template>
                </div>
            </div>
            <slot v-if="this.$slots.extra" slot="extra" name="extra"></slot>
        </page-header>
        <div ref="page" :class="['page-content', layout, pageWidth]">
            <slot></slot>
        </div>
    </div>
</template>

<script>
import PageHeader from '@/components/page/PageHeader';
import { mapState, mapMutations } from 'vuex';
import { getI18nKey } from '@/utils/routerUtil';

export default {
    name: 'PageLayout',
    components: { PageHeader },
    props: ['desc', 'logo', 'title', 'avatar', 'linkList', 'extraImage'],
    data() {
        return {
            page: {},
            pageHeaderHeight: 0,
        };
    },
    watch: {
        $route() {
            this.page = this.$route.meta.page;
        },
    },
    updated() {
        if (!this._inactive) {
            this.updatePageHeight();
        }
    },
    activated() {
        this.updatePageHeight();
    },
    deactivated() {
        this.updatePageHeight(0);
    },
    mounted() {
        this.updatePageHeight();
    },
    created() {
        this.page = this.$route.meta.page;
    },
    beforeDestroy() {
        this.updatePageHeight(0);
    },
    computed: {
        ...mapState('settingModule', ['layout', 'multiPage', 'pageMinHeight', 'pageWidth', 'customTitles']),
        pageTitle() {
            let pageTitle = this.page && this.page.title;
            return this.customTitle || (pageTitle && this.$t(pageTitle)) || this.title || this.routeName;
        },
        routeName() {
            const route = this.$route;
            return this.$t(getI18nKey(route.matched[route.matched.length - 1].path));
        },
        breadcrumb() {
            let page = this.page;
            let breadcrumb = page && page.breadcrumb;
            if (breadcrumb) {
                let i18nBreadcrumb = [];
84
                breadcrumb.forEach(item => {
wb-ct393452's avatar
wb-ct393452 committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
                    i18nBreadcrumb.push(this.$t(item));
                });
                return i18nBreadcrumb;
            } else {
                return this.getRouteBreadcrumb();
            }
        },
        marginCorrect() {
            return this.multiPage ? 24 : 0;
        },
    },
    methods: {
        ...mapMutations('settingModule', ['correctPageMinHeight']),
        getRouteBreadcrumb() {
            let routes = this.$route.matched;
            const path = this.$route.path;
            let breadcrumb = [];
            routes
103 104
                .filter(item => path.includes(item.path))
                .forEach(route => {
wb-ct393452's avatar
wb-ct393452 committed
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
                    const path = route.path.length === 0 ? '/home' : route.path;
                    breadcrumb.push(this.$t(getI18nKey(path)));
                });
            let pageTitle = this.page && this.page.title;
            if (this.customTitle || pageTitle) {
                breadcrumb[breadcrumb.length - 1] = this.customTitle || pageTitle;
            }
            return breadcrumb;
        },
        /**
         * 用于计算页面内容最小高度
         * @param newHeight
         */
        updatePageHeight(newHeight = this.$refs.pageHeader.$el.offsetHeight + this.marginCorrect) {
            this.correctPageMinHeight(this.pageHeaderHeight - newHeight);
            this.pageHeaderHeight = newHeight;
        },
    },
};
</script>

<style lang="less">
.link {
    line-height: 24px;
    a {
        font-size: 14px;
        margin-right: 32px;
        i {
            font-size: 22px;
            margin-right: 8px;
        }
    }
}
.page-content {
    position: relative;
140

wb-ct393452's avatar
wb-ct393452 committed
141 142 143 144 145 146
    &.head.fixed {
        margin: 0 auto;
        max-width: 1400px;
    }
}
</style>