短报文教学

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

88
app/service/home.js Normal file
View File

@@ -0,0 +1,88 @@
'use strict';
const BaseService = require('../core/base_service.js');
const sha256 = require("crypto-js/sha256");
const fileName = "home";
class HomeService extends BaseService {
// token 状态查询接口
async tokenStatus(uid) {
const { ctx } = this;
const redis = ctx.app.redis;
try {
let result = {};
const tokenUser = await redis.get('db4').get(uid);
if (!tokenUser) {
const where = { user_uid: uid };
const attributes = ['token'];
result.remainder = false;
result.token = await ctx.model.BuserToken.findOne({ attributes, where });
result.type = result.token == null ? 'none' : "long";
} else {
result.remainder = await redis.get('db4').ttl(uid);
result.token = JSON.parse(tokenUser).token;
result.type = JSON.parse(tokenUser).lifeHold ? "lifeHold" : "short";
}
const token = await redis.get('db4').get(result.token);
if(token) {
result.attacksMark = JSON.parse(token).attacksMark;
result.banAt = result.attacksMark ? JSON.parse(token).banAt : 0;
}
return result ? result : false;
} catch (error) {
const blockName = fileName + "--" + "tokenStatus";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 用户获取短期 token 的接口
async getUserToken(uid) {
const { ctx } = this;
const redis = ctx.app.redis;
try {
const token = this.shaForm(uid, this.generateRandomString());
const checkUser = await redis.get('db4').get(uid);
let tokenUser, tokenMark;
if (!checkUser) {
const where = { user_uid: uid };
const attributes = ['token'];
const result = await ctx.model.BuserToken.findOne({ attributes, where });
if (result == null) {
// 设置过期时间为一周
tokenUser = await redis.get('db4').set(uid, JSON.stringify({ token, lifeHold: false }), 'EX', 604800);
tokenMark = await redis.get('db4').set(token, JSON.stringify({ uid, attacksMark: false }), 'EX', 604800);
}
}
return tokenUser && tokenMark ? true : false;
} catch (error) {
const blockName = fileName + "--" + "getUserToken";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 用户延续短期 token 有效时间的接口
async lifeHold(uid) {
const { ctx } = this;
const redis = ctx.app.redis;
try {
const user = await redis.get('db4').get(uid);
let tokenUser, tokenMark;
if (!JSON.parse(user).lifeHold) {
let ex = await redis.get('db4').ttl(uid) + 604800;
tokenUser = await redis.get('db4').set(uid, JSON.stringify({ token: JSON.parse(user).token, lifeHold: true }), 'EX', ex);
tokenMark = await redis.get('db4').expire(JSON.parse(user).token, ex);
}
return tokenUser && tokenMark ? true : false;
} catch (error) {
const blockName = fileName + "--" + "lifeHold";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
}
module.exports = HomeService;

100
app/service/teaching.js Normal file
View File

@@ -0,0 +1,100 @@
'use strict';
const BaseService = require('../core/base_service.js');
const fileName = "teaching";
class TeachingService extends BaseService {
// 获取最近发送的一条信息
async getMsg(redis, token) {
const { ctx } = this;
try {
const verifyResult = await this.Verify(redis, token);
let result = { code: verifyResult };
if (verifyResult == 401) {
const where = { token };
const attributes = ['token'];
const dbResult = await ctx.model.BuserToken.findOne({ attributes, where });
result.code = dbResult ? dbResult.dataValus.user_uid : 401;
}
if (result.code !== 401 && result.code !== 403) {
const uidLastCode = this.shunt(result.code);
const model = {
"B": ctx.model.BShortMsg,
"C": ctx.model2.CShortMsg
};
const attributes = ['card_id', 'encode_type', 'msg_data', 'created_at'];
const where = { user_uid: result.code };
const limit = 1;
const order = [['created_at', 'DESC']];
const dbOptions = { attributes, limit, order, where };
const dbResult = await model[uidLastCode].findOne(dbOptions);
if (dbResult) {
result.userData = dbResult.dataValues;
result.code = 200;
} else {
result.code = false;
}
}
return result.code ? result : false;
} catch (error) {
const blockName = fileName + "--" + "getMsg";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 获取若干条历史消息
async getHistory(redis, token, limit, offset) {
const { ctx } = this;
try {
const verifyResult = await this.Verify(redis, token);
let result = { code: verifyResult };
if (verifyResult == 401) {
const where = { token };
const attributes = ['token'];
const dbResult = await ctx.model.BuserToken.findOne({ attributes, where });
result.code = dbResult ? dbResult.dataValus.user_uid : 401;
}
if (result.code !== 401 && result.code !== 403) {
const uidLastCode = this.shunt(result.code);
const model = {
"B": ctx.model.BShortMsg,
"C": ctx.model2.CShortMsg
};
const attributes = ['card_id', 'encode_type', 'msg_data', 'created_at'];
const where = { user_uid: result.code };
const order = [['created_at', 'DESC']];
if (limit == undefined || !this.isAllDigits(limit)) {
limit = 10
} else {
limit = parseInt(limit);
}
if (offset == undefined || !this.isAllDigits(offset)) {
offset = 1
} else {
offset = parseInt(offset);
}
offset = (offset - 1) * limit;
const dbOptions = { attributes, limit, offset, order, where };
const dbResult = await model[uidLastCode].findAndCountAll(dbOptions);
if (dbResult.rows.length !== 0) {
result.userData = {
rows: dbResult.rows,
count: dbResult.count
};
result.code = 200;
} else {
result.code = false;
}
}
return result.code ? result : false;
} catch (error) {
const blockName = fileName + "--" + "getHistory";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
}
module.exports = TeachingService;