2022-04-06 16:56:15 +08:00
|
|
|
import { defineComponent, computed, nextTick, onMounted, watch, onBeforeUnmount } from 'vue';
|
2022-02-28 18:28:14 +08:00
|
|
|
import { storeToRefs } from 'pinia';
|
2022-03-24 01:37:51 +08:00
|
|
|
import { useRoute, useRouter } from 'vue-router';
|
|
|
|
import { usePermissionStore, useSettingStore, useTabsRouterStore } from '@/store';
|
2022-02-28 18:28:14 +08:00
|
|
|
|
2022-03-24 01:37:51 +08:00
|
|
|
import LayoutHeader from './components/Header.vue';
|
|
|
|
import LayoutBreadcrumb from './components/Breadcrumb.vue';
|
|
|
|
import LayoutFooter from './components/Footer.vue';
|
|
|
|
import LayoutSideNav from './components/SideNav';
|
|
|
|
import LayoutContent from './components/Content.vue';
|
|
|
|
import Setting from './setting.vue';
|
2021-12-12 01:04:13 +08:00
|
|
|
|
2021-12-22 12:07:29 +08:00
|
|
|
import { prefix } from '@/config/global';
|
2022-03-25 01:05:01 +08:00
|
|
|
import { TRouterInfo } from '@/interface';
|
2022-03-24 01:37:51 +08:00
|
|
|
|
2021-12-12 01:04:13 +08:00
|
|
|
import '@/style/layout.less';
|
|
|
|
|
2021-12-22 12:07:29 +08:00
|
|
|
const name = `${prefix}-base-layout`;
|
2021-12-12 01:04:13 +08:00
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
name,
|
2022-02-28 18:28:14 +08:00
|
|
|
setup() {
|
|
|
|
const route = useRoute();
|
2022-03-24 01:37:51 +08:00
|
|
|
const router = useRouter();
|
2022-02-28 18:28:14 +08:00
|
|
|
const permissionStore = usePermissionStore();
|
|
|
|
const settingStore = useSettingStore();
|
2022-03-24 01:37:51 +08:00
|
|
|
const tabsRouterStore = useTabsRouterStore();
|
2022-02-28 18:28:14 +08:00
|
|
|
const { routers: menuRouters } = storeToRefs(permissionStore);
|
|
|
|
const setting = storeToRefs(settingStore);
|
|
|
|
|
|
|
|
const mainLayoutCls = computed(() => [
|
|
|
|
{
|
|
|
|
't-layout--with-sider': settingStore.showSidebar,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
const headerMenu = computed(() => {
|
|
|
|
if (settingStore.layout === 'mix') {
|
|
|
|
if (settingStore.splitMenu) {
|
|
|
|
return menuRouters.value.map((menu) => ({
|
2021-12-12 15:46:02 +08:00
|
|
|
...menu,
|
|
|
|
children: [],
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
2022-02-28 18:28:14 +08:00
|
|
|
return menuRouters.value;
|
|
|
|
});
|
|
|
|
|
|
|
|
const sideMenu = computed(() => {
|
|
|
|
const { layout, splitMenu } = settingStore;
|
|
|
|
let newMenuRouters = menuRouters.value;
|
2021-12-12 15:46:02 +08:00
|
|
|
if (layout === 'mix' && splitMenu) {
|
2022-02-28 18:28:14 +08:00
|
|
|
newMenuRouters.forEach((menu) => {
|
|
|
|
if (route.path.indexOf(menu.path) === 0) {
|
|
|
|
newMenuRouters = menu.children.map((subMenu) => ({ ...subMenu, path: `${menu.path}/${subMenu.path}` }));
|
2021-12-12 15:46:02 +08:00
|
|
|
}
|
2021-12-20 12:32:33 +08:00
|
|
|
});
|
2021-12-12 15:46:02 +08:00
|
|
|
}
|
2022-02-28 18:28:14 +08:00
|
|
|
return newMenuRouters;
|
|
|
|
});
|
|
|
|
|
2022-03-24 01:37:51 +08:00
|
|
|
const appendNewRoute = () => {
|
|
|
|
const {
|
|
|
|
path,
|
|
|
|
meta: { title },
|
|
|
|
name,
|
|
|
|
} = route;
|
|
|
|
tabsRouterStore.appendTabRouterList({ path, title: title as string, name, isAlive: true });
|
|
|
|
};
|
|
|
|
|
2022-04-06 16:56:15 +08:00
|
|
|
const getTabRouterListCache = () => {
|
|
|
|
tabsRouterStore.initTabRouterList(JSON.parse(localStorage.getItem('tabRouterList')));
|
|
|
|
};
|
|
|
|
const setTabRouterListCache = () => {
|
|
|
|
const { tabRouters } = tabsRouterStore;
|
|
|
|
localStorage.setItem('tabRouterList', JSON.stringify(tabRouters));
|
|
|
|
};
|
|
|
|
|
2022-03-24 01:37:51 +08:00
|
|
|
onMounted(() => {
|
|
|
|
appendNewRoute();
|
|
|
|
});
|
|
|
|
|
2022-04-06 16:56:15 +08:00
|
|
|
// 如果不需要持久化标签页可以注释掉以下的 onMounted 和 onBeforeUnmount 的内容
|
|
|
|
onMounted(() => {
|
|
|
|
if (localStorage.getItem('tabRouterList')) getTabRouterListCache();
|
|
|
|
window.addEventListener('beforeunload', setTabRouterListCache);
|
|
|
|
});
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
window.removeEventListener('beforeunload', setTabRouterListCache);
|
|
|
|
});
|
|
|
|
|
2022-03-24 01:37:51 +08:00
|
|
|
watch(
|
|
|
|
() => route.path,
|
|
|
|
() => {
|
|
|
|
appendNewRoute();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleRemove = ({ value: path, index }) => {
|
|
|
|
const { tabRouters } = tabsRouterStore;
|
|
|
|
const nextRouter = tabRouters[index + 1] || tabRouters[index - 1];
|
|
|
|
|
|
|
|
tabsRouterStore.subtractCurrentTabRouter({ path, routeIdx: index });
|
|
|
|
if (path === route.path) {
|
|
|
|
router.push(nextRouter.path);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const handleChangeCurrentTab = (path: string) => {
|
|
|
|
router.push(path);
|
|
|
|
};
|
|
|
|
const handleRefresh = (currentPath: string, routeIdx: number) => {
|
|
|
|
tabsRouterStore.toggleTabRouterAlive(routeIdx);
|
|
|
|
nextTick(() => {
|
|
|
|
tabsRouterStore.toggleTabRouterAlive(routeIdx);
|
|
|
|
router.replace({ path: currentPath });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
const handleCloseAhead = (path: string, routeIdx: number) => {
|
|
|
|
tabsRouterStore.subtractTabRouterAhead({ path, routeIdx });
|
|
|
|
};
|
|
|
|
const handleCloseBehind = (path: string, routeIdx: number) => {
|
|
|
|
tabsRouterStore.subtractTabRouterBehind({ path, routeIdx });
|
|
|
|
};
|
|
|
|
const handleCloseOther = (path: string, routeIdx: number) => {
|
|
|
|
tabsRouterStore.subtractTabRouterOther({ path, routeIdx });
|
|
|
|
};
|
|
|
|
|
2022-02-28 18:28:14 +08:00
|
|
|
const renderSidebar = () => {
|
2021-12-12 01:04:13 +08:00
|
|
|
return (
|
2022-02-28 18:28:14 +08:00
|
|
|
settingStore.showSidebar && (
|
2022-03-24 01:37:51 +08:00
|
|
|
<LayoutSideNav
|
2022-02-28 18:28:14 +08:00
|
|
|
showLogo={settingStore.showSidebarLogo}
|
|
|
|
layout={settingStore.layout}
|
|
|
|
isFixed={settingStore.isSidebarFixed}
|
|
|
|
menu={sideMenu.value}
|
|
|
|
theme={settingStore.displayMode}
|
|
|
|
isCompact={settingStore.isSidebarCompact}
|
2021-12-12 01:04:13 +08:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
);
|
2022-02-28 18:28:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const renderHeader = () => {
|
2021-12-12 01:04:13 +08:00
|
|
|
return (
|
2022-02-28 18:28:14 +08:00
|
|
|
settingStore.showHeader && (
|
2022-03-24 01:37:51 +08:00
|
|
|
<LayoutHeader
|
2022-02-28 18:28:14 +08:00
|
|
|
showLogo={settingStore.showHeaderLogo}
|
|
|
|
theme={settingStore.displayMode}
|
|
|
|
layout={settingStore.layout}
|
|
|
|
isFixed={settingStore.isHeaderFixed}
|
|
|
|
menu={headerMenu.value}
|
|
|
|
isCompact={settingStore.isSidebarCompact}
|
2021-12-12 01:04:13 +08:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
);
|
2022-02-28 18:28:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const renderFooter = () => {
|
|
|
|
return (
|
|
|
|
<t-footer class={`${prefix}-footer-layout`}>
|
2022-03-24 01:37:51 +08:00
|
|
|
<LayoutFooter />
|
2022-02-28 18:28:14 +08:00
|
|
|
</t-footer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderContent = () => {
|
2022-03-24 01:37:51 +08:00
|
|
|
const { showBreadcrumb, showFooter, isUseTabsRouter } = settingStore;
|
|
|
|
const { tabRouters } = tabsRouterStore;
|
2021-12-12 01:04:13 +08:00
|
|
|
return (
|
2021-12-22 12:07:29 +08:00
|
|
|
<t-layout class={[`${prefix}-layout`]}>
|
2022-03-24 01:37:51 +08:00
|
|
|
{isUseTabsRouter && (
|
|
|
|
<t-tabs
|
|
|
|
theme="card"
|
|
|
|
class={`${prefix}-layout-tabs-nav`}
|
|
|
|
value={route.path}
|
|
|
|
onChange={handleChangeCurrentTab}
|
2022-04-06 16:56:15 +08:00
|
|
|
style={{ width: '100%', position: 'sticky', top: 0 }}
|
2022-03-24 01:37:51 +08:00
|
|
|
onRemove={handleRemove}
|
|
|
|
>
|
2022-03-25 01:05:01 +08:00
|
|
|
{tabRouters.map((router: TRouterInfo, idx: number) => (
|
2022-03-24 01:37:51 +08:00
|
|
|
<t-tab-panel
|
|
|
|
value={router.path}
|
|
|
|
key={`${router.path}_${idx}`}
|
|
|
|
label={
|
|
|
|
<t-dropdown
|
|
|
|
trigger="context-menu"
|
|
|
|
minColumnWidth={128}
|
|
|
|
popupProps={{ overlayClassName: 'router-tabs-dropdown' }}
|
|
|
|
v-slots={{
|
2022-03-25 01:05:01 +08:00
|
|
|
dropdown: () =>
|
|
|
|
router.path === route.path ? (
|
|
|
|
<t-dropdown-menu>
|
|
|
|
<t-dropdown-item onClick={() => handleRefresh(router.path, idx)}>
|
|
|
|
<t-icon name="refresh" />
|
|
|
|
刷新
|
2022-03-24 01:37:51 +08:00
|
|
|
</t-dropdown-item>
|
2022-03-25 01:05:01 +08:00
|
|
|
{idx > 0 && (
|
|
|
|
<t-dropdown-item onClick={() => handleCloseAhead(router.path, idx)}>
|
|
|
|
<t-icon name="arrow-left" />
|
|
|
|
关闭左侧
|
|
|
|
</t-dropdown-item>
|
|
|
|
)}
|
|
|
|
{idx < tabRouters.length - 1 && (
|
|
|
|
<t-dropdown-item onClick={() => handleCloseBehind(router.path, idx)}>
|
|
|
|
<t-icon name="arrow-right" />
|
|
|
|
关闭右侧
|
|
|
|
</t-dropdown-item>
|
|
|
|
)}
|
|
|
|
<t-dropdown-item onClick={() => handleCloseOther(router.path, idx)}>
|
|
|
|
<t-icon name="close-circle" />
|
|
|
|
关闭其它
|
2022-03-24 01:37:51 +08:00
|
|
|
</t-dropdown-item>
|
2022-03-25 01:05:01 +08:00
|
|
|
</t-dropdown-menu>
|
|
|
|
) : null,
|
2022-03-24 01:37:51 +08:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{!router.isHome ? router.title : <t-icon name="home" />}
|
|
|
|
</t-dropdown>
|
|
|
|
}
|
|
|
|
removable={!router.isHome}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</t-tabs>
|
|
|
|
)}
|
2021-12-22 12:07:29 +08:00
|
|
|
<t-content class={`${prefix}-content-layout`}>
|
2022-03-24 01:37:51 +08:00
|
|
|
{showBreadcrumb && <LayoutBreadcrumb />}
|
|
|
|
<LayoutContent />
|
2021-12-12 01:04:13 +08:00
|
|
|
</t-content>
|
2022-02-28 18:28:14 +08:00
|
|
|
{showFooter && renderFooter()}
|
2021-12-12 01:04:13 +08:00
|
|
|
</t-layout>
|
|
|
|
);
|
2022-02-28 18:28:14 +08:00
|
|
|
};
|
2021-12-12 01:04:13 +08:00
|
|
|
|
2022-02-28 18:28:14 +08:00
|
|
|
return {
|
|
|
|
setting,
|
|
|
|
mainLayoutCls,
|
|
|
|
renderSidebar,
|
|
|
|
renderHeader,
|
|
|
|
renderContent,
|
|
|
|
};
|
2021-12-12 01:04:13 +08:00
|
|
|
},
|
|
|
|
render() {
|
|
|
|
const { layout } = this.setting;
|
|
|
|
const header = this.renderHeader();
|
|
|
|
const sidebar = this.renderSidebar();
|
|
|
|
const content = this.renderContent();
|
|
|
|
return (
|
2021-12-24 11:24:14 +08:00
|
|
|
<div>
|
2021-12-12 01:04:13 +08:00
|
|
|
{layout === 'side' ? (
|
|
|
|
<t-layout class={this.mainLayoutCls} key="side">
|
2021-12-12 15:46:02 +08:00
|
|
|
<t-aside>{sidebar}</t-aside>
|
2021-12-12 01:04:13 +08:00
|
|
|
<t-layout>{[header, content]}</t-layout>
|
|
|
|
</t-layout>
|
|
|
|
) : (
|
|
|
|
<t-layout key="no-side">
|
|
|
|
{header}
|
|
|
|
<t-layout class={this.mainLayoutCls}>{[sidebar, content]}</t-layout>
|
|
|
|
</t-layout>
|
|
|
|
)}
|
2022-03-24 01:37:51 +08:00
|
|
|
<Setting />
|
2021-12-12 01:04:13 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|