Authored by weiqingting

提交

@@ -4,6 +4,7 @@ module.exports = { @@ -4,6 +4,7 @@ module.exports = {
4 queryRefundExchangeList: { 4 queryRefundExchangeList: {
5 title: '特殊商品列表数据', 5 title: '特殊商品列表数据',
6 url: '/refundExchangeAttribute/queryRefundExchangeList', 6 url: '/refundExchangeAttribute/queryRefundExchangeList',
  7 + timeout:3000,
7 params: [ 8 params: [
8 {name: 'productSkn', type: 'Number'}, 9 {name: 'productSkn', type: 'Number'},
9 {name: 'brandId', type: 'Number'}, 10 {name: 'brandId', type: 'Number'},
@@ -102,10 +102,6 @@ function __requestApi(config, apiOpt, req, callback) { @@ -102,10 +102,6 @@ function __requestApi(config, apiOpt, req, callback) {
102 for (var i in o) { 102 for (var i in o) {
103 fns.push(o[i]); 103 fns.push(o[i]);
104 } 104 }
105 - //return apiOpt.call(0, req, function (err, result) {  
106 - // req._yoheaders = req._yoheaders;  
107 - // return callback(null, result);  
108 - //});  
109 return apiOpt.apply(0, [req, function (err, result) { 105 return apiOpt.apply(0, [req, function (err, result) {
110 req._yoheaders = req._yoheaders; 106 req._yoheaders = req._yoheaders;
111 return callback(null, result); 107 return callback(null, result);
@@ -113,42 +109,46 @@ function __requestApi(config, apiOpt, req, callback) { @@ -113,42 +109,46 @@ function __requestApi(config, apiOpt, req, callback) {
113 } 109 }
114 110
115 var options = __requestOption(req, apiOpt, config, me.CONSTS); 111 var options = __requestOption(req, apiOpt, config, me.CONSTS);
116 -  
117 - Request(options, function (error, response, body) {  
118 - var _err_ = new Error();  
119 - if (error) {  
120 - console.info("Error [request"+options.url+"]:" + options.title);  
121 - console.error(error);  
122 - return callback(error, null);  
123 - }  
124 - try {  
125 - if (response && response.statusCode === 200) {  
126 - var obj = JSON.parse(body)  
127 - if (!(typeof obj == "object")) {  
128 - _err_.message = "Error[json parse@" + options.title + "--"+options.url+"]:" + body;  
129 - console.info(_err_.message);  
130 - console.error(_err_);  
131 - return callback(_err_, null);  
132 - }  
133 - } else {  
134 - _err_.message = "Error[response state @" + options.title + "--"+options.url+"]:" + response;  
135 - console.info(_err_.message);  
136 - console.error(_err_);  
137 - return callback(_err_, null);  
138 - }  
139 -  
140 - } catch (err) {  
141 - console.info("Error[response to json @" + options.title + "--"+options.url+"]");  
142 - console.info(response);  
143 - console.error(err);  
144 - return callback(err, null);  
145 - }  
146 - return callback(null, JSON.parse(body));  
147 - }); 112 + __sendRequest(options, function (result) {
  113 + return callback(null, result);
  114 + }, function (result) {
  115 + return callback(result, null);
  116 + }, {len:1}, 0, []);
  117 + //Request(options, function (error, response, body) {
  118 + // var _err_ = new Error();
  119 + // if (error) {
  120 + // console.info("Error [request"+options.url+"]:" + options.title);
  121 + // console.error(error);
  122 + // return callback(error, null);
  123 + // }
  124 + // try {
  125 + // if (response && response.statusCode === 200) {
  126 + // var obj = JSON.parse(body)
  127 + // if (!(typeof obj == "object")) {
  128 + // _err_.message = "Error[json parse@" + options.title + "--"+options.url+"]:" + body;
  129 + // console.info(_err_.message);
  130 + // console.error(_err_);
  131 + // return callback(_err_, null);
  132 + // }
  133 + // } else {
  134 + // _err_.message = "Error[response state @" + options.title + "--"+options.url+"]:" + response;
  135 + // console.info(_err_.message);
  136 + // console.error(_err_);
  137 + // return callback(_err_, null);
  138 + // }
  139 +
  140 + // } catch (err) {
  141 + // console.info("Error[response to json @" + options.title + "--"+options.url+"]");
  142 + // console.info(response);
  143 + // console.error(err);
  144 + // return callback(err, null);
  145 + // }
  146 + // return callback(null, JSON.parse(body));
  147 + //});
148 }; 148 };
149 149
150 function __requestOption(req, apiOpt, config, consts) { 150 function __requestOption(req, apiOpt, config, consts) {
151 - var method = (apiOpt.method || "POST").toLocaleUpperCase(); 151 + var method = (apiOpt.method || "POST").toLocaleUpperCase(), errs = [];
152 var data = {}; 152 var data = {};
153 if (apiOpt.params && (_.isArray(apiOpt.params) || _.isPlainObject(apiOpt.params))) { 153 if (apiOpt.params && (_.isArray(apiOpt.params) || _.isPlainObject(apiOpt.params))) {
154 if (_.isArray(apiOpt.params)) { 154 if (_.isArray(apiOpt.params)) {
@@ -168,6 +168,7 @@ function __requestOption(req, apiOpt, config, consts) { @@ -168,6 +168,7 @@ function __requestOption(req, apiOpt, config, consts) {
168 }); 168 });
169 } else 169 } else
170 if (_.isPlainObject(apiOpt.params)) { 170 if (_.isPlainObject(apiOpt.params)) {
  171 + var fns = [];
171 for (var name in apiOpt.params) { 172 for (var name in apiOpt.params) {
172 if (req.param("@" + name)) { 173 if (req.param("@" + name)) {
173 continue; 174 continue;
@@ -179,10 +180,34 @@ function __requestOption(req, apiOpt, config, consts) { @@ -179,10 +180,34 @@ function __requestOption(req, apiOpt, config, consts) {
179 if (req.param(name)) { 180 if (req.param(name)) {
180 data[name] = param.type(req.param(name)); 181 data[name] = param.type(req.param(name));
181 } 182 }
  183 + if (param.coerce && typeof param.coerce == "function") {
  184 + fns.push({ param: param,name:name});
  185 + }
  186 + }
  187 + fns.forEach(function (fn) {
  188 + var d = fn.param.coerce.call(data, req);
  189 + if (d) {
  190 + data[fn.name] = param.type(d);
  191 + }
  192 + });
  193 + //验证
  194 + for (var name in apiOpt.params) {
  195 + var param = apiOpt.params[name];
  196 + if (param.required && !data[name]) {
  197 + errs.push("params "+name + "is required!!!");
  198 + }
  199 + if (param.validator&&typeof param.validator=="function") {
  200 + var isSuc = param.validator.call(data, data[name]);
  201 +
  202 + if (isSuc===false) {
  203 + errs.push("params " +name + " validator error!!!");
  204 + }
  205 + }
182 } 206 }
183 } 207 }
184 } 208 }
185 var options = { method: method }; 209 var options = { method: method };
  210 + options.errs = errs;
186 options.title = apiOpt.title || ''; 211 options.title = apiOpt.title || '';
187 options.outobj = apiOpt.outobj || ''; 212 options.outobj = apiOpt.outobj || '';
188 if (options.method == "GET") { 213 if (options.method == "GET") {
@@ -240,6 +265,9 @@ function __requestOption(req, apiOpt, config, consts) { @@ -240,6 +265,9 @@ function __requestOption(req, apiOpt, config, consts) {
240 if (req._yoheaders) { 265 if (req._yoheaders) {
241 options.headers = _.merge(options.headers, req._yoheaders); 266 options.headers = _.merge(options.headers, req._yoheaders);
242 } 267 }
  268 + if(apiOpt.timeout){
  269 + options.timeout=apiOpt.timeout;
  270 + }
243 console.log("*************************************"); 271 console.log("*************************************");
244 console.log("Http", options.url, data, options.headers); 272 console.log("Http", options.url, data, options.headers);
245 console.log("*************************************"); 273 console.log("*************************************");
@@ -336,12 +364,20 @@ queue.prototype = { @@ -336,12 +364,20 @@ queue.prototype = {
336 }, 364 },
337 365
338 } 366 }
339 -function __sendRequest(options, success, fail,wlen,i,args) { 367 +function __sendRequest(options, success, fail, wlen, i, args) {
  368 + var _err_ = new Error(), obj;
  369 + if (options.errs instanceof Array&&options.errs.length) {
  370 + _err_.message = options.errs.join(',');
  371 + console.info("Error [options" + options.url + "]:" + options.title);
  372 + console.error(_err_);
  373 + wlen.len = 0;
  374 + fail && fail(_err_);
  375 + return;
  376 + }
340 Request(options, function (error, response, body) { 377 Request(options, function (error, response, body) {
341 if (!wlen.len) { 378 if (!wlen.len) {
342 return; 379 return;
343 } 380 }
344 - var _err_ = new Error(), obj;  
345 if (error) { 381 if (error) {
346 console.info("Error [request"+options.url+"]:" + options.title); 382 console.info("Error [request"+options.url+"]:" + options.title);
347 console.error(error); 383 console.error(error);