193 lines
7.8 KiB
JavaScript
193 lines
7.8 KiB
JavaScript
const Controller = require('../core/base_controller');
|
|
|
|
const fs = require('fs');
|
|
|
|
const fileName = "Exam";
|
|
|
|
class ExamController extends Controller {
|
|
// 管理系统新增考试信息
|
|
async addExam() {
|
|
const { ctx } = this;
|
|
try {
|
|
let rdmcode = await this.checkUid(ctx);
|
|
if (!rdmcode) {
|
|
this.setResponse(ctx, 401, 'token过期或错误', "none");
|
|
return;
|
|
}
|
|
const requestMsg = ctx.request.body.requestMsg;
|
|
requestMsg.forEach(element => {
|
|
element.uid = this.decryptRequest(element.uid);
|
|
element.exam_name = this.decryptRequest(element.exam_name);
|
|
element.status = Number(this.decryptRequest(element.status));
|
|
element.application_start = element.application_start;
|
|
element.application_end = element.application_end;
|
|
element.exam_start = element.exam_start;
|
|
element.exam_end = element.exam_end;
|
|
});
|
|
const result = await ctx.service.exam.addExam(requestMsg);
|
|
if (result == "error") {
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
return;
|
|
}
|
|
if (result) {
|
|
this.setResponse(ctx, 200, '操作成功', "none");
|
|
} else {
|
|
this.setResponse(ctx, 404, '操作失败', "none");
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "addExam";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 管理系统查看考试信息
|
|
async showExam() {
|
|
const { ctx } = this;
|
|
try {
|
|
let rdmcode = await this.checkUid(ctx);
|
|
if (!rdmcode) {
|
|
this.setResponse(ctx, 401, 'token过期或错误', "none");
|
|
return;
|
|
}
|
|
const uid = JSON.parse(rdmcode).uid;
|
|
const body = ctx.request.body;
|
|
const result = await ctx.service.exam.showExam(uid, body);
|
|
if (result == "error") {
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
return;
|
|
}
|
|
if (result) {
|
|
let examMsg = [];
|
|
result.rows.forEach(element => {
|
|
examMsg.push(element.dataValues);
|
|
});
|
|
const data = {
|
|
examMsg,
|
|
count: result.count
|
|
};
|
|
this.setResponse(ctx, 200, '操作成功', "deconstruction", data);
|
|
} else {
|
|
this.setResponse(ctx, 404, '操作失败', "none");
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "showExam";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 管理系统编辑考试信息
|
|
async editExam() {
|
|
const { ctx } = this;
|
|
try {
|
|
let rdmcode = await this.checkUid(ctx);
|
|
if (!rdmcode) {
|
|
this.setResponse(ctx, 401, 'token过期或错误', "none");
|
|
return;
|
|
}
|
|
const uid = JSON.parse(rdmcode).uid;
|
|
const { model, updateMsg, requestMsg } = ctx.request.body;
|
|
const modelType = this.decryptRequest(model);
|
|
const result = await ctx.service.exam.editExam(uid, modelType, updateMsg, requestMsg);
|
|
if (result == "error") {
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
return;
|
|
}
|
|
if (result) {
|
|
this.setResponse(ctx, 200, '操作成功', "none");
|
|
} else {
|
|
this.setResponse(ctx, 404, '操作失败', "none");
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "editExam";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 管理系统删除考试信息
|
|
async deleteExam() {
|
|
const { ctx } = this;
|
|
try {
|
|
let rdmcode = await this.checkUid(ctx);
|
|
if (!rdmcode) {
|
|
this.setResponse(ctx, 401, 'token过期或错误', "none");
|
|
return;
|
|
}
|
|
const uid = JSON.parse(rdmcode).uid;
|
|
const { model, password, requestMsg } = ctx.request.body;
|
|
const modelType = this.decryptRequest(model);
|
|
const deCodePassword = this.decryptRequest(password);
|
|
const result = await ctx.service.exam.deleteExam(uid, modelType, deCodePassword, requestMsg);
|
|
if (result == "error") {
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
return;
|
|
}
|
|
if (result) {
|
|
this.setResponse(ctx, 200, '操作成功', "none");
|
|
} else {
|
|
this.setResponse(ctx, 404, '操作失败', "none");
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "deleteExam";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 管理系统导出考试信息数据
|
|
async downloadExamData() {
|
|
const { ctx } = this;
|
|
try {
|
|
let rdmcode = await this.checkUid(ctx);
|
|
if (!rdmcode) {
|
|
this.setResponse(ctx, 401, 'token过期或错误', "none");
|
|
return;
|
|
}
|
|
const uid = JSON.parse(rdmcode).uid;
|
|
const { form, searchMsg, dataHeaders } = ctx.request.body;
|
|
const result = await ctx.service.exam.downloadExamData(uid, searchMsg);
|
|
if (result == "error") {
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
return;
|
|
}
|
|
if (result.code) {
|
|
const file = this.downloadData(form, result.data, dataHeaders);
|
|
if (file) {
|
|
const { filePath, completeName, buf } = file;
|
|
try {
|
|
fs.writeFileSync(filePath, Buffer.from(buf));
|
|
const data = {
|
|
// 本机服务器地址
|
|
// 'http://127.0.0.1:8020'
|
|
// 'http://192.168.31.28:8020'
|
|
// 公司服务器地址
|
|
// 'http://123.57.205.46:8020'
|
|
// 'http://certadmin.hwasmart.com//bdTrainingExamManager'
|
|
fileUrl: 'http://127.0.0.1:8020' + `/public/${completeName}?time=${new Date().getTime()}`,
|
|
fileName: completeName,
|
|
}
|
|
this.setResponse(ctx, 200, '文件已成功写入', "deconstruction", data);
|
|
setTimeout(() => {
|
|
fs.unlinkSync(filePath);
|
|
}, 600000);
|
|
} catch (error) {
|
|
this.setResponse(ctx, 500, '写入文件时发生错误', "none");
|
|
}
|
|
} else {
|
|
this.setResponse(ctx, 404, '操作失败', "none");
|
|
}
|
|
} else {
|
|
this.setResponse(ctx, 404, '操作失败', "none");
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "downloadExamData";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = ExamController;
|