mqtt加解密
This commit is contained in:
parent
cc5d939bea
commit
9d04d9d5f2
|
@ -1,6 +1,17 @@
|
|||
import db from '../utils/mysql';
|
||||
//应用服务
|
||||
class MpMapper {
|
||||
async getMpInfoById(mpIdOrName: string) {
|
||||
return await db(
|
||||
'select a.limit_flag as limitFlag,a.sso_app_key as ssoAppKey,a.sso_app_kecret as ssoAppSecret from szja_mp_info a where mp_id = ?',
|
||||
[mpIdOrName],
|
||||
);
|
||||
}
|
||||
|
||||
async getMpInfo() {
|
||||
return await db('select * from szja_mp_info');
|
||||
}
|
||||
|
||||
async getMpInfoV2() {
|
||||
return await db('select mp_id as mpId from szja_mp_info');
|
||||
}
|
||||
|
|
|
@ -36,6 +36,22 @@ export class MpService {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询应用服务用户信息权限
|
||||
* @param req
|
||||
* @param res
|
||||
* @param next
|
||||
*/
|
||||
async queryMPInfos(mpIdOrName) {
|
||||
let data;
|
||||
if (mpIdOrName) {
|
||||
data = await mpMapper.getMpInfoById(mpIdOrName);
|
||||
} else {
|
||||
data = await mpMapper.getMpInfo();
|
||||
}
|
||||
return Base.success(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用服务后台从全国/地方服务平台获取用户信息
|
||||
* @param req
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import Base from '../utils/base';
|
||||
import userMapper from '../user/user.mapper';
|
||||
import MqttMapper from './mqtt.mapper';
|
||||
import { SecureService } from 'src/secure/secure.service';
|
||||
|
||||
class MqttService {
|
||||
/**
|
||||
|
@ -93,6 +94,21 @@ class MqttService {
|
|||
hccId,
|
||||
});
|
||||
}
|
||||
|
||||
// 封装mqtt加密
|
||||
async encrypt(seId, keyAlias, data) {
|
||||
const info = await new SecureService().encrypt({
|
||||
seId,
|
||||
keyAlias,
|
||||
data,
|
||||
});
|
||||
if (!info) {
|
||||
// 加密失败
|
||||
console.log('mqtt加密失败');
|
||||
return '';
|
||||
}
|
||||
return info;
|
||||
}
|
||||
}
|
||||
|
||||
export default new MqttService();
|
||||
|
|
|
@ -50,6 +50,7 @@ client.on('message', async (topic, mes) => {
|
|||
console.log('mqtt解密失败');
|
||||
return;
|
||||
}
|
||||
console.log('mqtt解密之后获取的数据:', data);
|
||||
const { cmd, reqId, payload } = data;
|
||||
console.log(
|
||||
`${new Date()}----cmd:${cmd}, reqId:${reqId}, payload:${JSON.stringify(
|
||||
|
@ -68,6 +69,7 @@ client.on('message', async (topic, mes) => {
|
|||
const { dataList } = payload;
|
||||
return;
|
||||
}
|
||||
let info;
|
||||
// topic : 1/req/{hccId}
|
||||
if (arr[1] === 'req') {
|
||||
switch (cmd) {
|
||||
|
@ -75,12 +77,14 @@ client.on('message', async (topic, mes) => {
|
|||
// 家庭绑定同步
|
||||
const { bindList } = payload;
|
||||
await mqttService.bindInfo(bindList, hccId);
|
||||
client.publish(`2/rsp/${hccId}`, JSON.stringify(res));
|
||||
info = await mqttService.encrypt(seId, keyAlias, res);
|
||||
client.publish(`2/rsp/${hccId}`, info);
|
||||
break;
|
||||
case '2':
|
||||
// 家庭解绑同步
|
||||
await mqttService.unbind(payload);
|
||||
client.publish(`2/rsp/${hccId}`, JSON.stringify(res));
|
||||
info = await mqttService.encrypt(seId, keyAlias, res);
|
||||
client.publish(`2/rsp/${hccId}`, info);
|
||||
break;
|
||||
case '3':
|
||||
// 查询小程序列表
|
||||
|
@ -88,6 +92,7 @@ client.on('message', async (topic, mes) => {
|
|||
res.payload = {
|
||||
appInfos: data,
|
||||
};
|
||||
info = await mqttService.encrypt(seId, keyAlias, res);
|
||||
client.publish(`2/rsp/${hccId}`, JSON.stringify(res));
|
||||
break;
|
||||
case '4':
|
||||
|
@ -98,6 +103,7 @@ client.on('message', async (topic, mes) => {
|
|||
address_detail,
|
||||
hccId,
|
||||
});
|
||||
info = await mqttService.encrypt(seId, keyAlias, res);
|
||||
client.publish(`2/rsp/${hccId}`, JSON.stringify(res));
|
||||
break;
|
||||
case '5':
|
||||
|
@ -106,6 +112,7 @@ client.on('message', async (topic, mes) => {
|
|||
res.payload = {
|
||||
appInfos: data,
|
||||
};
|
||||
info = await mqttService.encrypt(seId, keyAlias, res);
|
||||
client.publish(`2/rsp/${hccId}`, JSON.stringify(res));
|
||||
break;
|
||||
case '6':
|
||||
|
@ -118,6 +125,7 @@ client.on('message', async (topic, mes) => {
|
|||
await mqttService.pushInfo({ dataList, mpList });
|
||||
console.log('家庭数据推送数据成功');
|
||||
console.log('家庭数据推送数据成功');
|
||||
info = await mqttService.encrypt(seId, keyAlias, res);
|
||||
client.publish(`2/rsp/${hccId}`, JSON.stringify(res));
|
||||
break;
|
||||
default:
|
||||
|
@ -137,6 +145,7 @@ client.on('message', async (topic, mes) => {
|
|||
client.on('reconnect', (err) => {
|
||||
console.log('mqtt 正在重连:', err);
|
||||
});
|
||||
|
||||
client.on('error', (err) => {
|
||||
console.log(`mqtt连接失败,${err}`);
|
||||
});
|
||||
|
@ -147,13 +156,6 @@ client.on('close', async () => {
|
|||
|
||||
client.subscribe(['1/req/#', '1/rsp/#']);
|
||||
|
||||
// setInterval(() => {
|
||||
// console.log('定时器开始');
|
||||
// client.publish(`2/req/111`, '成功', () => {
|
||||
// console.log('通知成功');
|
||||
// })
|
||||
// }, 5000)
|
||||
|
||||
// 查询家庭数据
|
||||
setInterval(async () => {
|
||||
console.log(`${new Date()}查询家庭数据`);
|
||||
|
@ -173,6 +175,7 @@ setInterval(async () => {
|
|||
console.log('mqtt加密失败');
|
||||
return;
|
||||
}
|
||||
// const data = await mqttService.encrypt()
|
||||
client.publish(`2/req/1234567890`, data, () => {
|
||||
// 记录reqid,放入全局缓存
|
||||
global.reqIdMap.set(reqId, null);
|
||||
|
|
|
@ -89,6 +89,6 @@ export class SecureService {
|
|||
return;
|
||||
}
|
||||
globalConfig.cookie[seId] = result.headers['set-cookie'][0];
|
||||
return secureDectyptBase64(result.data.data);
|
||||
return secureDectyptBase64(result.data.data.data);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user