216 lines
9.0 KiB
JavaScript
216 lines
9.0 KiB
JavaScript
const Controller = require('../core/base_controller');
|
|
|
|
const fs = require('fs');
|
|
|
|
const fileName = "TopicWhitelist";
|
|
|
|
class TopicWhitelistController extends Controller {
|
|
// 管理系统新增题库白名单
|
|
async addTopicWhitelist() {
|
|
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;
|
|
let userMsgObj = {
|
|
phone: [],
|
|
};
|
|
console.log(requestMsg)
|
|
requestMsg.forEach(item => {
|
|
item.phone = this.decryptRequest(item.phone);
|
|
item.type = Number(this.decryptRequest(item.type));
|
|
if (item.expired_at) {
|
|
item.expired_at = this.decryptRequest(item.expired_at);
|
|
} else {
|
|
item.expired_at = null;
|
|
}
|
|
userMsgObj.phone.push(item.phone);
|
|
});
|
|
const result = await ctx.service.topicWhitelist.addTopicWhitelist(userMsgObj, 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 + "--" + "addTopicWhitelist";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 管理系统查看题库白名单
|
|
async showTopicWhitelist() {
|
|
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.topicWhitelist.showTopicWhitelist(uid, body);
|
|
if (result == "error") {
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
return;
|
|
}
|
|
if (result) {
|
|
let idCardArr = [];
|
|
result.rows.forEach(item => {
|
|
idCardArr.push(item.dataValues.examinee.dataValues.identity_card);
|
|
});
|
|
const photoResult = await ctx.service.search.checkPhoto(idCardArr);
|
|
let topicWhitelistMsg = [];
|
|
result.rows.forEach(element => {
|
|
if (photoResult == "error") {
|
|
element.dataValues.userPhotoUrl = '#';
|
|
}
|
|
if (photoResult) {
|
|
ctx.set('Cache-Control', 'no-cache, no-store, must-revalidate');
|
|
ctx.set("Pragma", "no-cache");
|
|
ctx.set("Expires", "0");
|
|
photoResult.forEach(item => {
|
|
if (element.dataValues.examinee.dataValues.identity_card == item.identity_card) {
|
|
element.dataValues.userPhotoUrl = item.photoUrl;
|
|
}
|
|
});
|
|
}
|
|
topicWhitelistMsg.push(element.dataValues);
|
|
});
|
|
const data = {
|
|
topicWhitelistMsg,
|
|
count: result.count
|
|
};
|
|
this.setResponse(ctx, 200, '操作成功', "deconstruction", data);
|
|
} else {
|
|
this.setResponse(ctx, 404, '操作失败', "none");
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "showTopicWhitelist";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 管理系统编辑题库白名单
|
|
async editTopicWhitelist() {
|
|
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.topicWhitelist.editTopicWhitelist(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 + "--" + "editTopicWhitelist";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 管理系统删除题库白名单
|
|
async deleteTopicWhitelist() {
|
|
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.topicWhitelist.deleteTopicWhitelist(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 + "--" + "deleteTopicWhitelist";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 管理系统导出题库白名单
|
|
async downloadTopicWhitelistData() {
|
|
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.topicWhitelist.downloadTopicWhitelistData(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 + "--" + "downloadTopicWhitelistData";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = TopicWhitelistController;
|