修改代码格式;

数据库查询加 lean 判断;
其它内容微调。
This commit is contained in:
liangtongchuan
2020-08-27 20:01:35 +08:00
parent 940879016f
commit c53db8634d
17 changed files with 710 additions and 323 deletions

View File

@@ -5,14 +5,14 @@ const isJSON = require('koa-is-json');
function aesEncrypt(data, key, iv) {
const cipher = crypto.createCipheriv('aes-192-cbc', key, iv);
var crypted = cipher.update(data, 'utf8', 'hex');
let crypted = cipher.update(data, 'utf8', 'hex');
crypted += cipher.final('hex');
return crypted;
}
function aesDecrypt(data, key, iv) {
const decipher = crypto.createDecipheriv('aes-192-cbc', key, iv);
var decrypted = decipher.update(data, 'hex', 'utf8');
let decrypted = decipher.update(data, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
@@ -20,29 +20,31 @@ function aesDecrypt(data, key, iv) {
module.exports = options => {
return async function parmsDecode(ctx: Context, next) {
if (options.threshold && ctx.length < options.threshold) return;
let reqBody = ctx.request.body;
if (!reqBody.data) return;
const reqBody = ctx.request.body;
if (isJSON(reqBody)) {
let encodeStr = aesEncrypt(JSON.stringify(reqBody), ENCRYPT_KEY, ENCRYPT_IV);
const encodeStr = aesEncrypt(JSON.stringify(reqBody), ENCRYPT_KEY, ENCRYPT_IV);
console.log(`encoded str: ${encodeStr}`);
}
if (!reqBody.data) return;
let decodeStr = aesDecrypt(reqBody.data, ENCRYPT_KEY, ENCRYPT_IV);
const decodeStr = aesDecrypt(reqBody.data, ENCRYPT_KEY, ENCRYPT_IV);
ctx.logger.debug('decoded str:', decodeStr);
try {
ctx.request.body = JSON.parse(decodeStr);
console.log('req body', ctx.request.body);
} catch (e) {
console.error('parms parse err');
}
await next();
let resBody = ctx.body;
const resBody = ctx.body;
console.log('return value:', resBody);
if (isJSON(resBody)) {
ctx.body = {result: aesEncrypt(JSON.stringify(resBody), ENCRYPT_KEY, ENCRYPT_IV)};
ctx.body = { result: aesEncrypt(JSON.stringify(resBody), ENCRYPT_KEY, ENCRYPT_IV) };
} else {
ctx.body = {result: aesEncrypt(JSON.stringify({status: 3, data: 'internal err'}), ENCRYPT_KEY, ENCRYPT_IV)};
ctx.body = { result: aesEncrypt(JSON.stringify({ status: 3, data: 'internal err' }), ENCRYPT_KEY, ENCRYPT_IV) };
}
}
}
};
};