184 lines
6.1 KiB
JavaScript
184 lines
6.1 KiB
JavaScript
/**
|
||
* 菜单功能修复脚本
|
||
* 修复"启动收银台.bat"启动后菜单功能无法正常使用的问题
|
||
*/
|
||
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
|
||
console.log('=== 菜单功能修复工具 ===');
|
||
|
||
// 修复Layout.js中的菜单点击处理函数
|
||
function fixLayoutMenuClick() {
|
||
console.log('\n1. 修复Layout.js菜单点击处理函数...');
|
||
try {
|
||
const layoutPath = path.join(__dirname, 'src', 'renderer', 'components', 'Layout.js');
|
||
if (fs.existsSync(layoutPath)) {
|
||
let content = fs.readFileSync(layoutPath, 'utf8');
|
||
|
||
// 备份原文件
|
||
fs.writeFileSync(layoutPath + '.backup', content);
|
||
console.log(' ✅ 已备份原文件');
|
||
|
||
// 查找现有的handleMenuClick函数
|
||
const oldFunction = `const handleMenuClick = ({ key }) => {
|
||
try {
|
||
// 使用electronNavigate处理导航
|
||
electronNavigate(key, navigate);
|
||
} catch (error) {
|
||
console.error('菜单导航失败:', error);
|
||
}
|
||
};`;
|
||
|
||
// 新的增强版函数
|
||
const newFunction = `const handleMenuClick = ({ key }) => {
|
||
console.log('菜单点击:', key);
|
||
|
||
try {
|
||
// 首先尝试使用electronNavigate处理导航
|
||
if (typeof electronNavigate === 'function') {
|
||
electronNavigate(key, navigate);
|
||
console.log('通过electronNavigate导航成功');
|
||
return;
|
||
}
|
||
} catch (error) {
|
||
console.warn('electronNavigate导航失败:', error);
|
||
}
|
||
|
||
// 备用方案1: 直接使用React Router的navigate
|
||
try {
|
||
if (navigate && typeof navigate === 'function') {
|
||
navigate(key);
|
||
console.log('通过React Router导航成功');
|
||
return;
|
||
}
|
||
} catch (error) {
|
||
console.warn('React Router导航失败:', error);
|
||
}
|
||
|
||
// 备用方案2: 使用window.location.hash
|
||
try {
|
||
if (key.startsWith('/')) {
|
||
window.location.hash = key;
|
||
console.log('通过window.location.hash导航成功');
|
||
return;
|
||
}
|
||
} catch (error) {
|
||
console.warn('window.location.hash导航失败:', error);
|
||
}
|
||
|
||
// 最后的备选方案: 刷新页面
|
||
console.error('所有导航方案都失败了,将刷新页面');
|
||
window.location.reload();
|
||
};`;
|
||
|
||
// 替换函数
|
||
if (content.includes(oldFunction)) {
|
||
content = content.replace(oldFunction, newFunction);
|
||
fs.writeFileSync(layoutPath, content, 'utf8');
|
||
console.log(' ✅ Layout.js菜单点击处理函数已修复');
|
||
} else {
|
||
console.log(' ⚠️ 未找到预期的handleMenuClick函数,可能已被修改');
|
||
|
||
// 尝试更通用的替换
|
||
const genericPattern = /const handleMenuClick = \({ key }\) => {[\s\S]*?};/;
|
||
if (genericPattern.test(content)) {
|
||
content = content.replace(genericPattern, newFunction);
|
||
fs.writeFileSync(layoutPath, content, 'utf8');
|
||
console.log(' ✅ Layout.js菜单点击处理函数已通过通用模式修复');
|
||
} else {
|
||
console.log(' ❌ 无法定位handleMenuClick函数进行修复');
|
||
}
|
||
}
|
||
} else {
|
||
console.log(' ❌ Layout.js文件不存在');
|
||
}
|
||
} catch (error) {
|
||
console.error(' ❌ 修复Layout.js失败:', error);
|
||
}
|
||
}
|
||
|
||
// 修复App.js中的路由配置
|
||
function fixAppRouting() {
|
||
console.log('\n2. 检查App.js路由配置...');
|
||
try {
|
||
const appPath = path.join(__dirname, 'src', 'renderer', 'App.js');
|
||
if (fs.existsSync(appPath)) {
|
||
let content = fs.readFileSync(appPath, 'utf8');
|
||
|
||
// 检查是否使用HashRouter
|
||
if (content.includes('HashRouter as Router')) {
|
||
console.log(' ✅ 已正确使用HashRouter');
|
||
} else if (content.includes('BrowserRouter as Router')) {
|
||
console.log(' ⚠️ 使用了BrowserRouter,可能需要改为HashRouter');
|
||
} else {
|
||
console.log(' ⚠️ 未明确使用HashRouter或BrowserRouter');
|
||
}
|
||
|
||
// 检查路由配置
|
||
const routePattern = /<Route path="\/cashier" element={<Cashier \/>} \/>/;
|
||
if (routePattern.test(content)) {
|
||
console.log(' ✅ 收银台路由配置正确');
|
||
} else {
|
||
console.log(' ⚠️ 收银台路由配置可能存在问题');
|
||
}
|
||
} else {
|
||
console.log(' ❌ App.js文件不存在');
|
||
}
|
||
} catch (error) {
|
||
console.error(' ❌ 检查App.js失败:', error);
|
||
}
|
||
}
|
||
|
||
// 更新HTML模板,确保基础路径正确
|
||
function updateHtmlTemplate() {
|
||
console.log('\n3. 检查HTML模板...');
|
||
try {
|
||
const htmlPath = path.join(__dirname, 'src', 'renderer', 'index.html');
|
||
if (fs.existsSync(htmlPath)) {
|
||
let content = fs.readFileSync(htmlPath, 'utf8');
|
||
|
||
// 检查base标签
|
||
if (content.includes('<base href="./">')) {
|
||
console.log(' ✅ base标签已正确设置');
|
||
} else {
|
||
// 添加base标签
|
||
const headEndIndex = content.indexOf('</head>');
|
||
if (headEndIndex > 0) {
|
||
const baseTag = '\n <base href="./">\n';
|
||
content = content.slice(0, headEndIndex) + baseTag + content.slice(headEndIndex);
|
||
fs.writeFileSync(htmlPath, content, 'utf8');
|
||
console.log(' ✅ 已添加base标签');
|
||
} else {
|
||
console.log(' ⚠️ 无法定位head标签,未添加base标签');
|
||
}
|
||
}
|
||
} else {
|
||
console.log(' ❌ index.html文件不存在');
|
||
}
|
||
} catch (error) {
|
||
console.error(' ❌ 检查HTML模板失败:', error);
|
||
}
|
||
}
|
||
|
||
// 主函数
|
||
function main() {
|
||
console.log('开始修复菜单功能问题...\n');
|
||
|
||
fixLayoutMenuClick();
|
||
fixAppRouting();
|
||
updateHtmlTemplate();
|
||
|
||
console.log('\n=== 菜单功能修复完成 ===');
|
||
console.log('修复内容:');
|
||
console.log('1. 增强了Layout.js中的菜单点击处理函数,添加多重备选导航方案');
|
||
console.log('2. 确保使用HashRouter以兼容Electron环境');
|
||
console.log('3. 在HTML模板中添加了正确的base标签');
|
||
console.log('\n请重新构建并启动应用以使更改生效:');
|
||
console.log(' 1. npm run build');
|
||
console.log(' 2. npm start');
|
||
console.log('\n或者直接运行"启动收银台.bat"');
|
||
}
|
||
|
||
// 执行修复
|
||
main(); |