You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
268 lines
8.8 KiB
268 lines
8.8 KiB
|
7 months ago
|
import { defineStore } from 'pinia';
|
||
|
|
import router, { constantRoutes, dynamicRoutes } from '@/router';
|
||
|
|
import store from '@/store';
|
||
|
|
import { getRouters } from '@/api/menu';
|
||
|
|
import auth from '@/plugins/auth';
|
||
|
|
import { RouteRecordRaw } from 'vue-router';
|
||
|
|
import Layout from '@/layout/index.vue';
|
||
|
|
import ParentView from '@/components/ParentView/index.vue';
|
||
|
|
import InnerLink from '@/layout/components/InnerLink/index.vue';
|
||
|
|
import { ref } from 'vue';
|
||
|
|
import { createCustomNameComponent } from '@/utils/createCustomNameComponent';
|
||
|
|
|
||
|
|
// 匹配views里面所有的.vue文件
|
||
|
|
const modules = import.meta.glob('./../../views/**/*.vue');
|
||
|
|
/** 系统分类选项(顺序:首页、执法监督码、后台管理) */
|
||
|
|
export interface SystemCodeOption {
|
||
|
|
label: string;
|
||
|
|
value: string;
|
||
|
|
icon: string;
|
||
|
|
/** 当设置了 iframeUrl 时,切换到该选项会以全屏 iframe 展示,而非加载菜单路由 */
|
||
|
|
iframeUrl?: string;
|
||
|
|
}
|
||
|
|
export const systemCodeOptions: SystemCodeOption[] = [
|
||
|
|
{ label: '首页', value: 'planindex', icon: 'HomeFilled', iframeUrl: '/enforcement-code-v1/statistical/home.html' },
|
||
|
|
{ label: '执法监督码', value: 'supervisionCode', icon: 'Document' },
|
||
|
|
{ label: '后台管理', value: 'authService', icon: 'Setting' }
|
||
|
|
];
|
||
|
|
|
||
|
|
export const usePermissionStore = defineStore('permission', () => {
|
||
|
|
const routes = ref<RouteRecordRaw[]>([]);
|
||
|
|
const addRoutes = ref<RouteRecordRaw[]>([]);
|
||
|
|
const defaultRoutes = ref<RouteRecordRaw[]>([]);
|
||
|
|
const topbarRouters = ref<RouteRecordRaw[]>([]);
|
||
|
|
const sidebarRouters = ref<RouteRecordRaw[]>([]);
|
||
|
|
/** sessionStorage key */
|
||
|
|
const SYSTEM_CODE_KEY = 'currentSystemCode';
|
||
|
|
/** 当前选中的系统编码(从 sessionStorage 恢复,默认 planindex) */
|
||
|
|
const savedCode = sessionStorage.getItem(SYSTEM_CODE_KEY) || 'planindex';
|
||
|
|
const savedOption = systemCodeOptions.find((o) => o.value === savedCode);
|
||
|
|
const currentSystemCode = ref<string>(savedCode);
|
||
|
|
/** 当前是否处于 iframe 全屏模式 */
|
||
|
|
const iframeMode = ref<boolean>(savedOption?.iframeUrl ? true : false);
|
||
|
|
/** 当前 iframe 地址 */
|
||
|
|
const iframeUrl = ref<string>(savedOption?.iframeUrl || '');
|
||
|
|
|
||
|
|
const getRoutes = (): RouteRecordRaw[] => {
|
||
|
|
return routes.value as RouteRecordRaw[];
|
||
|
|
};
|
||
|
|
const getDefaultRoutes = (): RouteRecordRaw[] => {
|
||
|
|
return defaultRoutes.value as RouteRecordRaw[];
|
||
|
|
};
|
||
|
|
const getSidebarRoutes = (): RouteRecordRaw[] => {
|
||
|
|
return sidebarRouters.value as RouteRecordRaw[];
|
||
|
|
};
|
||
|
|
const getTopbarRoutes = (): RouteRecordRaw[] => {
|
||
|
|
return topbarRouters.value as RouteRecordRaw[];
|
||
|
|
};
|
||
|
|
|
||
|
|
const setRoutes = (newRoutes: RouteRecordRaw[]): void => {
|
||
|
|
addRoutes.value = newRoutes;
|
||
|
|
routes.value = constantRoutes.concat(newRoutes);
|
||
|
|
};
|
||
|
|
const setDefaultRoutes = (routes: RouteRecordRaw[]): void => {
|
||
|
|
defaultRoutes.value = constantRoutes.concat(routes);
|
||
|
|
};
|
||
|
|
const setTopbarRoutes = (routes: RouteRecordRaw[]): void => {
|
||
|
|
topbarRouters.value = routes;
|
||
|
|
};
|
||
|
|
const setSidebarRouters = (routes: RouteRecordRaw[]): void => {
|
||
|
|
sidebarRouters.value = routes;
|
||
|
|
};
|
||
|
|
const generateRoutes = async (systemCode?: string): Promise<RouteRecordRaw[]> => {
|
||
|
|
const res = await getRouters(systemCode || currentSystemCode.value);
|
||
|
|
const { data } = res;
|
||
|
|
const sdata = JSON.parse(JSON.stringify(data));
|
||
|
|
const rdata = JSON.parse(JSON.stringify(data));
|
||
|
|
const defaultData = JSON.parse(JSON.stringify(data));
|
||
|
|
const sidebarRoutes = filterAsyncRouter(sdata);
|
||
|
|
const rewriteRoutes = filterAsyncRouter(rdata, undefined, true);
|
||
|
|
const defaultRoutes = filterAsyncRouter(defaultData);
|
||
|
|
const asyncRoutes = filterDynamicRoutes(dynamicRoutes);
|
||
|
|
asyncRoutes.forEach((route) => {
|
||
|
|
router.addRoute(route);
|
||
|
|
});
|
||
|
|
setRoutes(rewriteRoutes);
|
||
|
|
setSidebarRouters(constantRoutes.concat(sidebarRoutes));
|
||
|
|
setDefaultRoutes(sidebarRoutes);
|
||
|
|
setTopbarRoutes(defaultRoutes);
|
||
|
|
// 路由name重复检查
|
||
|
|
duplicateRouteChecker(asyncRoutes, sidebarRoutes);
|
||
|
|
return new Promise<RouteRecordRaw[]>((resolve) => resolve(rewriteRoutes));
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 遍历后台传来的路由字符串,转换为组件对象
|
||
|
|
* @param asyncRouterMap 后台传来的路由字符串
|
||
|
|
* @param lastRouter 上一级路由
|
||
|
|
* @param type 是否是重写路由
|
||
|
|
*/
|
||
|
|
const filterAsyncRouter = (asyncRouterMap: RouteRecordRaw[], lastRouter?: RouteRecordRaw, type = false): RouteRecordRaw[] => {
|
||
|
|
return asyncRouterMap.filter((route) => {
|
||
|
|
if (type && route.children) {
|
||
|
|
route.children = filterChildren(route.children, undefined);
|
||
|
|
}
|
||
|
|
// Layout ParentView 组件特殊处理
|
||
|
|
if (route.component?.toString() === 'Layout') {
|
||
|
|
route.component = Layout;
|
||
|
|
} else if (route.component?.toString() === 'ParentView') {
|
||
|
|
route.component = ParentView;
|
||
|
|
} else if (route.component?.toString() === 'InnerLink') {
|
||
|
|
route.component = InnerLink;
|
||
|
|
} else {
|
||
|
|
route.component = loadView(route.component, route.name as string);
|
||
|
|
}
|
||
|
|
if (route.children != null && route.children && route.children.length) {
|
||
|
|
route.children = filterAsyncRouter(route.children, route, type);
|
||
|
|
} else {
|
||
|
|
delete route.children;
|
||
|
|
delete route.redirect;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
};
|
||
|
|
const filterChildren = (childrenMap: RouteRecordRaw[], lastRouter?: RouteRecordRaw): RouteRecordRaw[] => {
|
||
|
|
let children: RouteRecordRaw[] = [];
|
||
|
|
childrenMap.forEach((el) => {
|
||
|
|
el.path = lastRouter ? lastRouter.path + '/' + el.path : el.path;
|
||
|
|
if (el.children && el.children.length && el.component?.toString() === 'ParentView') {
|
||
|
|
children = children.concat(filterChildren(el.children, el));
|
||
|
|
} else {
|
||
|
|
children.push(el);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
return children;
|
||
|
|
};
|
||
|
|
/**
|
||
|
|
* 切换菜单组:更新 systemCode,重新加载路由并刷新页面
|
||
|
|
*/
|
||
|
|
const switchSystemCode = async (systemCode: string) => {
|
||
|
|
currentSystemCode.value = systemCode;
|
||
|
|
sessionStorage.setItem(SYSTEM_CODE_KEY, systemCode);
|
||
|
|
const option = systemCodeOptions.find((o) => o.value === systemCode);
|
||
|
|
// 判断是否为 iframe 模式
|
||
|
|
if (option?.iframeUrl) {
|
||
|
|
iframeMode.value = true;
|
||
|
|
iframeUrl.value = option.iframeUrl;
|
||
|
|
// iframe 模式下清空侧边栏路由
|
||
|
|
setSidebarRouters([]);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
iframeMode.value = false;
|
||
|
|
iframeUrl.value = '';
|
||
|
|
// 移除之前动态添加的路由
|
||
|
|
addRoutes.value.forEach((route) => {
|
||
|
|
if (route.name) {
|
||
|
|
router.removeRoute(route.name);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
// 重新生成路由
|
||
|
|
const accessRoutes = await generateRoutes(systemCode);
|
||
|
|
accessRoutes.forEach((route) => {
|
||
|
|
if (!router.hasRoute(route.name!)) {
|
||
|
|
router.addRoute(route);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
return {
|
||
|
|
routes,
|
||
|
|
topbarRouters,
|
||
|
|
sidebarRouters,
|
||
|
|
defaultRoutes,
|
||
|
|
currentSystemCode,
|
||
|
|
iframeMode,
|
||
|
|
iframeUrl,
|
||
|
|
|
||
|
|
getRoutes,
|
||
|
|
getDefaultRoutes,
|
||
|
|
getSidebarRoutes,
|
||
|
|
getTopbarRoutes,
|
||
|
|
|
||
|
|
setRoutes,
|
||
|
|
generateRoutes,
|
||
|
|
setSidebarRouters,
|
||
|
|
switchSystemCode
|
||
|
|
};
|
||
|
|
});
|
||
|
|
|
||
|
|
// 动态路由遍历,验证是否具备权限
|
||
|
|
export const filterDynamicRoutes = (routes: RouteRecordRaw[]) => {
|
||
|
|
const res: RouteRecordRaw[] = [];
|
||
|
|
routes.forEach((route) => {
|
||
|
|
if (route.permissions) {
|
||
|
|
if (auth.hasPermiOr(route.permissions)) {
|
||
|
|
res.push(route);
|
||
|
|
}
|
||
|
|
} else if (route.roles) {
|
||
|
|
if (auth.hasRoleOr(route.roles)) {
|
||
|
|
res.push(route);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
return res;
|
||
|
|
};
|
||
|
|
|
||
|
|
export const loadView = (view: any, name: string) => {
|
||
|
|
let res;
|
||
|
|
for (const path in modules) {
|
||
|
|
const viewsIndex = path.indexOf('/views/');
|
||
|
|
let dir = path.substring(viewsIndex + 7);
|
||
|
|
dir = dir.substring(0, dir.lastIndexOf('.vue'));
|
||
|
|
if (dir === view) {
|
||
|
|
res = createCustomNameComponent(modules[path], { name });
|
||
|
|
return res;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return res;
|
||
|
|
};
|
||
|
|
|
||
|
|
// 非setup
|
||
|
|
export const usePermissionStoreHook = () => {
|
||
|
|
return usePermissionStore(store);
|
||
|
|
};
|
||
|
|
|
||
|
|
interface Route {
|
||
|
|
name?: string | symbol;
|
||
|
|
path: string;
|
||
|
|
children?: Route[];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 检查路由name是否重复
|
||
|
|
* @param localRoutes 本地路由
|
||
|
|
* @param routes 动态路由
|
||
|
|
*/
|
||
|
|
function duplicateRouteChecker(localRoutes: Route[], routes: Route[]) {
|
||
|
|
// 展平
|
||
|
|
function flatRoutes(routes: Route[]) {
|
||
|
|
const res: Route[] = [];
|
||
|
|
routes.forEach((route) => {
|
||
|
|
if (route.children) {
|
||
|
|
res.push(...flatRoutes(route.children));
|
||
|
|
} else {
|
||
|
|
res.push(route);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
return res;
|
||
|
|
}
|
||
|
|
|
||
|
|
const allRoutes = flatRoutes([...localRoutes, ...routes]);
|
||
|
|
|
||
|
|
const nameList: string[] = [];
|
||
|
|
allRoutes.forEach((route) => {
|
||
|
|
const name = route.name.toString();
|
||
|
|
if (name && nameList.includes(name)) {
|
||
|
|
const message = `路由名称: [${name}] 重复, 会造成 404`;
|
||
|
|
console.error(message);
|
||
|
|
ElNotification({
|
||
|
|
title: '路由名称重复',
|
||
|
|
message,
|
||
|
|
type: 'error'
|
||
|
|
});
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
nameList.push(route.name.toString());
|
||
|
|
});
|
||
|
|
}
|