Authored by weiqingting

框架优化

... ... @@ -56,7 +56,7 @@ Controller.prototype.__define=function(method,router,view,apis,callback,clientmo
}
args.push(function (err, interfaces, names){
if (err) {
res.json({code:500,message:"接口异常"});
res.json({code:500,message:err.message||'接口异常'});
return;
}
//callback
... ...
... ... @@ -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)) {
... ... @@ -165,6 +165,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;
... ... @@ -176,10 +177,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") {
... ... @@ -333,12 +358,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);
... ...
... ... @@ -56,7 +56,7 @@ Controller.prototype.__define=function(method,router,view,apis,callback,clientmo
}
args.push(function (err, interfaces, names){
if (err) {
res.json({code:500,message:"接口异常"});
res.json({code:500,message:err.message||'接口异常'});
return;
}
//callback
... ...
... ... @@ -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)) {
... ... @@ -165,6 +165,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;
... ... @@ -176,10 +177,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") {
... ... @@ -333,12 +358,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);
... ...