diff --git a/app/controller/home.js b/app/controller/home.js index f2906e9..1e67834 100644 --- a/app/controller/home.js +++ b/app/controller/home.js @@ -16,8 +16,8 @@ const KEY = CryptoJS.enc.Utf8.parse('BDQH'); const decryptRequest = (body) => { return CryptoJS.AES.decrypt(body, KEY, { - mode: CryptoJS.mode.ECB, - padding: CryptoJS.pad.Pkcs7 + mode: CryptoJS.mode.ECB, + padding: CryptoJS.pad.Pkcs7 }).toString(CryptoJS.enc.Utf8); } @@ -72,7 +72,7 @@ class HomeController extends Controller { } const uid = JSON.parse(rdmcode).uid; const result = await ctx.service.home.getSN(uid); - if(result == "error") { + if (result == "error") { setResponse(ctx, 500, '服务器错误', "none"); return; } @@ -101,7 +101,7 @@ class HomeController extends Controller { const uid = JSON.parse(rdmcode).uid; const { snNumber } = ctx.request.body; const result = await ctx.service.home.searchSN(snNumber); - if(result == "error") { + if (result == "error") { setResponse(ctx, 500, '服务器错误', "none"); return; } @@ -134,7 +134,7 @@ class HomeController extends Controller { } const uid = JSON.parse(rdmcode).uid; const result = await ctx.service.home.searchBindSN(uid); - if(result == "error") { + if (result == "error") { setResponse(ctx, 500, '服务器错误', "none"); return; } @@ -147,9 +147,9 @@ class HomeController extends Controller { setResponse(ctx, 404, '获取可绑定的SN号失败', "none"); } } catch (error) { - const blockName = fileName + "--" + "searchBindSN"; - ctx.app.middleware.utils.log('../../../controllerLog', blockName + ">>>>" + error); - setResponse(ctx, 500, '服务器错误', "none"); + const blockName = fileName + "--" + "searchBindSN"; + ctx.app.middleware.utils.log('../../../controllerLog', blockName + ">>>>" + error); + setResponse(ctx, 500, '服务器错误', "none"); } } @@ -164,7 +164,7 @@ class HomeController extends Controller { } const uid = JSON.parse(rdmcode).uid; const result = await ctx.service.home.searchBindUser(uid); - if(result == "error") { + if (result == "error") { setResponse(ctx, 500, '服务器错误', "none"); return; } @@ -195,7 +195,7 @@ class HomeController extends Controller { const uid = JSON.parse(rdmcode).uid; const body = ctx.request.body; const result = await ctx.service.home.bindSN(uid, body); - if(result == "error") { + if (result == "error") { setResponse(ctx, 500, '服务器错误', "none"); return; } @@ -226,7 +226,7 @@ class HomeController extends Controller { const password = decryptRequest(body.password); const requestMsg = body.userMsg; const result = await ctx.service.home.unBindingSN(uid, model, password, requestMsg); - if(result == "error") { + if (result == "error") { setResponse(ctx, 500, '服务器错误', "none"); return; } @@ -254,7 +254,7 @@ class HomeController extends Controller { const uid = JSON.parse(rdmcode).uid; const body = ctx.request.body; const result = await ctx.service.home.getRecord(uid, body); - if(result == "error") { + if (result == "error") { setResponse(ctx, 500, '服务器错误', "none"); return; } @@ -281,9 +281,8 @@ class HomeController extends Controller { // 存储消息记录 async shortMsg() { const ctx = this.ctx; - try { + try { const body = ctx.request.body; - console.log(body) const { sn_number, original_msg, msg_data, created_at } = body; const form = { sn_number: sn_number, @@ -292,7 +291,7 @@ class HomeController extends Controller { created_at: created_at }; const result = await ctx.service.home.shortMsg(form); - if(result == "error") { + if (result == "error") { setResponse(ctx, 500, '服务器错误', "none"); return; } @@ -308,6 +307,35 @@ class HomeController extends Controller { } } + // 保存未知消息 + async saveUnknown() { + const { ctx } = this; + try { + const body = ctx.request.body; + const { system_type, original_msg, msg_data, created_at } = body; + const form = { + system_type: system_type, + original_msg: original_msg, + msg_data: msg_data, + created_at: created_at + }; + const result = await ctx.service.home.saveUnknown(form); + 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 + "--" + "saveUnknown"; + ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error); + this.setResponse(ctx, 500, '服务器错误', "none"); + } + } + // 获取历史消息 async getHistoryMsg() { const ctx = this.ctx; @@ -320,7 +348,7 @@ class HomeController extends Controller { const uid = JSON.parse(rdmcode).uid; const body = ctx.request.body; const result = await ctx.service.home.getHistoryMsg(uid, body); - if(result == "error") { + if (result == "error") { setResponse(ctx, 500, '服务器错误', "none"); return; } @@ -344,36 +372,36 @@ class HomeController extends Controller { } } - // 删除历史消息 - async delHistoryMsg() { - const ctx = this.ctx; - try { - let rdmcode = await checkUid(ctx); - if (!rdmcode) { - setResponse(ctx, 401, 'token过期或错误', "none"); - return; - } - const uid = JSON.parse(rdmcode).uid; - const body = ctx.request.body; - const model = decryptRequest(body.model); - const password = decryptRequest(body.password); - const requestMsg = body.userMsg; - const resetList = await ctx.service.home.delHistoryMsg(uid, model, password, requestMsg); - if(resetList == "error") { - setResponse(ctx, 500, '服务器错误', "none"); - return; - } - if (resetList) { - setResponse(ctx, 200, '删除历史消息成功', "none"); - } else { - setResponse(ctx, 404, '删除历史消息失败', "none"); - } - } catch (error) { - const blockName = fileName + "--" + "delHistoryMsg"; - ctx.app.middleware.utils.log('../../../controllerLog', blockName + ">>>>" + error); - setResponse(ctx, 500, '服务器错误', "none"); + // 删除历史消息 + async delHistoryMsg() { + const ctx = this.ctx; + try { + let rdmcode = await checkUid(ctx); + if (!rdmcode) { + setResponse(ctx, 401, 'token过期或错误', "none"); + return; } + const uid = JSON.parse(rdmcode).uid; + const body = ctx.request.body; + const model = decryptRequest(body.model); + const password = decryptRequest(body.password); + const requestMsg = body.userMsg; + const resetList = await ctx.service.home.delHistoryMsg(uid, model, password, requestMsg); + if (resetList == "error") { + setResponse(ctx, 500, '服务器错误', "none"); + return; + } + if (resetList) { + setResponse(ctx, 200, '删除历史消息成功', "none"); + } else { + setResponse(ctx, 404, '删除历史消息失败', "none"); + } + } catch (error) { + const blockName = fileName + "--" + "delHistoryMsg"; + ctx.app.middleware.utils.log('../../../controllerLog', blockName + ">>>>" + error); + setResponse(ctx, 500, '服务器错误', "none"); } + } /** * 将sequelize查询结果转换为普通对象数组 @@ -399,11 +427,11 @@ class HomeController extends Controller { } const uid = JSON.parse(rdmcode).uid; let result; - if(uid[uid.length - 1] == 'B') { + if (uid[uid.length - 1] == 'B') { const body = ctx.request.body; result = await ctx.service.home.bsideUserManager(uid, body); } - if(result == "error") { + if (result == "error") { setResponse(ctx, 500, '服务器错误', "none"); return; } @@ -435,11 +463,11 @@ class HomeController extends Controller { } const uid = JSON.parse(rdmcode).uid; let result; - if(uid[uid.length - 1] == 'B') { + if (uid[uid.length - 1] == 'B') { const body = ctx.request.body; result = await ctx.service.home.bsideSNManager(uid, body); } - if(result == "error") { + if (result == "error") { setResponse(ctx, 500, '服务器错误', "none"); return; } diff --git a/app/middleware/socket/socketServer.js b/app/middleware/socket/socketServer.js index f45d33f..a9a34fb 100644 --- a/app/middleware/socket/socketServer.js +++ b/app/middleware/socket/socketServer.js @@ -21,14 +21,14 @@ module.exports = (app) => { // 00000043010000000000000000000000000000000000000000 18 31353037365f32303236303330333135313835315f323832 00 0f 0102030405060708090a0b0c0d0e0f let sn_value = value.slice(52, 100), snBuffer = Buffer.from(sn_value, 'hex'), - snNumber = snBuffer.toString('ascii').split("_")[0], + snNumber = snBuffer.toString('ascii').split("_")[0].padStart(8, '0'), msg_value = value.slice(104, value.length), msgBuffer = Buffer.from(msg_value, 'hex'), msgData = msgBuffer.toString('ascii'); console.log(msgData) // socket接收消息后触发事件监听器,将消息发送给websocket客户端并存储在数据库中 shunt.socketEmitter().emit(snNumber, { msg_data: msgData, created_at: new Date() }); - await app.curl('http://127.0.0.1:' + app.server.address().port + '/shortMsg', { + const shortMsg = await app.curl('http://127.0.0.1:' + app.server.address().port + '/shortMsg', { dataType: 'json', contentType: 'application/x-www-form-urlencoded', method: 'POST', @@ -40,6 +40,20 @@ module.exports = (app) => { created_at: new Date() }, }); + if (shortMsg.data.code == 404) { + await app.curl('http://127.0.0.1:' + app.server.address().port + '/saveUnknown', { + dataType: 'json', + contentType: 'application/x-www-form-urlencoded', + method: 'POST', + timeout: 3000, + data: { + system_type: 1, + original_msg: value, + msg_data: msgData, + created_at: new Date() + }, + }); + } }); socket.on('end', function (data) { console.log(data); diff --git a/app/model3/Unknown.js b/app/model3/Unknown.js new file mode 100644 index 0000000..1ba5de1 --- /dev/null +++ b/app/model3/Unknown.js @@ -0,0 +1,20 @@ +'use strict'; + +module.exports = (app) => { + const { INTEGER, TEXT, DATE } = app.Sequelize; + + const Unknown = app.model3.define('unknown', { + id: { type: INTEGER(8), primaryKey: true, autoIncrement: true }, + system_type: INTEGER(3), + original_msg: TEXT, + msg_data: TEXT, + created_at: DATE, + },{ + freezeTableName: true, + timestamp: true, + createdAt: true, + updatedAt: false, + }); + + return Unknown; +}; \ No newline at end of file diff --git a/app/router.js b/app/router.js index 4300bdf..b9f603c 100644 --- a/app/router.js +++ b/app/router.js @@ -30,6 +30,8 @@ module.exports = app => { router.post('/getRecord', controller.home.getRecord); // 存储消息记录 router.post('/shortMsg', controller.home.shortMsg); + // 保存未知消息 + router.post("/saveUnknown", controller.home.saveUnknown); // 获取历史消息 router.post('/getHistoryMsg', controller.home.getHistoryMsg); // 删除历史消息 diff --git a/app/service/home.js b/app/service/home.js index 2c77cbc..4c43adc 100644 --- a/app/service/home.js +++ b/app/service/home.js @@ -217,18 +217,18 @@ class HomeService extends Service { let { offset, limit, searchOptions, sortOptions } = body; offset = (offset - 1) * limit; let attributes = ['user_uid', 'sn_number', 'type', 'created_at'], - where = {}, order = [], dbOptions, joinSubstring = [], - substring = ['user_uid', 'sn_number'], key = ['type'], between = ['created_at']; + where = {}, order = [], dbOptions, joinSubstring = [], + substring = ['user_uid', 'sn_number'], key = ['type'], between = ['created_at']; const uidLastCode = shunt(uid); attributes.forEach(element => { if (searchOptions[element]) { - if(searchOptions[element] == '*空') { - if(joinSubstring.includes(element)) { - where[element] = null; + if (searchOptions[element] == '*空') { + if (joinSubstring.includes(element)) { + where[element] = null; } else { - where[element] = [null, '']; + where[element] = [null, '']; } - } else if(substring.includes(element)) { + } else if (substring.includes(element)) { where[element] = { [Op.substring]: searchOptions[element] }; } else if (key.includes(element)) { where[element] = searchOptions[element] == "1" ? uidLastCode === 'B' ? 1 : 2 : 0; @@ -302,6 +302,19 @@ class HomeService extends Service { } } + // 保存未知消息 + async saveUnknown(form) { + const { ctx } = this; + try { + const result = await ctx.model3.Unknown.create(form); + return result || false; + } catch (error) { + const blockName = fileName + "--" + "saveUnknown"; + ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); + return 'error'; + } + } + // 获取历史消息 async getHistoryMsg(uid, body) { const { ctx } = this; @@ -309,15 +322,15 @@ class HomeService extends Service { let { offset, limit, searchOptions, sortOptions } = body; offset = (offset - 1) * limit; let attributes = ['original_msg', 'sn_number', 'msg_data', 'created_at'], - where = { user_uid: uid }, order = [], dbOptions, joinSubstring = [], - substring = ['original_msg', 'sn_number', 'msg_data'], key = [], between = ['created_at']; + where = { user_uid: uid }, order = [], dbOptions, joinSubstring = [], + substring = ['original_msg', 'sn_number', 'msg_data'], key = [], between = ['created_at']; attributes.forEach(element => { if (searchOptions[element]) { - if(searchOptions[element] == '*空') { - if(joinSubstring.includes(element)) { - where[element] = null; + if (searchOptions[element] == '*空') { + if (joinSubstring.includes(element)) { + where[element] = null; } else { - where[element] = [null, '']; + where[element] = [null, '']; } } else if (substring.includes(element)) { where[element] = { [Op.substring]: searchOptions[element] }; @@ -370,8 +383,8 @@ class HomeService extends Service { for (const key in requestMsg) { requestMsg[key].forEach(element => { sn_number.push(element.sn_number), - msg_data.push(element.msg_data), - created_at.push(element.origin_time) + msg_data.push(element.msg_data), + created_at.push(element.origin_time) }); } where = { user_uid: uid, sn_number, msg_data, created_at }; @@ -401,16 +414,16 @@ class HomeService extends Service { let { offset, limit, searchOptions } = body; offset = (offset - 1) * limit; let attributes = ['account_name', 'user_name', 'phone'], - where = { organization: result.organization }, dbOptions, joinSubstring = []; + where = { organization: result.organization }, dbOptions, joinSubstring = []; attributes.forEach(element => { if (searchOptions[element]) { - if(searchOptions[element] == '*空') { - if(joinSubstring.includes(element)) { - where[element] = null; + if (searchOptions[element] == '*空') { + if (joinSubstring.includes(element)) { + where[element] = null; } else { - where[element] = [null, '']; + where[element] = [null, '']; } - } else{ + } else { where[element] = { [Op.substring]: searchOptions[element] }; } } @@ -432,7 +445,7 @@ class HomeService extends Service { const result = await ctx.model.BSide.findOne({ where: { uid, status: { [Op.not]: 2 }, role: 1 } }); if (!result || result.organization == null) return false; let attributes = ['length', 'sn_set'], - where = { user_uid: uid }, dbOptions; + where = { user_uid: uid }, dbOptions; dbOptions = { attributes, where }; const snMsg = await ctx.model.BuserSNAdmin.findOne(dbOptions); if (!snMsg) return false;