Files
telegram-management-system/simple-api-server.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

235 lines
6.9 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.

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 && req.body.page) || 1;
const size = (req.body && req.body.size) || 10;
// 查询总数
const [countResult] = await connection.execute('SELECT COUNT(*) as total FROM accounts');
const total = countResult[0].total;
// 查询数据 - 使用字符串拼接避免参数问题
const offset = (page - 1) * size;
const [rows] = await connection.execute(
`SELECT id, phone, firstname, lastname, status, usageId, createdAt, updatedAt, lastOnline, isBan FROM accounts ORDER BY id DESC LIMIT ${size} OFFSET ${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 isBan = 1 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
});
}
});
// TG账号用途管理接口
app.post('/accountUsage/list', async (req, res) => {
try {
const connection = await mysql.createConnection(dbConfig);
const pageIndex = (req.body && req.body.pageIndex) || 1;
const pageSize = (req.body && req.body.pageSize) || 10;
const type = (req.body && req.body.type) || '';
let whereClause = '';
if (type) {
whereClause = `WHERE type LIKE '%${type}%'`;
}
// 查询总数
const [countResult] = await connection.execute(`SELECT COUNT(*) as total FROM account_usages ${whereClause}`);
const total = countResult[0].total;
// 查询数据
const offset = (pageIndex - 1) * pageSize;
const [rows] = await connection.execute(
`SELECT id, type, createdAt, updatedAt FROM account_usages ${whereClause} ORDER BY id DESC LIMIT ${pageSize} OFFSET ${offset}`
);
await connection.end();
res.json({
success: true,
code: '200',
message: '查询成功',
data: {
list: rows,
totalRow: total,
pageIndex: pageIndex,
pageSize: pageSize
}
});
} catch (error) {
console.error('账号用途API错误:', error);
res.status(500).json({
success: false,
code: '500',
message: '服务器错误: ' + error.message,
data: null
});
}
});
// 添加账号用途接口
app.post('/accountUsage/add', async (req, res) => {
try {
const connection = await mysql.createConnection(dbConfig);
const { type } = req.body;
const [result] = await connection.execute(
'INSERT INTO account_usages (type, createdAt, updatedAt) VALUES (?, NOW(), NOW())',
[type]
);
await connection.end();
res.json({
success: true,
code: '200',
message: '添加成功',
data: { id: result.insertId }
});
} catch (error) {
console.error('添加账号用途错误:', error);
res.status(500).json({
success: false,
code: '500',
message: '添加失败: ' + error.message,
data: null
});
}
});
// 更新账号用途接口
app.post('/accountUsage/update', async (req, res) => {
try {
const connection = await mysql.createConnection(dbConfig);
const { id, type } = req.body;
await connection.execute(
'UPDATE account_usages SET type = ?, updatedAt = NOW() WHERE id = ?',
[type, id]
);
await connection.end();
res.json({
success: true,
code: '200',
message: '更新成功',
data: null
});
} catch (error) {
console.error('更新账号用途错误:', error);
res.status(500).json({
success: false,
code: '500',
message: '更新失败: ' + error.message,
data: null
});
}
});
// 启动服务
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`);
console.log(`📋 账号用途管理: POST http://localhost:${PORT}/accountUsage/list`);
console.log(` 添加账号用途: POST http://localhost:${PORT}/accountUsage/add`);
console.log(`✏️ 更新账号用途: POST http://localhost:${PORT}/accountUsage/update`);
});