修改mqtt日志

This commit is contained in:
wuhan 2023-08-03 16:55:32 +08:00
parent 0ddddd5ad8
commit 58d7509c94
5 changed files with 80 additions and 43 deletions

View File

@ -95,11 +95,10 @@ class MqttMapper {
]);
}
async addAllLog({ id, type = 0, text, status = 1, reason }) {
const textLog = type ? 'plaintext' : 'cipher';
async addAllLog({ id, type = 0, plaintext, ciphertext, status = 1, reason }) {
return await db(
`insert into szja_encrypt_operate_log (id,type,${textLog}),status,reason`,
[id, type, text, status],
`insert into szja_encrypt_operate_log (id,type,plaintext,ciphertext,status,fail_reason) values(?,?,?,?,?,?)`,
[id, type, plaintext, ciphertext, status, reason],
);
}
}

View File

@ -80,7 +80,7 @@ client.on('message', async (topic, mes) => {
return;
}
let info;
await mqttMapper.updateLog(0);
// await mqttMapper.updateLog(0);
// topic : 1/req/{hccId}
if (arr[1] === 'req') {
switch (cmd) {
@ -177,25 +177,18 @@ setInterval(async () => {
if (hccInfo.length) {
seId = hccInfo[0].se_id;
}
// 调用加密接口
const info = await mqttService.encrypt(seId, getConfig().keyAlias, {
const obj = {
cmd: '7',
reqId,
payload: { dataType: '01', mpId: '6433be833832fb000180193e' },
});
await mqttMapper.updateLog(1);
// 加密成功
// await mqttMapper.addAllLog({
// id: v4().replace(/-/g, ''),
// type: 0,
// text: JSON.stringify({
// cmd: '7',
// reqId,
// payload: { dataType: '01', mpId: '6433be833832fb000180193e' },
// }),
// status: 1,
// reason: '',
// });
};
// 调用加密接口
const info = await mqttService.encrypt(seId, getConfig().keyAlias, obj);
if (!info) {
console.log('mqtt加密失败');
return;
}
// await mqttMapper.updateLog(1);
client.publish(`2/req/${hccId}`, JSON.stringify({ message: info }), () => {
// 记录reqid放入全局缓存
global.reqIdMap.set(reqId, null);

View File

@ -1,6 +1,7 @@
import { Controller, Post, Body, UseGuards } from '@nestjs/common';
import { OpenDataService } from './open-data.service';
import { RolesGuard } from './open_data.middleware';
import Base from '../utils/base';
@Controller('api/v1/szjt')
@UseGuards(RolesGuard)
@ -19,8 +20,8 @@ export class OpenDataController {
@Post('/user/queryNum')
userData(@Body() { areaCode, type, limitSize }) {
if (!type){
return Base.error('type传参错误')
if (!type) {
return Base.error('type传参错误');
}
if (!limitSize) {
if (Number(type)) {
@ -29,7 +30,11 @@ export class OpenDataController {
limitSize = 24;
}
}
return this.openDataService.userData(areaCode, Number(type), Number(limitSize));
return this.openDataService.userData(
areaCode,
Number(type),
Number(limitSize),
);
}
@Post('/mp/queryNum')
@ -48,7 +53,11 @@ export class OpenDataController {
if (!type) {
type = 0;
}
return this.openDataService.userFile(Number(pageNo), Number(pageSize), Number(type));
return this.openDataService.userFile(
Number(pageNo),
Number(pageSize),
Number(type),
);
}
@Post('/user/queryDataList')
@ -79,6 +88,10 @@ export class OpenDataController {
if (!pageSize) {
pageSize = 10;
}
return this.openDataService.homeList(Number(pageNo), Number(pageSize), areaCode);
return this.openDataService.homeList(
Number(pageNo),
Number(pageSize),
areaCode,
);
}
}

View File

@ -8,6 +8,8 @@ import {
} from '../utils/index';
import Base from '../utils/base';
import globalConfig from '../utils/global';
import mqttMapper from 'src/mqtt/mqtt.mapper';
import { v4 } from 'uuid';
@Injectable()
export class SecureService {
@ -34,26 +36,36 @@ export class SecureService {
if (globalConfig.cookie[seId]) {
headers['cookie'] = globalConfig.cookie[seId];
}
const options = {
seId,
keyAlias,
data: toBase64(data),
algorithm,
iv,
};
// 调用加密接口
const result = await AxiosResult(
`${getConfig().secureIp}/api/v1/se/key/encrypt`,
{
seId,
keyAlias,
data: toBase64(data),
algorithm,
iv,
},
options,
{
headers,
},
1,
0,
);
if (result.isError) {
return;
}
// 取出header
// globalConfig.cookie[seId] = result.headers['set-cookie'][0];
// 加密成功
await mqttMapper.addAllLog({
id: v4().replace(/-/g, ''),
type: 0,
plaintext: JSON.stringify(options),
ciphertext: result.data.data.encData,
status: 1,
reason: '',
});
return result.data.data.encData;
}
@ -77,24 +89,33 @@ export class SecureService {
if (globalConfig.cookie[seId]) {
headers['cookie'] = globalConfig.cookie[seId];
}
const options = {
seId,
keyAlias,
dataEnc,
algorithm,
iv,
};
const result = await AxiosResult(
`${getConfig().secureIp}/api/v1/se/key/decrypt`,
{
seId,
keyAlias,
dataEnc,
algorithm,
iv,
},
options,
{
headers,
},
0,
1,
);
if (result.isError) {
// 错误的回应
return;
}
await mqttMapper.addAllLog({
id: v4().replace(/-/g, ''),
type: 1,
plaintext: JSON.stringify(options),
ciphertext: result.data.data.data,
status: 1,
reason: '',
});
if (result.headers['set-cookie']?.length) {
globalConfig.cookie[seId] = result.headers['set-cookie'][0];
}

View File

@ -1,6 +1,7 @@
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
import axios from 'axios';
import mqttMapper from '../mqtt/mqtt.mapper';
import { v4 } from 'uuid';
/**
* axios
@ -38,6 +39,7 @@ export async function get(url: string, config?) {
}
}
// 用于安全接口加解密
export async function AxiosResult(url: string, options: any, config?, type?) {
try {
// console.log(
@ -52,7 +54,16 @@ export async function AxiosResult(url: string, options: any, config?, type?) {
return result;
} catch (error) {
// console.log(error);
await mqttMapper.addErrorLog(JSON.stringify(error), type);
// 加密失败
await mqttMapper.addAllLog({
id: v4().replace(/-/g, ''),
type: type,
plaintext: JSON.stringify(options),
ciphertext: '',
reason: JSON.stringify(error),
status: 0,
});
// await mqttMapper.addErrorLog(JSON.stringify(error), type);
return {
isError: true,
respCode: error.code,