Commit b7761f60 by 宋毅

tj

parent fb7f0af6
......@@ -5,6 +5,7 @@ const uuidv4 = require('uuid/v4');
const md5 = require("MD5");
class APIBase {
constructor() {
this.execClientNew = system.getObject("util.execClientNew");
this.logCtl = system.getObject("service.common.oplogSve");
this.exTime = 2 * 3600;//缓存过期时间,2小时
}
......@@ -44,20 +45,24 @@ class APIBase {
isPassResult.requestId = "";
return isPassResult;
}
var rtn = await this[methodname](pobj, query, req);
var result = await this[methodname](pobj, query, req);
if (!result.requestId) {
result.requestId = pobj.RequestId ? pobj.RequestId : pobj.requestId || req.requestId;
}
this.logCtl.createDb({
appid: pobj.appInfo ? pobj.appInfo.uapp_id : "",
appkey: pobj.appInfo ? pobj.appInfo.uapp_key : "",
requestId: req.requestId,
op: req.classname + "/" + methodname,
content: JSON.stringify(pobj),
resultInfo: JSON.stringify(rtn),
resultInfo: JSON.stringify(result),
clientIp: req.clientIp,
agent: req.uagent,
opTitle: "api服务提供方appKey:" + settings.appKey,
});
rtn.requestId = req.requestId;
return rtn;
var tmpResult = pobj.actionType && pobj.actionType.indexOf("List") < 0 ? result : { status: result.status, message: result.message, requestId: result.requestId };
this.execClientNew.execLogs("center-order-doexecMethod-reqPath:" + req.path, pobj, "center-order-doexecMethod", tmpResult, null);
return result;
} catch (e) {
console.log(e.stack, "api调用出现异常,请联系管理员..........")
this.logCtl.createDb({
......@@ -71,16 +76,17 @@ class APIBase {
agent: req.uagent,
opTitle: "api调用出现异常,请联系管理员error,appKey:" + settings.appKey,
});
this.logCtl.error({
appid: pobj.appInfo ? pobj.appInfo.uapp_id : "",
appkey: pobj.appInfo ? pobj.appInfo.uapp_key : "",
requestId: req.requestId,
op: req.classname + "/" + methodname,
content: e.stack,
clientIp: pobj.clientIp,
agent: req.uagent,
optitle: "api调用出现异常,请联系管理员",
});
// this.logCtl.error({
// appid: pobj.appInfo ? pobj.appInfo.uapp_id : "",
// appkey: pobj.appInfo ? pobj.appInfo.uapp_key : "",
// requestId: req.requestId,
// op: req.classname + "/" + methodname,
// content: e.stack,
// clientIp: pobj.clientIp,
// agent: req.uagent,
// optitle: "api调用出现异常,请联系管理员",
// });
this.execClientNew.execLogs("center-order-doexecMethod-reqPath异常:" + req.path, pobj, "center-channel-doexecMethod", tmpResult, null);
var rtnerror = system.getResultFail(-200, "出现异常,error:" + e.stack);
rtnerror.requestId = req.requestId;
return rtnerror;
......
const childproc = require('child_process');
const system = require("../system");
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const settings = require("../../../app/config/settings");
const axios = require('axios');
const moment = require('moment');
const uuid = require('uuid');
class ExecClientNew {
constructor() {
this.cmdGetPattern = "curl -G -X GET '{url}'";
this.cmdPostPattern = "curl -k -H 'Content-type: application/json' -d '{data}' {url}";
// this.cmdPushDataPostPattern = "curl -k -H 'Content-type: application/json' -H 'token:{tk}' -H 'appkey:{appkey}' -d '{data}' {url}";
}
/**
* 带超时时间的post请求
* @param {*} execFile 执行文件
* @param {*} params 请求数据-json格式
* @param {*} url 请求地址
* @param {*} ContentType 请求头类型,默认application/json
* @param {*} headData 请求头内容-json格式,如:请求头中传递token,格式:{token:"9098902q849q0434q09439"}
* @param {*} timeOut 超时时间设置,单位秒
* @param {*} req 请求信息
*/
async execPostTimeOutByBusiness(execFile, params, url, ContentType, headData, timeOut = 60, req) {
var rtn = null;
var reqResult = null;
var requestId = null;
if (req && req.body && (req.body.RequestId || req.body.requestId)) {
requestId = req.body.RequestId || req.body.requestId;
} else {
if (req && req.query && (req.query.RequestId || req.query.requestId)) {
requestId = req.query.RequestId || req.query.requestId;
}
}
try {
if (settings.env === "localhost" || settings.env === "dev") {
var headers = {
'Content-type': 'application/json'
}
if (headData) {
var headDataKeys = Object.keys(headData);
if (headDataKeys.length > 0) {
for (let index = 0; index < headDataKeys.length; index++) {
const indexKey = headDataKeys[index];
var headValue = headData[indexKey];
if (indexKey && headValue) {
headers[indexKey] = headValue;
}
}
}
}
axios.default.timeout = timeOut;
let result = await axios({
// headers: {'Content-Type': 'application/x-www-form-urlencoded'},
headers: headers,
method: 'POST',
url: url,
data: JSON.stringify(params)
});
if (result.status == 200) {
reqResult = system.getResultSuccess(result.data);
} else {
reqResult = system.getResult(null, "执行execPostTimeOutByBusiness存在错误");
}
reqResult.requestId = requestId || uuid.v1();
if (result.headers) {
delete result["headers"];
}
if (result.request) {
delete result["request"];
}
if (result.config) {
delete result["config"];
}
params.reqUrl = url;
this.execLogs(execFile + "执行execPostTimeOutByBusiness,errorInfo信息为请求的返回结果", params, params.identifyCode, reqResult, result);
return reqResult;
}
//方式二
rtn = await this.execPostTimeOut(params, url, ContentType, headData, timeOut);
params.reqUrl = url;
if (!rtn || !rtn.stdout) {
this.execLogs(execFile + "执行execPostTimeOutByBusiness返回的数据为空", params, params.identifyCode, null, null);
reqResult = system.getResult(null, "execPostTimeOut data is empty");
} else {
var result = JSON.parse(rtn.stdout);
reqResult = system.getResultSuccess(result);
}
reqResult.requestId = requestId || uuid.v1();
this.execLogs(execFile + "执行execPostTimeOutByBusiness,errorInfo信息为请求的返回结果", params, params.identifyCode, reqResult, rtn);
return reqResult;
} catch (error) {
console.log("执行execPostByTimeOut存在异常", error.stack);
reqResult = system.getResultFail(-200, execFile + "执行execPostByTimeOut存在异常");
reqResult.requestId = requestId || uuid.v1();
this.execLogs(execFile + "执行execPostByTimeOut存在异常", params, params.identifyCode, reqResult, error.stack);
return reqResult;
}
}
/**
* 记录日志信息
* @param {*} opTitle 操作的标题
* @param {*} params 参数
* @param {*} identifyCode 业务标识
* @param {*} resultInfo 返回结果
* @param {*} errorInfo 错误信息
*/
async execLogs(opTitle, params, identifyCode, resultInfo, errorInfo) {
var reqUrl = settings.opNewLogUrl();
params = typeof params === 'object' ? JSON.stringify(params) : params || "";
resultInfo = typeof resultInfo === 'object' ? JSON.stringify(resultInfo) : resultInfo || "";
errorInfo = typeof errorInfo === 'object' ? JSON.stringify(errorInfo) : errorInfo || "";
var tmpParams = {
opTitle: opTitle || "",
identifyCode: identifyCode || "",
messageBody: params,
resultInfo: resultInfo || "",
errorInfo: errorInfo || "",
requestId: resultInfo ? resultInfo.requestId || "" : "",
created_at: moment().format("YYYY-MM-DD HH:mm:ss:SSS"),
timestamp: Date.now()
}
this.execPostTimeOut(tmpParams, reqUrl, 'application/json', null, 20);
}
/**
* get请求
* @param {*} params 提交的数据-格式JSON
* @param {*} url
* @param {*} headData 请求信息,JSON格式
*/
async execGet(params, url, headData) {
let cmd = this.FetchGetCmd(params, url, headData);
var result = await this.exec(cmd);
return result;
}
/**
*
* @param {*} params 提交的数据JSON格式
* @param {*} url
* @param {*} headData 请求信息,JSON格式
* @param {*} timeOut 超时时间
*/
async execGetTimeOut(params, url, headData, timeOut = 5000) {
//timeOut,单位是毫秒
let cmd = this.FetchGetCmd(params, url, headData);
var options = {
timeout: timeOut,
};
const { stdout, stderr } = await exec(cmd, options);
return { stdout, stderr };
}
/**
* 带超时时间的post请求
* @param {*} params 请求数据-json格式
* @param {*} url 请求地址
* @param {*} ContentType 请求头类型,默认application/json
* @param {*} headData 请求头内容-json格式,如:请求头中传递token,格式:{token:"9098902q849q0434q09439"}
*/
async execPost(params, url, ContentType, headData) {
if (!ContentType) {
ContentType = "application/json";
}
let cmd = this.FetchPostCmd(params, url, ContentType, headData);
var options = {
maxBuffer: 1024 * 1024 * 15
};
var result = await this.exec(cmd, options, headData);
return result;
}
/**
* 带超时时间的post请求
* @param {*} params 请求数据-json格式
* @param {*} url 请求地址
* @param {*} ContentType 请求头类型,默认application/json
* @param {*} headData 请求头内容-json格式,如:请求头中传递token,格式:{token:"9098902q849q0434q09439"}
* @param {*} timeOut 超时时间设置,单位秒
*/
async execPostTimeOut(params, url, ContentType, headData, timeOut = 5000) {
if (!ContentType) {
ContentType = "application/json";
}
let cmd = this.FetchPostCmd(params, url, ContentType, headData);
var options = {
timeout: timeOut,
maxBuffer: 1024 * 1024 * 15
};
var result = await this.exec(cmd, options);
return result;
}
//--------------------------------------------------辅助方法start-----------------
async exec(cmd) {
//await后面表达式返回的promise对象,是then的语法糖,await返回then函数的返回值
//异常需要try/catch自己捕获或外部catch捕获
const { stdout, stderr } = await exec(cmd);
return { stdout, stderr };
}
FetchGetCmd(params, url, headData) {
var cmd = this.cmdGetPattern.replace(
/\{data\}/g, params).replace(/\{url\}/g, url);
return cmd;
}
FetchPostCmd(params, url, ContentType, headData) {
if (!ContentType) {
ContentType = "application/json";
}
var data = null;
if (typeof params === 'object') {
// 声明cache变量,便于匹配是否有循环引用的情况
var cache = [];
data = JSON.stringify(params, function (key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// 移除
return;
}
// 收集所有的值
cache.push(value);
}
return value;
});
cache = null; // 清空变量,便于垃圾回收机制回收
} else {
data = params;
}
var cmdStr = "curl --user admines:adminGSBes. -k -H 'Content-type:" + ContentType + "'";
if (headData) {
var headDataKeys = Object.keys(headData);
if (headDataKeys.length > 0) {
for (let index = 0; index < headDataKeys.length; index++) {
const indexKey = headDataKeys[index];
var headValue = headData[indexKey];
if (indexKey && headValue) {
cmdStr = cmdStr + " -H '" + indexKey + ":" + headValue + "'";
}
}
}
}
cmdStr = cmdStr + " -d '" + data + "' " + url;
console.log(cmdStr, ":cmdStr.................");
return cmdStr;
}
/**
* 返回20位业务订单号
* @param {*} prefix 业务前缀
*/
async getBusUid(prefix) {
prefix = (prefix || "");
if (prefix) {
prefix = prefix.toUpperCase();
}
var prefixlength = prefix.length;
var subLen = 8 - prefixlength;
var uidStr = "";
if (subLen > 0) {
uidStr = await this.getUidInfo(subLen, 60);
}
var timStr = moment().format("YYYYMMDDHHmm");
return prefix + timStr + uidStr;
}
/**
* 返回指定长度的字符串
* @param {*} len 返回长度
* @param {*} radix 参与计算的长度,最大为62
*/
async getUidInfo(len, radix) {
var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');//长度62,到yz长度为长36
var uuid = [], i;
radix = radix || chars.length;
if (len) {
for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
} else {
var r;
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
uuid[14] = '4';
for (i = 0; i < 36; i++) {
if (!uuid[i]) {
r = 0 | Math.random() * 16;
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
}
}
}
return uuid.join('');
}
//--------------------------------------------------辅助方法end-----------------
}
module.exports = ExecClientNew;
......@@ -20,6 +20,13 @@ var settings = {
usertimeout: 3600,//单位秒
basepath: path.normalize(path.join(__dirname, '../..')),
port: process.env.NODE_PORT || 4011,
//记录日志地址
opNewLogUrl() {
if (this.env == 'dev') {
return "http://43.247.184.94:7200/center_order_req_log/_doc?pretty";
}
return "http://43.247.184.94:7200/center_order_req_log/_doc?pretty";
},
reqTransferurl: function () {
if (this.env == "dev") {
return "http://192.168.18.61:3003/";
......@@ -42,15 +49,15 @@ var settings = {
return "http://gsb.qifu.gongsibao.com:4012/";
}
},
push360Url:function(){
if(this.env == 'dev'){
push360Url: function () {
if (this.env == 'dev') {
return "http://180.163.239.98:38086/";
} else {
return "http://180.163.239.98:38086/";
}
},
auth360Url:function(){
if(this.env == 'dev'){
auth360Url: function () {
if (this.env == 'dev') {
return "https://oauth2.e.360.cn/site/token";
} else {
return "https://oauth2.e.360.cn/site/token";
......@@ -64,7 +71,7 @@ var settings = {
return 1;
},
},
homePage: function () {
if (this.env == "dev") {
var localsettings = require("./localsettings");
......@@ -114,8 +121,8 @@ var settings = {
};
}
},
addArr: function() {
return ["南海诸岛", "北京", "天津", "上海", "重庆", "河北", "河南", "云南", "辽宁", "黑龙江", "湖南", "安徽", "山东", "新疆", "江苏", "浙江", "江西", "湖北", "广西", "甘肃", "山西", "内蒙古", "陕西", "吉林", "福建", "贵州", "广东", "青海", "西藏", "四川", "宁夏", "海南", "台湾", "香港", "澳门"]
addArr: function () {
return ["南海诸岛", "北京", "天津", "上海", "重庆", "河北", "河南", "云南", "辽宁", "黑龙江", "湖南", "安徽", "山东", "新疆", "江苏", "浙江", "江西", "湖北", "广西", "甘肃", "山西", "内蒙古", "陕西", "吉林", "福建", "贵州", "广东", "青海", "西藏", "四川", "宁夏", "海南", "台湾", "香港", "澳门"]
}
};
module.exports = settings;
......@@ -15,6 +15,7 @@
"after": "^0.8.2",
"ali-oss": "^4.12.2",
"aliyun-api-gateway": "^1.1.6",
"axios": "^0.21.0",
"babel-polyfill": "^6.26.0",
"base64id": "^1.0.0",
"bluebird": "^3.5.1",
......@@ -57,4 +58,4 @@
"element-theme": "^2.0.1",
"element-theme-chalk": "^2.4.0"
}
}
\ 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