Files
telegram-management-system/fix-api-routes.js
你的用户名 237c7802e5
Some checks failed
Deploy / deploy (push) Has been cancelled
Initial commit: Telegram Management System
Full-stack web application for Telegram management
- Frontend: Vue 3 + Vben Admin
- Backend: NestJS
- Features: User management, group broadcast, statistics

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-04 15:37:50 +08:00

264 lines
7.4 KiB
JavaScript
Raw Permalink 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.

#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
console.log('🔧 修复后端API路由问题...\n');
// 1. 检查TgAccountRouter.js文件
const routerPath = '/Users/hahaha/telegram-management-system/backend/src/routers/TgAccountRouter.js';
if (!fs.existsSync(routerPath)) {
console.error('❌ TgAccountRouter.js 文件不存在');
process.exit(1);
}
console.log('✅ 找到 TgAccountRouter.js 文件');
// 2. 读取并检查路由文件内容
const routerContent = fs.readFileSync(routerPath, 'utf8');
// 检查路由路径配置
if (routerContent.includes('this.path="/tgAccount"')) {
console.log('✅ 路由路径配置正确: /tgAccount');
} else {
console.log('⚠️ 路由路径可能有问题');
}
// 检查list接口
if (routerContent.includes('path: this.path+"/list"')) {
console.log('✅ list接口路径配置正确');
} else {
console.log('❌ 缺少list接口配置');
}
// 3. 创建测试API脚本
const testApiScript = `#!/usr/bin/env node
const mysql = require('mysql2/promise');
async function createMockApi() {
const connection = await mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: '',
database: 'tg_manage'
});
try {
// 查询TG账号数据
const [rows] = await connection.execute(
'SELECT id, phone, firstname, lastname, status, usageId FROM accounts ORDER BY id DESC LIMIT 20'
);
console.log('📊 TG账号数据样例:');
console.log(JSON.stringify(rows.slice(0, 5), null, 2));
// 统计数据
const [stats] = await connection.execute(
\`SELECT
COUNT(*) as total,
SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as active,
SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) as inactive,
SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as banned
FROM accounts\`
);
console.log('📈 账号统计:');
console.log(JSON.stringify(stats[0], null, 2));
// 用途统计
const [usages] = await connection.execute(
'SELECT usageId, COUNT(*) as count FROM accounts GROUP BY usageId'
);
console.log('📋 用途分布:');
console.log(JSON.stringify(usages, null, 2));
} catch (error) {
console.error('❌ 数据库查询失败:', error.message);
} finally {
await connection.end();
}
}
if (require.main === module) {
createMockApi().catch(console.error);
}
module.exports = { createMockApi };
`;
fs.writeFileSync('/Users/hahaha/telegram-management-system/test-api-data.js', testApiScript);
console.log('✅ 创建API测试脚本: test-api-data.js');
// 4. 创建临时修复方案
const fixContent = `
// 临时修复方案:为前端创建模拟数据接口
const express = require('express');
const cors = require('cors');
const mysql = require('mysql2/promise');
const app = express();
app.use(cors());
app.use(express.json());
// 数据库连接配置
const dbConfig = {
host: '127.0.0.1',
user: 'root',
password: '',
database: 'tg_manage'
};
// TG账号列表接口
app.post('/api/tgAccount/list', async (req, res) => {
try {
const connection = await mysql.createConnection(dbConfig);
const page = req.body.page || 1;
const size = req.body.size || 10;
const offset = (page - 1) * size;
// 查询总数
const [countResult] = await connection.execute(
'SELECT COUNT(*) as total FROM accounts'
);
const total = countResult[0].total;
// 查询数据
const [rows] = await connection.execute(
\`SELECT
id, phone, firstname, lastname, status, usageId,
createdAt, updatedAt, lastOnline
FROM accounts
ORDER BY id DESC
LIMIT ? OFFSET ?\`,
[size, offset]
);
// 统计数据
const [stats] = await connection.execute(
\`SELECT
COUNT(*) as total,
SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as active,
SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) as inactive,
SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as banned
FROM accounts\`
);
await connection.end();
res.json({
success: true,
code: 200,
data: {
list: rows,
total: total,
page: page,
size: size,
totalPages: Math.ceil(total / size),
stats: stats[0]
},
msg: '查询成功'
});
} catch (error) {
console.error('API错误:', error);
res.status(500).json({
success: false,
code: 500,
data: null,
msg: '服务器错误: ' + error.message
});
}
});
// 账号用途列表接口
app.post('/api/tgAccount/usageList', async (req, res) => {
try {
const connection = await mysql.createConnection(dbConfig);
const [rows] = await connection.execute(
\`SELECT usageId, COUNT(*) as count,
CASE usageId
WHEN 1 THEN '营销推广'
WHEN 2 THEN '客服咨询'
WHEN 3 THEN '群组管理'
WHEN 4 THEN '自动回复'
WHEN 5 THEN '数据采集'
ELSE '未分类'
END as usageName
FROM accounts
GROUP BY usageId
ORDER BY count DESC\`
);
await connection.end();
res.json({
success: true,
code: 200,
data: {
list: rows,
total: rows.length
},
msg: '查询成功'
});
} catch (error) {
console.error('API错误:', error);
res.status(500).json({
success: false,
code: 500,
data: null,
msg: '服务器错误: ' + error.message
});
}
});
// 启动服务
const PORT = 3002;
app.listen(PORT, () => {
console.log(\`🚀 临时API服务启动成功端口: \${PORT}\`);
console.log(\`📡 TG账号列表: POST http://localhost:\${PORT}/api/tgAccount/list\`);
console.log(\`📊 账号用途统计: POST http://localhost:\${PORT}/api/tgAccount/usageList\`);
});
`;
fs.writeFileSync('/Users/hahaha/telegram-management-system/temp-api-server.js', fixContent);
console.log('✅ 创建临时API服务: temp-api-server.js');
console.log(`
🎯 修复步骤:
1. 问题诊断:
❌ 后端 /tgAccount/list 路由返回 404
✅ 数据库有 2909 条 TG 账号数据
❌ 前端无法获取数据显示"暂无数据"
2. 立即修复方案:
📡 启动临时API服务推荐
cd /Users/hahaha/telegram-management-system
npm install mysql2 express cors
node temp-api-server.js
3. 前端配置修改:
📝 修改前端API基础URL指向临时服务
🔄 重新测试页面数据加载
4. 长期修复方案:
🔧 修复后端路由配置问题
🧪 确保路由正确注册和认证
📊 完善数据返回格式
📊 数据验证:
• MySQL表: accounts (2909条记录)
• 状态分布: 全部为活跃状态
• 临时API将提供正确的数据格式
🚀 现在执行临时修复方案!
`);