Commit d162153b by 宋毅

百度加日志

parent 045a1ff9
...@@ -3,108 +3,111 @@ const sha256 = require('sha256'); ...@@ -3,108 +3,111 @@ const sha256 = require('sha256');
const system = require("../system"); const system = require("../system");
var settings = require("../../config/settings"); var settings = require("../../config/settings");
const AppServiceBase = require("../service/app.base"); const AppServiceBase = require("../service/app.base");
class baiduClient extends AppServiceBase{
constructor() { class baiduClient extends AppServiceBase {
super(); constructor() {
this.pushlogSve = system.getObject("service.common.pushlogSve"); super();
this.execClient = system.getObject("util.execClient"); this.pushlogSve = system.getObject("service.common.pushlogSve");
this.baiduClientParams = settings.baiduClientParams(); this.execClient = system.getObject("util.execClient");
} this.baiduClientParams = settings.baiduClientParams();
//2020 1027 laolan 调用百度网关
async baiduPost( path, actionBody) {
console.log('actionBody++',actionBody)
let domain = this.baiduClientParams.domain;
let url = domain+path;
let version ="bce-auth-v1";
let accessKey = this.baiduClientParams.accessKey;
let secretKey = this.baiduClientParams.secretKey;
let timestamp = new Date().toISOString()
let site = timestamp.lastIndexOf(".");
timestamp = timestamp.substring(0,site)+"Z"
let signedHeaders = "host";
let extime = 1800;
let canonicalHeaders = "host:"+this.baiduClientParams.host;
//中间结果1:规范化请求和前缀字符串
var canonicalRequest = null
var pathParams = path.split("?");
if(pathParams && pathParams.length > 1){
canonicalRequest = "POST" + "\n" + pathParams[0] + "\n" + pathParams[1] + "\n" + canonicalHeaders;
}else{
canonicalRequest = "POST" + "\n" + path + "\n" + "\n" + canonicalHeaders;
}
// let canonicalRequest = "POST" + "\n" + path + "\n" + "\n" + canonicalHeaders;
console.log('canonicalRequest+++++++',canonicalRequest)
//authStringPrefix(前缀字符串,由除sk字段外的签名信息生成)
let authStringPrefix = version+"/"+accessKey+"/"+timestamp+"/"+extime;
//中间结果2:派生签名密钥 signingKey
let signingKey = crypto.createHmac('sha256', secretKey)
.update(authStringPrefix)
.digest('hex');
//中间结果3:签名摘要 signature
let signature = crypto.createHmac('sha256', signingKey)
.update(canonicalRequest)
.digest('hex');
//最终结果:认证字符串 authorization
let authorization = authStringPrefix+"/"+signedHeaders+"/"+signature;
var baiduObj={
authorization:authorization,
data:actionBody
} }
var rtn = await this.execClient.execBaiduPost(baiduObj, url);
if (!rtn || !rtn.stdout) { //2020 1027 laolan 调用百度网关
this.execClientNew.execLogs(`baiduClient-opBaiduClientPost-result:url=${url}`, actionBody, "center-channel-opBaiduClientPost", null, rtn); async baiduPost(path, actionBody) {
return system.getResult(null, rtn); console.log('actionBody++', actionBody)
let domain = this.baiduClientParams.domain;
let url = domain + path;
let version = "bce-auth-v1";
let accessKey = this.baiduClientParams.accessKey;
let secretKey = this.baiduClientParams.secretKey;
let timestamp = new Date().toISOString()
let site = timestamp.lastIndexOf(".");
timestamp = timestamp.substring(0, site) + "Z"
let signedHeaders = "host";
let extime = 1800;
let canonicalHeaders = "host:" + this.baiduClientParams.host;
//中间结果1:规范化请求和前缀字符串
var canonicalRequest = null
var pathParams = path.split("?");
if (pathParams && pathParams.length > 1) {
canonicalRequest = "POST" + "\n" + pathParams[0] + "\n" + pathParams[1] + "\n" + canonicalHeaders;
} else {
canonicalRequest = "POST" + "\n" + path + "\n" + "\n" + canonicalHeaders;
}
// let canonicalRequest = "POST" + "\n" + path + "\n" + "\n" + canonicalHeaders;
console.log('canonicalRequest+++++++', canonicalRequest)
//authStringPrefix(前缀字符串,由除sk字段外的签名信息生成)
let authStringPrefix = version + "/" + accessKey + "/" + timestamp + "/" + extime;
//中间结果2:派生签名密钥 signingKey
let signingKey = crypto.createHmac('sha256', secretKey)
.update(authStringPrefix)
.digest('hex');
//中间结果3:签名摘要 signature
let signature = crypto.createHmac('sha256', signingKey)
.update(canonicalRequest)
.digest('hex');
//最终结果:认证字符串 authorization
let authorization = authStringPrefix + "/" + signedHeaders + "/" + signature;
var baiduObj = {
authorization: authorization,
data: actionBody
}
var rtn = await this.execClient.execBaiduPost(baiduObj, url);
this.execClientNew.execLogs(`baiduClient-baiduPost-result:url=${url}`, baiduObj, "center-channel-opBaiduClientPost", rtn, null);
if (!rtn || !rtn.stdout) {
this.execClientNew.execLogs(`baiduClient-opBaiduClientPost-result:url=${url}`, actionBody, "center-channel-opBaiduClientPost", null, rtn);
return system.getResult(null, rtn);
}
var result = JSON.parse(rtn.stdout);
return result;
} }
var result = JSON.parse(rtn.stdout);
return result;
}
//百度接口 2020-10-27 laolan //百度接口 2020-10-27 laolan
async baiduReqbyget(obj, cbk) { async baiduReqbyget(obj, cbk) {
var path = obj.path; var path = obj.path;
var reqbody = obj.reqbody; var reqbody = obj.reqbody;
try { try {
var res = await this.baiduPost( path, reqbody, { var res = await this.baiduPost(path, reqbody, {
timeout: 10000, // default 3000 ms 修改3000为10000,原因ConfirmIcpIntention BizIds 超过5条会超时 timeout: 10000, // default 3000 ms 修改3000为10000,原因ConfirmIcpIntention BizIds 超过5条会超时
formatAction: true, // default true, format the action to Action formatAction: true, // default true, format the action to Action
formatParams: true, // default true, format the parameter name to first letter upper case formatParams: true, // default true, format the parameter name to first letter upper case
method: 'POST', // set the http method, default is GET method: 'POST', // set the http method, default is GET
headers: { headers: {
host:this.baiduClientParams.host host: this.baiduClientParams.host
}, // set the http request headers }, // set the http request headers
}); });
if(res.status == 0){ if (res.status == 0) {
await this.disposePushResult(obj, res, "baidu->pushBusiness2Baidu->result", ""); await this.disposePushResult(obj, res, "baidu->pushBusiness2Baidu->result", "");
}else{ } else {
await this.disposePushResultFail(obj, res, "badiu->pushBusiness2Baidu->catchError", this.pushlogFailType.FAILLOG); await this.disposePushResultFail(obj, res, "badiu->pushBusiness2Baidu->catchError", this.pushlogFailType.FAILLOG);
} }
// this.pushlogSve.createDb({ // this.pushlogSve.createDb({
// op: "new-baiduReqbyget", // op: "new-baiduReqbyget",
// content: JSON.stringify(obj),//推送的参数信息 // content: JSON.stringify(obj),//推送的参数信息
// resultInfo: JSON.stringify(res), // resultInfo: JSON.stringify(res),
// returnType: '1', // returnType: '1',
// logLevel: '1', // logLevel: '1',
// opTitle: "new-baiduReqbyget推送百度信息返回成功" // opTitle: "new-baiduReqbyget推送百度信息返回成功"
// }); // });
return system.getResultSuccess(res); return system.getResultSuccess(res);
} catch (e) { } catch (e) {
await this.disposePushResultFail(obj, e, "badiu->pushBusiness2Baidu->catchError", this.pushlogFailType.FAILLOG); await this.disposePushResultFail(obj, e, "badiu->pushBusiness2Baidu->catchError", this.pushlogFailType.FAILLOG);
// this.pushlogSve.createFailLogDb({ // this.pushlogSve.createFailLogDb({
// appid: obj.appInfo ? obj.appInfo.uapp_id || "" : "", // appid: obj.appInfo ? obj.appInfo.uapp_id || "" : "",
// appkey: obj.appInfo ? obj.appInfo.uapp_key || "" : "", // appkey: obj.appInfo ? obj.appInfo.uapp_key || "" : "",
// requestId: obj.requestId || "", // requestId: obj.requestId || "",
// content: JSON.stringify(obj),//推送的参数信息 // content: JSON.stringify(obj),//推送的参数信息
// resultInfo: "error:" + JSON.stringify(e), // resultInfo: "error:" + JSON.stringify(e),
// clientIp: obj.clientIp || "", // clientIp: obj.clientIp || "",
// failType: 1, // failType: 1,
// opTitle: "new-baiduReqbyget推送百度信息返回异常", // opTitle: "new-baiduReqbyget推送百度信息返回异常",
// pushNumber: obj.pushNumber || 1 // pushNumber: obj.pushNumber || 1
// }); // });
return system.getResultFail(-200, "出现异常,error:" + e.stack); return system.getResultFail(-200, "出现异常,error:" + e.stack);
}
} }
}
} }
module.exports = baiduClient; module.exports = baiduClient;
// var task = new baiduClient(); // var task = new baiduClient();
// console.log(task.baiduClientParams,"11111111111111111"); // console.log(task.baiduClientParams,"11111111111111111");
\ No newline at end of file
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