物联网问题解决,只差最后测试完善

This commit is contained in:
xuqiuyun
2025-10-23 17:28:06 +08:00
parent 0249dfc5bb
commit ecccd025d1
72 changed files with 7372 additions and 653 deletions

View File

@@ -27,6 +27,13 @@ const usePermissionStore = defineStore('permission', {
},
setUserPermission(arr) {
this.userPermission = arr;
console.log('=== 权限store更新 ===', arr);
},
// 强制刷新权限数据
async refreshPermissions() {
console.log('=== 强制刷新权限数据 ===');
this.routeFlag = false; // 重置路由标志,强制重新生成路由
return this.generateRoutes();
},
generateRoutes() {
return new Promise((resolve) => {
@@ -36,13 +43,17 @@ const usePermissionStore = defineStore('permission', {
console.log('=== 权限路由生成 ===', { code, data });
const btnList = data.filter((i) => i.type === 2);
const permissionList = btnList.map((i) => i.authority);
const permissionList = btnList.map((i) => i.authority).filter(auth => auth); // 过滤掉空权限
console.log('=== 设置用户权限列表 ===', permissionList);
this.setUserPermission(permissionList);
let menuList = data.filter((i) => i.type !== 2);
menuList = menuList.map((item) => {
// 确保 routeUrl 存在且不为空
const routeUrl = item.routeUrl || item.pageUrl || '';
let routeUrl = item.routeUrl || item.pageUrl || '';
// 规范化路径
routeUrl = normalizeRoutePath(routeUrl);
return {
id: item.id,
@@ -65,6 +76,21 @@ const usePermissionStore = defineStore('permission', {
const sdata = JSON.parse(JSON.stringify(menuList));
console.log('=== 处理后的菜单列表 ===', menuList);
console.log('=== 路径检查 ===', menuList.map(item => ({ name: item.name, path: item.path })));
// 检查并修复双斜杠路径
const doubleSlashPaths = menuList.filter(item => item.path && item.path.includes('//'));
if (doubleSlashPaths.length > 0) {
console.error('=== 发现双斜杠路径 ===', doubleSlashPaths);
// 修复双斜杠路径
menuList.forEach(item => {
if (item.path && item.path.includes('//')) {
const originalPath = item.path;
item.path = item.path.replace(/\/+/g, '/');
console.warn('修复菜单路径:', originalPath, '->', item.path);
}
});
}
// eslint-disable-next-line no-use-before-define
const rewriteRoutes = filterAsyncRouter(menuList, false, true);
@@ -110,6 +136,62 @@ function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.toLowerCase().slice(1);
}
// 规范化路由路径
function normalizeRoutePath(path) {
if (!path || typeof path !== 'string') {
return '/';
}
// 移除首尾空格
path = path.trim();
// 移除重复的斜杠
path = path.replace(/\/+/g, '/');
// 确保路径以 "/" 开头
if (!path.startsWith('/')) {
path = '/' + path;
}
// 确保路径不为空
if (path === '') {
path = '/';
}
return path;
}
// 安全拼接路径
function joinPaths(parentPath, childPath) {
// 规范化父路径
parentPath = normalizeRoutePath(parentPath);
childPath = normalizeRoutePath(childPath);
// 如果父路径是根路径,直接返回子路径
if (parentPath === '/') {
return childPath;
}
// 如果子路径是根路径,直接返回父路径
if (childPath === '/') {
return parentPath;
}
// 确保父路径不以斜杠结尾,子路径不以斜杠开头
if (parentPath.endsWith('/')) {
parentPath = parentPath.slice(0, -1);
}
if (childPath.startsWith('/')) {
childPath = childPath.slice(1);
}
// 拼接路径
const joinedPath = parentPath + '/' + childPath;
// 再次规范化,确保没有双斜杠
return normalizeRoutePath(joinedPath);
}
// 遍历后台传来的路由字符串,转换为组件对象
// eslint-disable-next-line no-unused-vars
function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
@@ -154,7 +236,7 @@ function filterChildren(childrenMap, lastRouter = false) {
if (el.component === 'ParentView' && !lastRouter) {
el.children.forEach((c) => {
// eslint-disable-next-line no-param-reassign
c.path = `${el.path}/${c.path}`;
c.path = joinPaths(el.path, c.path);
if (c.children && c.children.length) {
children = children.concat(filterChildren(c.children, c));
return;
@@ -166,7 +248,7 @@ function filterChildren(childrenMap, lastRouter = false) {
}
if (lastRouter) {
// eslint-disable-next-line no-param-reassign
el.path = `${lastRouter.path}/${el.path}`;
el.path = joinPaths(lastRouter.path, el.path);
if (el.children && el.children.length) {
children = children.concat(filterChildren(el.children, el));
return;