mirror of
https://github.com/sinodidi/---OS.git
synced 2026-03-23 23:10:28 +08:00
148 lines
4.0 KiB
JavaScript
148 lines
4.0 KiB
JavaScript
const express = require('express');
|
|
const cors = require('cors');
|
|
const bodyParser = require('body-parser');
|
|
const jwt = require('jsonwebtoken');
|
|
const bcrypt = require('bcrypt');
|
|
|
|
// 初始化Express应用
|
|
const app = express();
|
|
const PORT = process.env.PORT || 5000;
|
|
const JWT_SECRET = 'flyinox-wechat-backup-secret'; // 实际应用中应使用环境变量存储密钥
|
|
|
|
// 中间件
|
|
app.use(cors());
|
|
app.use(bodyParser.json());
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
// 简单的用户验证(实际应用中应使用数据库)
|
|
const users = [
|
|
{
|
|
id: 1,
|
|
username: 'admin',
|
|
password: '$2b$10$8KVH7I3NKGjaa2SYj1CMmuOFjBP5.U6H69H8y8fMHpgxBG.xzR.Ke', // admin
|
|
name: '管理员'
|
|
}
|
|
];
|
|
|
|
// 身份验证中间件
|
|
const authenticateToken = (req, res, next) => {
|
|
const authHeader = req.headers['authorization'];
|
|
const token = authHeader && authHeader.split(' ')[1];
|
|
|
|
if (!token) return res.status(401).json({ message: '未提供认证令牌' });
|
|
|
|
jwt.verify(token, JWT_SECRET, (err, user) => {
|
|
if (err) return res.status(403).json({ message: '无效或过期的令牌' });
|
|
req.user = user;
|
|
next();
|
|
});
|
|
};
|
|
|
|
// 路由定义
|
|
// 登录
|
|
app.post('/api/login', async (req, res) => {
|
|
const { username, password } = req.body;
|
|
|
|
const user = users.find(user => user.username === username);
|
|
if (!user) return res.status(401).json({ message: '用户名或密码错误' });
|
|
|
|
const validPassword = await bcrypt.compare(password, user.password);
|
|
if (!validPassword) return res.status(401).json({ message: '用户名或密码错误' });
|
|
|
|
const token = jwt.sign({ id: user.id, username: user.username }, JWT_SECRET, { expiresIn: '24h' });
|
|
|
|
res.json({
|
|
token,
|
|
user: {
|
|
id: user.id,
|
|
username: user.username,
|
|
name: user.name
|
|
}
|
|
});
|
|
});
|
|
|
|
// 获取仪表盘统计数据
|
|
app.get('/api/dashboard/stats', authenticateToken, (req, res) => {
|
|
// 在实际应用中,这些数据应从数据库获取
|
|
res.json({
|
|
totalChats: 142,
|
|
totalContacts: 237,
|
|
totalMessages: 12483,
|
|
lastBackup: '2023-08-15 14:30:22'
|
|
});
|
|
});
|
|
|
|
// 获取聊天列表
|
|
app.get('/api/chats', authenticateToken, (req, res) => {
|
|
// 在实际应用中,这些数据应从数据库获取
|
|
const chats = [
|
|
{
|
|
id: '1',
|
|
name: '张三',
|
|
avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
|
|
lastMessage: '晚上一起吃饭吗?',
|
|
lastTime: '14:23',
|
|
unread: 3,
|
|
isGroup: false,
|
|
},
|
|
{
|
|
id: '2',
|
|
name: '工作群',
|
|
avatar: '',
|
|
lastMessage: '李四: 明天的会议推迟到下午2点',
|
|
lastTime: '昨天',
|
|
unread: 0,
|
|
isGroup: true,
|
|
},
|
|
// 其他聊天记录...
|
|
];
|
|
|
|
res.json(chats);
|
|
});
|
|
|
|
// 获取聊天详情
|
|
app.get('/api/chats/:chatId', authenticateToken, (req, res) => {
|
|
const { chatId } = req.params;
|
|
|
|
// 在实际应用中,这些数据应从数据库获取
|
|
const chatInfo = {
|
|
id: chatId,
|
|
name: chatId === '1' ? '张三' : (chatId === '2' ? '工作群' : '聊天'),
|
|
avatar: chatId === '1' ? 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png' : '',
|
|
isGroup: chatId === '2' || chatId === '4',
|
|
memberCount: chatId === '2' ? 15 : (chatId === '4' ? 5 : 2),
|
|
createdAt: '2022-05-20',
|
|
};
|
|
|
|
res.json(chatInfo);
|
|
});
|
|
|
|
// 获取聊天消息
|
|
app.get('/api/chats/:chatId/messages', authenticateToken, (req, res) => {
|
|
const { chatId } = req.params;
|
|
|
|
// 在实际应用中,这些数据应从数据库获取
|
|
const today = new Date();
|
|
const yesterday = new Date(today);
|
|
yesterday.setDate(yesterday.getDate() - 1);
|
|
|
|
const messages = [
|
|
{
|
|
id: '1',
|
|
senderId: chatId === '1' ? '1' : 'user',
|
|
senderName: chatId === '1' ? '张三' : '我',
|
|
content: '你好,最近怎么样?',
|
|
type: 'text',
|
|
timestamp: '08:30',
|
|
date: today.toISOString().split('T')[0],
|
|
},
|
|
// 其他消息...
|
|
];
|
|
|
|
res.json(messages);
|
|
});
|
|
|
|
// 启动服务器
|
|
app.listen(PORT, () => {
|
|
console.log(`服务器运行在 http://localhost:${PORT}`);
|
|
});
|