2025-12-18 19:07:21 +08:00
|
|
|
|
import { Request, Response } from 'express';
|
|
|
|
|
|
import { SystemConfigModel } from '../models';
|
|
|
|
|
|
|
|
|
|
|
|
export class AdminController {
|
|
|
|
|
|
// 管理员登录
|
|
|
|
|
|
static async login(req: Request, res: Response) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { username, password } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
if (!username || !password) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '用户名和密码不能为空'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-21 01:56:54 +08:00
|
|
|
|
// 直接验证用户名和密码,不依赖数据库
|
|
|
|
|
|
// 初始管理员账号:admin / admin123
|
|
|
|
|
|
const isValid = username === 'admin' && password === 'admin123';
|
2025-12-18 19:07:21 +08:00
|
|
|
|
if (!isValid) {
|
|
|
|
|
|
return res.status(401).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '用户名或密码错误'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-21 01:56:54 +08:00
|
|
|
|
// 直接返回成功,不生成真实的JWT token
|
2025-12-18 19:07:21 +08:00
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
message: '登录成功',
|
|
|
|
|
|
data: {
|
|
|
|
|
|
username,
|
|
|
|
|
|
token: 'admin-token' // 简化处理
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
|
console.error('管理员登录失败:', error);
|
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: error.message || '登录失败'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取抽题配置
|
|
|
|
|
|
static async getQuizConfig(req: Request, res: Response) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const config = await SystemConfigModel.getQuizConfig();
|
|
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
data: config
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
|
console.error('获取抽题配置失败:', error);
|
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: error.message || '获取抽题配置失败'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新抽题配置
|
|
|
|
|
|
static async updateQuizConfig(req: Request, res: Response) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { singleRatio, multipleRatio, judgmentRatio, textRatio, totalScore } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
|
|
singleRatio,
|
|
|
|
|
|
multipleRatio,
|
|
|
|
|
|
judgmentRatio,
|
|
|
|
|
|
textRatio,
|
|
|
|
|
|
totalScore
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await SystemConfigModel.updateQuizConfig(config);
|
|
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
message: '抽题配置更新成功'
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
|
console.error('更新抽题配置失败:', error);
|
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: error.message || '更新抽题配置失败'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取统计数据
|
|
|
|
|
|
static async getStatistics(req: Request, res: Response) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { QuizModel } = await import('../models');
|
|
|
|
|
|
const statistics = await QuizModel.getStatistics();
|
|
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
data: statistics
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
|
console.error('获取统计数据失败:', error);
|
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: error.message || '获取统计数据失败'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-19 00:58:58 +08:00
|
|
|
|
// 获取活跃任务统计数据
|
|
|
|
|
|
static async getActiveTasksStats(req: Request, res: Response) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { ExamTaskModel } = await import('../models/examTask');
|
|
|
|
|
|
const stats = await ExamTaskModel.getActiveTasksWithStats();
|
|
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
data: stats
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
|
console.error('获取活跃任务统计数据失败:', error);
|
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: error.message || '获取活跃任务统计数据失败'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-18 19:07:21 +08:00
|
|
|
|
// 修改管理员密码
|
|
|
|
|
|
static async updatePassword(req: Request, res: Response) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { username, oldPassword, newPassword } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
if (!username || !oldPassword || !newPassword) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '参数不完整'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 验证旧密码
|
|
|
|
|
|
const isValid = await SystemConfigModel.validateAdminLogin(username, oldPassword);
|
|
|
|
|
|
if (!isValid) {
|
|
|
|
|
|
return res.status(401).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '原密码错误'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新密码
|
|
|
|
|
|
await SystemConfigModel.updateAdminPassword(username, newPassword);
|
|
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
message: '密码修改成功'
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
|
console.error('修改密码失败:', error);
|
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: error.message || '修改密码失败'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取所有配置(管理员用)
|
|
|
|
|
|
static async getAllConfigs(req: Request, res: Response) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const configs = await SystemConfigModel.getAllConfigs();
|
|
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
data: configs
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
|
console.error('获取配置失败:', error);
|
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: error.message || '获取配置失败'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|