短报文教学
This commit is contained in:
100
app/service/teaching.js
Normal file
100
app/service/teaching.js
Normal 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;
|
||||
Reference in New Issue
Block a user