短报文教学

This commit is contained in:
2026-03-27 17:57:07 +08:00
commit 4da5047bf2
27 changed files with 1073 additions and 0 deletions

96
app/controller/home.js Normal file
View File

@@ -0,0 +1,96 @@
const BaseController = require('../core/base_controller.js');
const fileName = "home";
class HomeController extends BaseController {
// token状态查询接口
async tokenStatus() {
const { ctx } = this;
try {
const uid = this.decryptRequest(ctx.request.body.uid).split('?')[0];
let rdmcode = await this.checkUid(ctx, uid);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token 过期或错误', "none");
return;
}
const result = await ctx.service.home.tokenStatus(uid);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
this.setResponse(ctx, 200, 'token 状态查询成功', "deconstruction", result);
} else {
this.setResponse(ctx, 404, 'token 状态查询失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "tokenStatus";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 用户获取短期token的接口
async getUserToken() {
const { ctx } = this;
try {
const uid = this.decryptRequest(ctx.request.body.uid).split('?')[0];
if (uid == "") {
this.setResponse(ctx, 404, '获取临时 token 失败', "none");
return;
}
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token 过期或错误', "none");
return;
}
const result = await ctx.service.home.getUserToken(uid);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
this.setResponse(ctx, 200, '获取临时 token 成功', "none");
} else {
this.setResponse(ctx, 404, '获取临时 token 失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "getUserToken";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 用户延续短期token有效时间的接口
async lifeHold() {
const { ctx } = this;
try {
const uid = this.decryptRequest(ctx.request.body.uid).split('?')[0];
if (uid == "") {
this.setResponse(ctx, 404, '延续临时 token 有效时间失败', "none");
return;
}
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token 过期或错误', "none");
return;
}
const result = await ctx.service.home.lifeHold(uid);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
this.setResponse(ctx, 200, '延续临时 token 有效时间成功', "none");
} else {
this.setResponse(ctx, 404, '延续临时 token 有效时间失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "lifeHold";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
}
module.exports = HomeController;

View File

@@ -0,0 +1,73 @@
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;