227 lines
7.9 KiB
JavaScript
227 lines
7.9 KiB
JavaScript
import { asyncRoutes, constantRoutes } from "@/router";
|
||
|
||
// 获取用户代理字符串
|
||
const userAgent = window.navigator.userAgent;
|
||
|
||
// 判断是否为移动设备
|
||
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent);
|
||
|
||
// 修复:更全面的路由过滤逻辑
|
||
function filterAsyncRoutes(routes, permissions, userRoles = []) {
|
||
const res = [];
|
||
|
||
// 定义需要客户角色才能访问的路由
|
||
const customerOnlyRoutes = [
|
||
"/product", "/overview", "/workOrderManagement",
|
||
"/unsubscribeManagement", "/informationPerfect",
|
||
"/rechargeManagement", "/invoiceManagement"
|
||
];
|
||
|
||
routes.forEach(route => {
|
||
// 创建路由副本
|
||
const tmpRoute = { ...route };
|
||
|
||
// 检查当前路由是否在权限列表中
|
||
const hasPermission = permissions.some(p => p.path === route.meta?.fullPath);
|
||
|
||
// 特殊处理:确保"全部产品"和"资源概览"这两个一级路由在客户角色下显示
|
||
const isCriticalRoute = route.path === "/product" || route.path === "/overview";
|
||
|
||
// 检查是否为仅客户可访问的路由
|
||
const isCustomerOnlyRoute = customerOnlyRoutes.includes(route.path);
|
||
|
||
// 如果路由需要客户角色,但用户不是客户,则跳过
|
||
if (isCustomerOnlyRoute && !userRoles.includes('客户')) {
|
||
return; // 跳过当前路由
|
||
}
|
||
|
||
// 如果当前路由有权限,则加入结果
|
||
if (hasPermission) {
|
||
res.push(tmpRoute);
|
||
}
|
||
// 如果是关键路由且用户是客户,也要加入结果
|
||
else if (isCriticalRoute && userRoles.includes('客户')) {
|
||
res.push(tmpRoute);
|
||
}
|
||
// 如果没有直接权限,但有子路由,递归处理子路由
|
||
else if (tmpRoute.children) {
|
||
const filteredChildren = filterAsyncRoutes(tmpRoute.children, permissions, userRoles);
|
||
if (filteredChildren.length > 0) {
|
||
tmpRoute.children = filteredChildren;
|
||
res.push(tmpRoute); // 即使父路由本身没有权限,只要有子路由有权限,也要保留父路由
|
||
}
|
||
}
|
||
// 如果当前路由既没有权限,也没有有权限的子路由,则不添加到结果中
|
||
});
|
||
return res;
|
||
}
|
||
|
||
// 新增:为普通用户添加订单管理和资源管理路由
|
||
function addUserRoutes(routes, userType, orgType, userRoles = []) {
|
||
console.log("addUserRoutes - userType:", userType, "orgType:", orgType, "userRoles:", userRoles);
|
||
|
||
const userRoutes = [];
|
||
|
||
// 如果是普通用户(org_type为2或userType为user),添加订单管理和资源管理路由
|
||
if (userType === 'user' || orgType == 2 || orgType == 3 ) {
|
||
const orderManagementRoute = routes.find(route => route.path === "/orderManagement");
|
||
const resourceManagementRoute = routes.find(route => route.path === "/resourceManagement");
|
||
|
||
if (orderManagementRoute) {
|
||
console.log("添加订单管理路由");
|
||
userRoutes.push(JSON.parse(JSON.stringify(orderManagementRoute))); // 深拷贝
|
||
}
|
||
|
||
if (resourceManagementRoute) {
|
||
console.log("添加资源管理路由");
|
||
userRoutes.push(JSON.parse(JSON.stringify(resourceManagementRoute))); // 深拷贝
|
||
}
|
||
}
|
||
|
||
// 新增:为所有用户添加五个新的客户菜单,但只有客户角色才能看到
|
||
const newCustomerRoutes = [
|
||
routes.find(route => route.path === "/unsubscribeManagement"),
|
||
routes.find(route => route.path === "/informationPerfect"),
|
||
routes.find(route => route.path === "/rechargeManagement"),
|
||
routes.find(route => route.path === "/invoiceManagement"),
|
||
routes.find(route => route.path === "/workOrderManagement")
|
||
|
||
].filter(route => {
|
||
// 过滤掉undefined,并且只有客户角色才能看到这些路由
|
||
return route && userRoles.includes('客户');
|
||
});
|
||
|
||
console.log("添加新的客户菜单路由:", newCustomerRoutes.map(r => r.path));
|
||
userRoutes.push(...newCustomerRoutes);
|
||
|
||
return userRoutes;
|
||
}
|
||
|
||
function filterRoutesMobile(routes) {
|
||
return routes.filter(route => {
|
||
if (route.children && route.children.length) {
|
||
route.children = filterRoutesMobile(route.children);
|
||
return route.children.length > 0;
|
||
}
|
||
if (route.meta?.isMobile || route.meta?.isMobile === true) {
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
});
|
||
}
|
||
|
||
function filterRoutesPc(routes) {
|
||
return routes.filter(route => {
|
||
if (route.children && route.children.length) {
|
||
route.children = filterRoutesPc(route.children);
|
||
return route.children.length > 0;
|
||
}
|
||
if (!route.meta?.isMobile || route.meta?.isMobile === false) {
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
});
|
||
}
|
||
|
||
const state = {
|
||
routes: [],
|
||
addRoutes: [],
|
||
users: []
|
||
};
|
||
|
||
const mutations = {
|
||
SET_ROUTES: (state, routes) => {
|
||
console.log("MUTATION SET_ROUTES - received routes:", routes);
|
||
state.addRoutes = routes;
|
||
sessionStorage.setItem("routes", JSON.stringify(routes));
|
||
state.routes = constantRoutes.concat(routes);
|
||
console.log("MUTATION SET_ROUTES - final state.routes:", state.routes);
|
||
},
|
||
SETUSERS: (state, user) => {
|
||
state.users = user;
|
||
}
|
||
};
|
||
|
||
const actions = {
|
||
generateRoutes({ commit, rootState }, params) {
|
||
console.log("ACTION generateRoutes - params:", params);
|
||
return new Promise((resolve) => {
|
||
let accessedRoutes;
|
||
|
||
// 从参数或sessionStorage中获取用户类型和组织类型
|
||
const userType = params.userType || sessionStorage.getItem('userType') || '';
|
||
const orgType = params.orgType || parseInt(sessionStorage.getItem('orgType')) || 0;
|
||
|
||
// 获取用户角色(从store或sessionStorage)
|
||
const userRoles = rootState.user.roles || JSON.parse(sessionStorage.getItem('roles') || '[]');
|
||
console.log("用户角色:", userRoles);
|
||
|
||
console.log("用户类型:", userType, "orgType:", orgType);
|
||
|
||
if (params.user && params.user.includes("admin") && orgType != 2&3) {
|
||
// 管理员:只显示超级管理员菜单
|
||
accessedRoutes = asyncRoutes.filter(item => item.path === '/superAdministrator');
|
||
} else {
|
||
const auths = params.auths ? JSON.parse(JSON.stringify(params.auths)) : [];
|
||
console.log("ACTION generateRoutes - auths:", auths);
|
||
|
||
if (auths.length) {
|
||
// 确保 auths 中的 path 与路由 meta.fullPath 匹配
|
||
const paths = auths.map((item) => {
|
||
return item.path;
|
||
});
|
||
console.log("ACTION generateRoutes - paths from auths:", paths);
|
||
|
||
if (paths.includes("")) {
|
||
// 如果权限列表包含空路径,认为用户有所有权限
|
||
accessedRoutes = asyncRoutes || [];
|
||
} else {
|
||
// 使用修复后的过滤函数,传入用户角色
|
||
accessedRoutes = filterAsyncRoutes(asyncRoutes, auths, userRoles);
|
||
}
|
||
} else {
|
||
// 如果没有权限列表,不显示任何动态路由
|
||
accessedRoutes = [];
|
||
}
|
||
|
||
// 新增:为普通用户添加订单管理和资源管理路由以及新的五个客户菜单
|
||
console.log("为用户添加特定路由");
|
||
const userSpecificRoutes = addUserRoutes(asyncRoutes, userType, orgType, userRoles);
|
||
|
||
// 确保不重复添加路由,同时检查角色权限
|
||
userSpecificRoutes.forEach(route => {
|
||
const isCustomerRoute = [
|
||
"/workOrderManagement", "/unsubscribeManagement", "/informationPerfect",
|
||
"/rechargeManagement", "/invoiceManagement"
|
||
].includes(route.path);
|
||
|
||
// 如果是客户路由但用户不是客户,则不添加
|
||
if (isCustomerRoute && !userRoles.includes('客户')) {
|
||
return;
|
||
}
|
||
|
||
if (!accessedRoutes.some(r => r.path === route.path)) {
|
||
accessedRoutes.push(route);
|
||
}
|
||
});
|
||
|
||
console.log("添加用户特定路由后的accessedRoutes:", accessedRoutes);
|
||
}
|
||
|
||
console.log("ACTION generateRoutes - calculated accessedRoutes:", accessedRoutes);
|
||
|
||
commit("SET_ROUTES", accessedRoutes);
|
||
resolve(accessedRoutes);
|
||
});
|
||
},
|
||
};
|
||
|
||
export default {
|
||
namespaced: true,
|
||
state,
|
||
mutations,
|
||
actions,
|
||
};
|