Files
Web_BLV_OA_Exam_Prod/api/controllers/adminController.ts

162 lines
4.2 KiB
TypeScript
Raw Normal View History

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: '用户名和密码不能为空'
});
}
const isValid = await SystemConfigModel.validateAdminLogin(username, password);
if (!isValid) {
return res.status(401).json({
success: false,
message: '用户名或密码错误'
});
}
// 这里可以生成JWT token简化处理直接返回成功
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 || '获取统计数据失败'
});
}
}
// 修改管理员密码
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 || '获取配置失败'
});
}
}
}