Commit ca59afe3 by wangyong

feat: (messageCtl) 磐农项目 监听消息API

parent a94910ea
......@@ -94,6 +94,9 @@ class CtlBase {
}
async doexec(methodname, pobj, query, req) {
try {
if (methodname == 'message') { //易代账推送消息 headers里没有x-app-key
req.headers["xappkey"] = settings.ydzKey().appKey;
}
let xarg = await this.setContextParams(pobj, query, req);
if (xarg && xarg[0] < 0) {
return system.getResultFail(...xarg);
......
var system = require("../../../system");
const CtlBase = require("../../ctl.base");
const CryptoJS = require("crypto-js");
var crypto = require('crypto');
const settings = require("./../../../../config/settings");
/**
* @module agriculture
* @class servicelistCtl
* 服务单ctl
*/
class messageCtl extends CtlBase {
constructor() {
super("agriculture", CtlBase.getServiceName(messageCtl));
this.servicebillCtl = system.getObject("service.agriculture.servicebillCtl");
}
async message(meg) {
console.log('-----------------------------message--------------------');
console.log(meg)
const res = {};
switch (meg.type) {
case "CREATE_ACCOUNTING":
res = await this.servicebillCtl.create(meg);
break;
case "APP_TICKET":
res = await this.service.ticketProcessing(meg);
break;
}
return { "result": "success" };
}
zeropad(inputData, length = 8) {
inputData = Buffer.from(inputData, "utf8").toString("hex");
const bitLength = inputData.length * length;
if (bitLength < 256) {
for (i = bitLength; i < 256; i += length) {
inputData += 0;
}
} else if (bitLength > 256) {
while ((inputData.length * length) % 256 != 0) {
inputData += 0;
}
}
return Buffer.from(inputData, "hex").toString("utf8");
}
}
module.exports = messageCtl;
\ No newline at end of file
......@@ -5,7 +5,7 @@ var settings = require("../../../../config/settings");
const CtlBase = require("../../ctl.base");
const axios = require("axios");
const co = require("co");
const jwt = require("jsonwebtoken");
class AuthCtl extends CtlBase {
constructor() {
......@@ -50,12 +50,14 @@ class AuthCtl extends CtlBase {
}
params[specialKey] = data;
const res = await this.getRequest(url, params);
let deTokenExp = Number((jwt.decode(res.data.result.access_token).exp));
let timestamp = Math.round(new Date() / 1000);
if (res.data.code == 200) {
let ydzToken = {
access_token: res.data.result.access_token,
refresh_token: res.data.result.refresh_token
}
const addRedisResult = await this.redisClient.setWithEx('ydzToken', JSON.stringify(ydzToken), 6 * 24 * 60 * 60);
const addRedisResult = await this.redisClient.setWithEx('ydzToken', JSON.stringify(ydzToken), Number(deTokenExp - timestamp));
// const getRedisResult = await this.redisClient.get('ydzToken');
return system.getResult(addRedisResult);
} else {
......
const ServiceBase = require("../../sve.base");
const redisClient = system.getObject("util.redisClient");
const settings = require("./../../../../config/settings");
class messageService extends ServiceBase {
constructor() {
super("agriculture", ServiceBase.getDaoName(messageService));
}
async ticketProcessing(meg) {
const messageSecret = settings.ydzKey().messageSecret; // 密钥 16 位
const iv = messageSecret; // 初始向量 initial vector 16 位, messageSecret 和 iv 可以一致
//const data = CryptoJS.enc.Utf8.parse(meg.encryptMsg);
// console.log('-----------------data-------------------');
// console.log(meg.encryptMsg);
// var decrypted = CryptoJS.AES.decrypt(data, messageSecret, {
// iv: iv,
// mode: CryptoJS.mode.CBC,
// padding: CryptoJS.pad.Pkcs7
// });
// // 转换为 utf8 字符串
// console.log('---------------------------before -------------');
// console.log(decrypted);
// decrypted = CryptoJS.enc.Utf8.stringify(decrypted);
// console.log('------------------------decrypted--------------------')
// console.log(decrypted)
// console.log(iv.length);
// var clearEncoding = 'utf8'
// var cipherEncoding = 'base64'
// var cipherChunks = []
// var decipher = crypto.createCipheriv('aes-128-ecb', messageSecret, iv)
// decipher.setAutoPadding(false)
// cipherChunks.push(decipher.update(this.zeropad(meg.encryptMsg, 8), clearEncoding, cipherEncoding))
// cipherChunks.push(decipher.final(cipherEncoding))
// console.log(cipherChunks.join('').toUpperCase());
// return cipherChunks.join('').toUpperCase();
// const addRedisResult = await this.redisClient.setWithEx('APP_TICKET', JSON.stringify(obj), 2 * 60 * 60); //俩小时
// return addRedisResult;
}
}
module.exports = messageService;
\ No newline at end of file
......@@ -64,14 +64,22 @@ var settings = {
if (this.env == "dev") { //集测环境
return "https://inte-openapi.chanjet.com";
} else {
return "";
return "https://market.chanjet.com";
}
},
ydzKey: function() { //TODO: 易代账Key
if (this.env == "dev") {
return { "appKey": "qP2tgcfw", "appSecret": "D25E0435ACFB4844DB75E7E0F87B9F71" };
return {
"appKey": "qP2tgcfw",
"appSecret": "D25E0435ACFB4844DB75E7E0F87B9F71",
"messageSecret": "1234567890123456"
};
} else {
return { "appKey": "", "appSecret": "" };
return {
"appKey": "cXqZIzIl",
"appSecret": "B5AE885F55652CE9A4686F96A4CD90D8",
"messageSecret": ""
};
}
},
pmappid: 1,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment