Commit 4253173a by 宋毅

渠道增加

parent 1b34e4d4
...@@ -2,6 +2,7 @@ var childproc = require('child_process'); ...@@ -2,6 +2,7 @@ var childproc = require('child_process');
const util = require('util'); const util = require('util');
const exec = util.promisify(require('child_process').exec); const exec = util.promisify(require('child_process').exec);
const uuidv4 = require('uuid/v4'); const uuidv4 = require('uuid/v4');
class ExecClient { class ExecClient {
constructor() { constructor() {
this.cmdPostPattern = "curl --user admines:adminGSBes. -k -H 'Content-type: application/json' -d '{data}' {url}"; this.cmdPostPattern = "curl --user admines:adminGSBes. -k -H 'Content-type: application/json' -d '{data}' {url}";
...@@ -10,30 +11,33 @@ class ExecClient { ...@@ -10,30 +11,33 @@ class ExecClient {
this.cmdDataPostByTokenUserPinPattern = "curl -k -H 'Content-type: application/json' -H 'token:{tk}' -H 'userpin:{UPIN}' -d '{data}' {url}"; this.cmdDataPostByTokenUserPinPattern = "curl -k -H 'Content-type: application/json' -H 'token:{tk}' -H 'userpin:{UPIN}' -d '{data}' {url}";
} }
getUUID() { getUUID() {
var uuid = uuidv4(); var uuid = uuidv4();
var u = uuid.replace(/\-/g, ""); var u = uuid.replace(/\-/g, "");
return u; return u;
} }
async exec(cmd) { async exec(cmd) {
//await后面表达式返回的promise对象,是then的语法糖,await返回then函数的返回值 //await后面表达式返回的promise对象,是then的语法糖,await返回then函数的返回值
//异常需要try/catch自己捕获或外部catch捕获 //异常需要try/catch自己捕获或外部catch捕获
const { stdout, stderr } = await exec(cmd); const {stdout, stderr} = await exec(cmd);
return { stdout, stderr }; return {stdout, stderr};
} }
async exec2(cmd) { async exec2(cmd) {
return exec(cmd, { encoding: "base64" }); return exec(cmd, {encoding: "base64"});
} }
FetchPostCmd(subData, url) { FetchPostCmd(subData, url) {
var data = JSON.stringify(subData); var data = JSON.stringify(subData);
var cmd = this.cmdPostPattern.replace(/\{data\}/g, var cmd = this.cmdPostPattern.replace(/\{data\}/g,
data).replace(/\{url\}/g, url); data).replace(/\{url\}/g, url);
console.log(cmd); console.log(cmd,"cmd......................................");
return cmd; return cmd;
} }
FetchPushDataPostCmd(subData, url, token, requestId) { FetchPushDataPostCmd(subData, url, token, requestId) {
var requestId = requestId || this.getUUID(); var requestId = requestId || this.getUUID();
var data = JSON.stringify(subData); var data = JSON.stringify(subData);
...@@ -42,6 +46,7 @@ class ExecClient { ...@@ -42,6 +46,7 @@ class ExecClient {
console.log(cmd); console.log(cmd);
return cmd; return cmd;
} }
execDataPostByTokenUserPinCmd(subData, url, token, userPin) { execDataPostByTokenUserPinCmd(subData, url, token, userPin) {
var data = JSON.stringify(subData); var data = JSON.stringify(subData);
var cmd = this.cmdDataPostByTokenUserPinPattern.replace(/\{data\}/g, var cmd = this.cmdDataPostByTokenUserPinPattern.replace(/\{data\}/g,
...@@ -49,21 +54,43 @@ class ExecClient { ...@@ -49,21 +54,43 @@ class ExecClient {
console.log(cmd); console.log(cmd);
return cmd; return cmd;
} }
async execPost(subData, url) { async execPost(subData, url) {
let cmd = this.FetchPostCmd(subData, url); let cmd = this.FetchPostCmd(subData, url);
var result = await this.exec(cmd); var result = await this.exec(cmd);
return result; return result;
} }
/**
* 带超时时间的post请求
* @param {*} params 请求数据-json格式
* @param {*} url 请求地址
* @param {*} ContentType 请求头类型,默认application/json
* @param {*} headData 请求头内容-json格式,如:请求头中传递token,格式:{token:"9098902q849q0434q09439"}
* @param {*} timeOut 超时时间设置,单位秒
*/
async execPostTimeOut(params, url, timeOut = 5000) {
let cmd = this.FetchPostCmd(params, url);
let options = {
timeout: timeOut,
maxBuffer: 1024 * 1024 * 20
};
let result = await this.exec(cmd, options);
return result;
}
async execPushDataPost(subData, url, token, requestId) { async execPushDataPost(subData, url, token, requestId) {
let cmd = this.FetchPushDataPostCmd(subData, url, token, requestId); let cmd = this.FetchPushDataPostCmd(subData, url, token, requestId);
var result = await this.exec(cmd); var result = await this.exec(cmd);
return result; return result;
} }
async execDataPostByTokenUserPin(subData, url, token, userPin) { async execDataPostByTokenUserPin(subData, url, token, userPin) {
let cmd = this.execDataPostByTokenUserPinCmd(subData, url, token, userPin); let cmd = this.execDataPostByTokenUserPinCmd(subData, url, token, userPin);
var result = await this.exec(cmd); var result = await this.exec(cmd);
return result; return result;
} }
async execPost2(subData, url) { async execPost2(subData, url) {
let cmd = this.FetchPostCmd(subData, url); let cmd = this.FetchPostCmd(subData, url);
var result = await this.exec2(cmd); var result = await this.exec2(cmd);
...@@ -83,6 +110,7 @@ class ExecClient { ...@@ -83,6 +110,7 @@ class ExecClient {
var result = await this.exec(cmd); var result = await this.exec(cmd);
return result; return result;
} }
async execGet2(subData, url) { async execGet2(subData, url) {
let cmd = this.FetchGetCmd(subData, url); let cmd = this.FetchGetCmd(subData, url);
console.log(cmd); console.log(cmd);
...@@ -96,8 +124,8 @@ class ExecClient { ...@@ -96,8 +124,8 @@ class ExecClient {
var options = { var options = {
timeout: timeOut, timeout: timeOut,
}; };
const { stdout, stderr } = await exec(cmd, options); const {stdout, stderr} = await exec(cmd, options);
return { stdout, stderr }; return {stdout, stderr};
} }
} }
......
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