minishouyin/test/fix-navigation.js

125 lines
4.2 KiB
JavaScript
Raw Normal View History

2025-11-12 11:35:57 +08:00
console.log('=== 导航菜单修复工具 ===');
const fs = require('fs');
const path = require('path');
// 修复Layout.js中的菜单导航问题
function fixLayoutNavigation() {
console.log('\n1. 修复Layout.js菜单导航...');
const layoutPath = path.join(__dirname, 'src', 'renderer', 'components', 'Layout.js');
try {
// 读取Layout.js内容
let content = fs.readFileSync(layoutPath, 'utf8');
// 确保handleMenuClick函数正确实现
const oldHandleMenuClick = /function handleMenuClick\(\{ key \}\) \{[\s\S]*?navigate\(key\);[\s\S]*?\}/;
if (oldHandleMenuClick.test(content)) {
const newHandleMenuClick = `function handleMenuClick({ key }) {
console.log('菜单点击:', key);
try {
navigate(key);
console.log('导航成功到:', key);
} catch (error) {
console.error('导航失败:', error);
// 直接使用window.location作为备选方案
window.location.href = key;
}
}`;
content = content.replace(oldHandleMenuClick, newHandleMenuClick);
console.log('✅ 已增强菜单点击处理函数');
}
// 确保Menu组件配置正确
const oldMenuConfig = /<Menu[\s\S]*?selectedKeys={\[location\.pathname\]}/;
if (oldMenuConfig.test(content)) {
const newMenuConfig = '<Menu\n mode="inline"\n selectedKeys={[location.pathname]}\n items={menuItems}\n onClick={handleMenuClick}\n style={{ height: \'100%\', borderRight: 0 }}\n forceRender={true}\n autoOpenKeys={[]}';
content = content.replace(/<Menu[\s\S]*?style={{ height: '100%', borderRight: 0 }}>/, newMenuConfig);
console.log('✅ 已增强Menu组件配置');
}
// 备份并写入修复后的内容
fs.writeFileSync(layoutPath + '.backup', fs.readFileSync(layoutPath, 'utf8'));
fs.writeFileSync(layoutPath, content);
console.log('✅ Layout.js修复完成');
} catch (error) {
console.error('❌ 修复Layout.js失败:', error.message);
}
}
// 修复App.js中的路由配置
function fixAppRoutes() {
console.log('\n2. 修复App.js路由配置...');
const appPath = path.join(__dirname, 'src', 'renderer', 'App.js');
try {
// 确保路由配置正确
let content = fs.readFileSync(appPath, 'utf8');
// 增强错误处理和调试
const oldRenderTryBlock = /try \{[\s\S]*?return \([\s\S]*?<Routes>/;
if (oldRenderTryBlock.test(content)) {
const debugInfo = `// 添加路由调试
console.log('渲染路由配置');
window.__routesDebug = {
user: mockUser,
location: window.location.pathname,
timestamp: new Date().toISOString()
};`;
// 在return前添加调试信息
content = content.replace('return (\n <ConfigProvider locale={zhCN}>', `${debugInfo}\n return (\n <ConfigProvider locale={zhCN}>`);
console.log('✅ 已添加路由调试信息');
}
fs.writeFileSync(appPath + '.fix-backup', fs.readFileSync(appPath, 'utf8'));
fs.writeFileSync(appPath, content);
console.log('✅ App.js修复完成');
} catch (error) {
console.error('❌ 修复App.js失败:', error.message);
}
}
// 创建一个简单的启动脚本来测试修复
function createTestScript() {
try {
const testScriptContent = `
@echo off
chcp 65001
cd %~dp0
echo 测试导航菜单修复...
echo 请按F12打开开发者工具查看控制台输出
npm start
pause
`;
fs.writeFileSync(path.join(__dirname, 'test-navigation.bat'), testScriptContent);
console.log('✅ 已创建测试脚本: test-navigation.bat');
} catch (error) {
console.error('❌ 创建测试脚本失败:', error.message);
}
}
// 主函数
function runFix() {
console.log('开始修复导航菜单问题...');
fixLayoutNavigation();
fixAppRoutes();
createTestScript();
console.log('\n=== 修复完成 ===');
console.log('1. 已增强菜单点击处理函数,添加错误处理和备选导航方案');
console.log('2. 已优化Menu组件配置');
console.log('3. 已添加详细的调试日志');
console.log('\n请运行 test-navigation.bat 测试修复效果');
console.log('修复思路: 增强导航错误处理,添加备选方案,便于调试');
}
runFix();