Files
wireguard-dashboard-admin/src/layout/hooks/useNav.ts

156 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-02-15 23:16:15 +08:00
import { computed } from "vue";
import { getConfig } from "@/config";
2022-08-22 21:34:55 +08:00
import { useRouter } from "vue-router";
import { emitter } from "@/utils/mitt";
2022-02-15 23:16:15 +08:00
import { routeMetaType } from "../types";
2022-10-25 17:51:21 +08:00
import { useGlobal } from "@pureadmin/utils";
import { transformI18n } from "@/plugins/i18n";
import { router, remainingPaths } from "@/router";
import { useAppStoreHook } from "@/store/modules/app";
import { useUserStoreHook } from "@/store/modules/user";
import { useEpThemeStoreHook } from "@/store/modules/epTheme";
2022-02-15 23:16:15 +08:00
2022-04-18 11:15:46 +08:00
const errorInfo = "当前路由配置不正确,请检查配置";
2022-02-15 23:16:15 +08:00
export function useNav() {
const pureApp = useAppStoreHook();
2022-08-22 21:34:55 +08:00
const routers = useRouter().options.routes;
2022-10-25 17:51:21 +08:00
2022-08-22 21:34:55 +08:00
/** 用户名 */
2022-10-25 17:51:21 +08:00
const username = computed(() => {
return useUserStoreHook()?.username;
});
2022-02-15 23:16:15 +08:00
2022-08-22 21:34:55 +08:00
/** 设置国际化选中后的样式 */
2022-02-15 23:16:15 +08:00
const getDropdownItemStyle = computed(() => {
return (locale, t) => {
return {
background: locale === t ? useEpThemeStoreHook().epThemeColor : "",
color: locale === t ? "#f4f4f5" : "#000"
};
};
});
2022-08-22 21:34:55 +08:00
const getDropdownItemClass = computed(() => {
return (locale, t) => {
return locale === t ? "" : "dark:hover:!text-primary";
2022-08-22 21:34:55 +08:00
};
});
2022-04-18 11:15:46 +08:00
const avatarsStyle = computed(() => {
2022-10-25 17:51:21 +08:00
return username.value ? { marginRight: "10px" } : "";
2022-04-18 11:15:46 +08:00
});
2022-02-15 23:16:15 +08:00
const isCollapse = computed(() => {
return !pureApp.getSidebarStatus;
});
2022-08-22 21:34:55 +08:00
const device = computed(() => {
return pureApp.getDevice;
});
const { $storage, $config } = useGlobal<GlobalPropertiesApi>();
const layout = computed(() => {
return $storage?.layout?.layout;
});
const title = computed(() => {
return $config.Title;
});
/** 动态title */
2022-02-15 23:16:15 +08:00
function changeTitle(meta: routeMetaType) {
2022-03-14 19:46:29 +08:00
const Title = getConfig().Title;
2022-05-11 16:43:56 +08:00
if (Title) document.title = `${transformI18n(meta.title)} | ${Title}`;
else document.title = transformI18n(meta.title);
2022-02-15 23:16:15 +08:00
}
2022-08-22 21:34:55 +08:00
/** 退出登录 */
2022-02-15 23:16:15 +08:00
function logout() {
2022-10-25 17:51:21 +08:00
useUserStoreHook().logOut();
2022-02-15 23:16:15 +08:00
}
function backHome() {
router.push("/welcome");
}
function onPanel() {
emitter.emit("openPanel");
}
function toggleSideBar() {
pureApp.toggleSideBar();
}
function handleResize(menuRef) {
2022-08-22 21:34:55 +08:00
menuRef?.handleResize();
2022-02-15 23:16:15 +08:00
}
function resolvePath(route) {
2022-04-18 11:15:46 +08:00
if (!route.children) return console.error(errorInfo);
2022-02-15 23:16:15 +08:00
const httpReg = /^http(s?):\/\//;
const routeChildPath = route.children[0]?.path;
if (httpReg.test(routeChildPath)) {
return route.path + "/" + routeChildPath;
} else {
return routeChildPath;
}
}
function menuSelect(indexPath: string, routers): void {
2022-02-28 22:33:56 +08:00
if (isRemaining(indexPath)) return;
2022-02-15 23:16:15 +08:00
let parentPath = "";
const parentPathIndex = indexPath.lastIndexOf("/");
if (parentPathIndex > 0) {
parentPath = indexPath.slice(0, parentPathIndex);
}
2022-08-22 21:34:55 +08:00
/** 找到当前路由的信息 */
2022-02-15 23:16:15 +08:00
function findCurrentRoute(indexPath: string, routes) {
2022-04-18 11:15:46 +08:00
if (!routes) return console.error(errorInfo);
2022-02-15 23:16:15 +08:00
return routes.map(item => {
if (item.path === indexPath) {
if (item.redirect) {
findCurrentRoute(item.redirect, item.children);
} else {
2022-08-22 21:34:55 +08:00
/** 切换左侧菜单 通知标签页 */
2022-02-15 23:16:15 +08:00
emitter.emit("changLayoutRoute", {
indexPath,
parentPath
});
}
} else {
if (item.children) findCurrentRoute(indexPath, item.children);
}
});
}
findCurrentRoute(indexPath, routers);
}
2022-08-22 21:34:55 +08:00
/** 判断路径是否参与菜单 */
2022-02-28 22:33:56 +08:00
function isRemaining(path: string): boolean {
return remainingPaths.includes(path);
}
2022-02-15 23:16:15 +08:00
return {
2022-08-22 21:34:55 +08:00
title,
device,
layout,
2022-02-15 23:16:15 +08:00
logout,
2022-08-22 21:34:55 +08:00
routers,
$storage,
2022-02-15 23:16:15 +08:00
backHome,
onPanel,
changeTitle,
toggleSideBar,
menuSelect,
handleResize,
resolvePath,
isCollapse,
pureApp,
2022-04-10 21:22:05 +08:00
username,
2022-04-18 11:15:46 +08:00
avatarsStyle,
2022-08-22 21:34:55 +08:00
getDropdownItemStyle,
getDropdownItemClass
2022-02-15 23:16:15 +08:00
};
}