Authored by weiqingting

提交

... ... @@ -4,6 +4,7 @@ module.exports = {
queryRefundExchangeList: {
title: '特殊商品列表数据',
url: '/refundExchangeAttribute/queryRefundExchangeList',
timeout:3000,
params: [
{name: 'productSkn', type: 'Number'},
{name: 'brandId', type: 'Number'},
... ...
... ... @@ -102,10 +102,6 @@ function __requestApi(config, apiOpt, req, callback) {
for (var i in o) {
fns.push(o[i]);
}
//return apiOpt.call(0, req, function (err, result) {
// req._yoheaders = req._yoheaders;
// return callback(null, result);
//});
return apiOpt.apply(0, [req, function (err, result) {
req._yoheaders = req._yoheaders;
return callback(null, result);
... ... @@ -113,42 +109,46 @@ function __requestApi(config, apiOpt, req, callback) {
}
var options = __requestOption(req, apiOpt, config, me.CONSTS);
Request(options, function (error, response, body) {
var _err_ = new Error();
if (error) {
console.info("Error [request"+options.url+"]:" + options.title);
console.error(error);
return callback(error, null);
}
try {
if (response && response.statusCode === 200) {
var obj = JSON.parse(body)
if (!(typeof obj == "object")) {
_err_.message = "Error[json parse@" + options.title + "--"+options.url+"]:" + body;
console.info(_err_.message);
console.error(_err_);
return callback(_err_, null);
}
} else {
_err_.message = "Error[response state @" + options.title + "--"+options.url+"]:" + response;
console.info(_err_.message);
console.error(_err_);
return callback(_err_, null);
}
} catch (err) {
console.info("Error[response to json @" + options.title + "--"+options.url+"]");
console.info(response);
console.error(err);
return callback(err, null);
}
return callback(null, JSON.parse(body));
});
__sendRequest(options, function (result) {
return callback(null, result);
}, function (result) {
return callback(result, null);
}, {len:1}, 0, []);
//Request(options, function (error, response, body) {
// var _err_ = new Error();
// if (error) {
// console.info("Error [request"+options.url+"]:" + options.title);
// console.error(error);
// return callback(error, null);
// }
// try {
// if (response && response.statusCode === 200) {
// var obj = JSON.parse(body)
// if (!(typeof obj == "object")) {
// _err_.message = "Error[json parse@" + options.title + "--"+options.url+"]:" + body;
// console.info(_err_.message);
// console.error(_err_);
// return callback(_err_, null);
// }
// } else {
// _err_.message = "Error[response state @" + options.title + "--"+options.url+"]:" + response;
// console.info(_err_.message);
// console.error(_err_);
// return callback(_err_, null);
// }
// } catch (err) {
// console.info("Error[response to json @" + options.title + "--"+options.url+"]");
// console.info(response);
// console.error(err);
// return callback(err, null);
// }
// return callback(null, JSON.parse(body));
//});
};
function __requestOption(req, apiOpt, config, consts) {
var method = (apiOpt.method || "POST").toLocaleUpperCase();
var method = (apiOpt.method || "POST").toLocaleUpperCase(), errs = [];
var data = {};
if (apiOpt.params && (_.isArray(apiOpt.params) || _.isPlainObject(apiOpt.params))) {
if (_.isArray(apiOpt.params)) {
... ... @@ -168,6 +168,7 @@ function __requestOption(req, apiOpt, config, consts) {
});
} else
if (_.isPlainObject(apiOpt.params)) {
var fns = [];
for (var name in apiOpt.params) {
if (req.param("@" + name)) {
continue;
... ... @@ -179,10 +180,34 @@ function __requestOption(req, apiOpt, config, consts) {
if (req.param(name)) {
data[name] = param.type(req.param(name));
}
if (param.coerce && typeof param.coerce == "function") {
fns.push({ param: param,name:name});
}
}
fns.forEach(function (fn) {
var d = fn.param.coerce.call(data, req);
if (d) {
data[fn.name] = param.type(d);
}
});
//验证
for (var name in apiOpt.params) {
var param = apiOpt.params[name];
if (param.required && !data[name]) {
errs.push("params "+name + "is required!!!");
}
if (param.validator&&typeof param.validator=="function") {
var isSuc = param.validator.call(data, data[name]);
if (isSuc===false) {
errs.push("params " +name + " validator error!!!");
}
}
}
}
}
var options = { method: method };
options.errs = errs;
options.title = apiOpt.title || '';
options.outobj = apiOpt.outobj || '';
if (options.method == "GET") {
... ... @@ -240,6 +265,9 @@ function __requestOption(req, apiOpt, config, consts) {
if (req._yoheaders) {
options.headers = _.merge(options.headers, req._yoheaders);
}
if(apiOpt.timeout){
options.timeout=apiOpt.timeout;
}
console.log("*************************************");
console.log("Http", options.url, data, options.headers);
console.log("*************************************");
... ... @@ -336,12 +364,20 @@ queue.prototype = {
},
}
function __sendRequest(options, success, fail,wlen,i,args) {
function __sendRequest(options, success, fail, wlen, i, args) {
var _err_ = new Error(), obj;
if (options.errs instanceof Array&&options.errs.length) {
_err_.message = options.errs.join(',');
console.info("Error [options" + options.url + "]:" + options.title);
console.error(_err_);
wlen.len = 0;
fail && fail(_err_);
return;
}
Request(options, function (error, response, body) {
if (!wlen.len) {
return;
}
var _err_ = new Error(), obj;
if (error) {
console.info("Error [request"+options.url+"]:" + options.title);
console.error(error);
... ...