minishouyin/test/check-navigation.js
2025-11-12 11:35:57 +08:00

111 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

console.log('=== 迷你收银台导航诊断工具 ===');
// 检查React Router版本和配置
function checkReactRouter() {
console.log('\n1. 检查React Router配置:');
try {
// 检查window.location
console.log(` 当前URL: ${window.location.pathname}`);
// 模拟导航测试
console.log(' 测试导航功能...');
// 检查是否有useNavigate钩子可用
if (typeof window.reactRouter !== 'undefined') {
console.log(' ✅ React Router已初始化');
} else {
console.log(' ⚠️ 无法直接检测React Router状态');
}
// 测试路由跳转
console.log(' 建议手动在控制台运行: window.location.href = "/cashier"');
} catch (error) {
console.error(' ❌ 检查React Router失败:', error);
}
}
// 检查API服务状态
function checkApiService() {
console.log('\n2. 检查API服务状态:');
try {
// 检查是否有全局API对象
if (window.__apiPort) {
console.log(` 当前API端口: ${window.__apiPort}`);
console.log(' ✅ API服务已初始化');
} else {
console.log(' ❌ API服务未初始化');
}
// 测试端口查找功能
if (window.__findAvailablePort) {
console.log(' 可调用window.__findAvailablePort()重新检测端口');
}
// 检查网络连接
console.log(' 测试网络连接...');
fetch(`http://localhost:3000/api`, {
method: 'GET',
timeout: 3000
})
.then(response => {
console.log(' ✅ 端口3000连接成功');
})
.catch(() => {
console.log(' ❌ 端口3000连接失败尝试端口3001...');
fetch(`http://localhost:3001/api`, {
method: 'GET',
timeout: 3000
})
.then(() => {
console.log(' ✅ 端口3001连接成功');
})
.catch(() => {
console.log(' ❌ 端口3001连接失败');
});
});
} catch (error) {
console.error(' ❌ 检查API服务失败:', error);
}
}
// 检查组件状态
function checkComponentStatus() {
console.log('\n3. 检查组件状态:');
try {
// 检查localStorage
const userInfo = localStorage.getItem('userInfo');
const token = localStorage.getItem('token');
console.log(` 用户信息: ${userInfo ? '存在' : '不存在'}`);
console.log(` Token: ${token ? '存在' : '不存在'}`);
// 检查是否有渲染错误
console.log(' 检查控制台是否有渲染错误');
// 提供手动测试方法
console.log('\n4. 手动测试方法:');
console.log(' - 清除缓存: localStorage.clear();');
console.log(' - 重新加载: window.location.reload();');
console.log(' - 切换端口: window.__switchApiPort(3001);');
} catch (error) {
console.error(' ❌ 检查组件状态失败:', error);
}
}
// 运行诊断
function runDiagnostics() {
console.log('开始诊断...');
checkReactRouter();
checkApiService();
checkComponentStatus();
console.log('\n诊断完成请查看以上结果。');
}
// 如果直接在控制台运行,立即执行
if (typeof process === 'undefined') {
runDiagnostics();
}
// 导出函数供其他地方使用
if (typeof module !== 'undefined') {
module.exports = { runDiagnostics, checkReactRouter, checkApiService, checkComponentStatus };
}