ebike-plus-ui/src/hooks/useRoutingMethod.ts

84 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-04-20 16:54:12 +08:00
import pinia from "@/store/index";
import { storeToRefs } from "pinia";
import { useRoutesConfigStore } from "@/store/modules/route-config";
import { findCategoryById, findPathOfParentNode } from "@/utils/tree-tools";
2024-04-20 16:54:12 +08:00
/**
* hooks
* @returns
*/
export const useRoutingMethod = () => {
/**
*
* @param {string} path path
2024-04-20 16:54:12 +08:00
* @returns undefined则表示未找到
*/
const findLinearArray = (path: string) => {
const routerStore = useRoutesConfigStore(pinia);
const { routeTree } = storeToRefs(routerStore);
return findCategoryById(routeTree.value, "path", path);
};
/**
*
* @param {string} path path
* @returns null
*/
const getAllParentRoute = (path: string) => {
const routerStore = useRoutesConfigStore(pinia);
const { routeTree } = storeToRefs(routerStore);
return findPathOfParentNode(routeTree.value, "path", path);
2024-04-20 16:54:12 +08:00
};
/**
*
* @param {string} key name
* @returns true存在 false不存在
*/
const hasRoute = (key: string) => {
const routerStore = useRoutesConfigStore(pinia);
const { routeList } = storeToRefs(routerStore);
return routeList.value.some((item: Menu.MenuOptions) => item.name == key);
};
2024-04-20 16:54:12 +08:00
/**
* tabs路由中查找路由
* @param {string} key name
* @returns undefined则表示未找到
*/
const findTagsList = (key: string) => {
const routerStore = useRoutesConfigStore(pinia);
2024-04-20 16:54:12 +08:00
const { tabsList } = storeToRefs(routerStore);
return tabsList.value.find((item: Menu.MenuOptions) => item.name == key);
};
/**
* url跳转
* @param {any} route
*/
const openExternalLinks = (route: any) => {
// 处理外链跳转
if (route.meta.link && !route.meta.iframe) {
window.open(route.meta.link as string, "_blank");
}
};
/**
* path必然带有"/:"/user/:id
* @param {string} path path
* @returns
*/
const isDynamicRoute = (path: string) => {
return path.includes("/:");
};
2024-04-20 16:54:12 +08:00
return {
findLinearArray,
getAllParentRoute,
findTagsList,
openExternalLinks,
isDynamicRoute,
hasRoute
2024-04-20 16:54:12 +08:00
};
};