ebike-plus-ui/src/router/route-output.ts

92 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-04-11 13:07:43 +08:00
import pinia from "@/store/index";
2024-04-12 00:31:21 +08:00
import router from "@/router/index.ts";
2024-04-13 15:54:07 +08:00
import { dynamicRoutes } from "@/router/route";
2024-04-11 13:07:43 +08:00
import { storeToRefs } from "pinia";
import { useUserInfoStore } from "@/store/user-info";
2024-04-12 00:31:21 +08:00
import { useRoutesListStore } from "@/store/route-list";
2024-04-13 15:54:07 +08:00
import { deepClone, arrayFlattened } from "@/utils/index";
2024-04-11 13:07:43 +08:00
2024-04-12 00:31:21 +08:00
/**
2024-04-13 15:54:07 +08:00
*
* 1store
* 2
* 3 + addRoute动态添加路由KeepAlive支持二级路由缓存
* 4name存入store
2024-04-12 00:31:21 +08:00
*/
2024-04-12 13:02:43 +08:00
export async function initSetRouter() {
2024-04-12 00:31:21 +08:00
const store = useRoutesListStore(pinia);
// 根据角色权限过滤树
2024-04-12 13:02:43 +08:00
let filteredTree = filterByRole(dynamicRoutes[0].children);
await store.setRouteTree(filteredTree);
2024-04-12 00:31:21 +08:00
// 根据树生成一维路由数组
2024-04-12 13:02:43 +08:00
const flattenedArray = linearArray(filteredTree);
2024-04-12 00:31:21 +08:00
// 设置完整的路由,二维路由,顶层路由 + 二级的一维路由
const twoStoryTree = dynamicRoutes.map(item => {
if (flattenedArray.length > 0) item.redirect = flattenedArray[0].path;
item.children = flattenedArray;
return item;
});
// 动态添加路由
twoStoryTree.forEach((route: any) => router.addRoute(route));
// 根据一维路由设置缓存name
setCacheName(flattenedArray);
}
2024-04-13 15:54:07 +08:00
/**
* name存入store
* @param {array} flattenedArray
*/
2024-04-12 00:31:21 +08:00
export function setCacheName(flattenedArray: any) {
const store = useRoutesListStore(pinia);
const cacheName = flattenedArray.map((item: any) => item.name);
2024-04-18 00:24:07 +08:00
store.setRouteNames(cacheName); // 缓存路由name
2024-04-13 15:54:07 +08:00
store.setRouteList(flattenedArray); // 缓存路由
2024-04-12 00:31:21 +08:00
}
2024-04-13 15:54:07 +08:00
/**
*
* @param {array} tree
* @returns
*/
2024-04-12 00:31:21 +08:00
export function linearArray(tree: any) {
2024-04-13 15:54:07 +08:00
const nodes: any = deepClone(tree);
return arrayFlattened(nodes, "children");
2024-04-08 00:13:27 +08:00
}
2024-04-11 00:15:57 +08:00
2024-04-13 15:54:07 +08:00
/**
*
* @param {array} nodes
* @returns
*/
2024-04-11 00:15:57 +08:00
export const filterByRole = (nodes: any) => {
return nodes.filter((item: any) => {
if (item.meta && item.meta.roles) {
if (!roleBase(item.meta.roles)) return false;
}
if (item.children) item.children = filterByRole(item.children);
return true;
});
};
2024-04-13 15:54:07 +08:00
/**
*
* @param {array} roles
* @returns true是 false否
*/
2024-04-11 00:15:57 +08:00
export const roleBase = (roles: Array<string>) => {
2024-04-11 13:07:43 +08:00
const store = useUserInfoStore(pinia);
const { account } = storeToRefs(store);
return account.value.roles.some((item: string) => roles.includes(item));
2024-04-11 00:15:57 +08:00
};
2024-04-18 13:01:43 +08:00
/**
* store
* @param {object} to
* @returns true是 false否
*/
export const currentlyRoute = (to: any) => {
// const store = useRoutesListStore(pinia);
// const { routeList, currentRoute, tagsList } = storeToRefs(store);
console.log("当前路由", to);
};