Files

74 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2026-03-27 17:57:07 +08:00
const BaseController = require('../core/base_controller.js');
const fileName = "teaching";
class TeachingController extends BaseController {
// 获取最近发送的一条信息
async getMsg() {
const { ctx } = this;
try {
const redis = ctx.app.redis;
const token = ctx.request.query.token;
this.banManager(redis, token);
const result = await ctx.service.teaching.getMsg(redis, token);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
} else if (result.code == 401) {
this.setResponse(ctx, 't401', 'token 过期或错误', "none");
} else if (result.code == 403) {
this.setResponse(ctx, 403, '此 token 已进入黑名单,请等待解封后再使用', "none");
} else if (result.code == 200) {
delete result.code;
const data = {
userData: result.userData,
responseTime: new Date()
};
this.setResponse(ctx, 200, '最近消息查询成功', "deconstruction", data);
} else {
this.setResponse(ctx, 404, '最近消息查询失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "getMsg";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 获取若干条历史消息
async getHistory() {
const { ctx } = this;
try {
const redis = ctx.app.redis;
const { token, limit, offset } = ctx.request.query;
this.banManager(redis, token);
const result = await ctx.service.teaching.getHistory(redis, token, limit, offset);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
} else if (result.code == 401) {
this.setResponse(ctx, 't401', 'token 过期或错误', "none");
} else if (result.code == 403) {
this.setResponse(ctx, 403, '此 token 已进入黑名单,请等待解封后再使用', "none");
} else if (result.code == 200) {
delete result.code;
let historyMsg = [];
result.userData.rows.forEach(element => {
historyMsg.push(element.dataValues);
});
const data = {
userData: historyMsg,
total: result.userData.count
};
this.setResponse(ctx, 200, '历史消息查询成功', "deconstruction", data);
} else {
this.setResponse(ctx, 404, '历史消息查询失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "getHistory";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
}
module.exports = TeachingController;