Controller.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var Response = require('./lib/Response.js');
  2. var Param = require('./lib/Param.js');
  3. var util = require('util');
  4. util.inherits(Controller, Response);
  5. function Controller(req, res) {
  6. /**
  7. * 模板数据
  8. * @type {{}}
  9. * @private
  10. */
  11. this._tpl_data = {};
  12. this._startTime = new Date().getTime();
  13. this._req = req;
  14. this._res = res;
  15. this._objParam = new Param(this);
  16. Response.call(this, req, res);
  17. }
  18. /**
  19. * 检查参数
  20. * @param rules 检查规则
  21. * {
  22. * 'appId' : 'int', //int类型
  23. * 'owners' : 'array', //array类型
  24. * 'instanceIds' : 'intArr', //array类型,元素为int类型
  25. * 'instanceTypes' : 'strArr', //array类型,元素为string类型
  26. * 'deviceId' : 'int/array', //int类型或者array类型,最后转化为元素为idArr类型
  27. * 'deviceClass' : 'string/array', //string类型或者array类型,最后转化为strArr类型
  28. * 'blocks' : {type : 'int', range : '(5, 10)'} //int类型, > 5 , < 10
  29. * 'blocks2' : {type : 'int', range : '[5, 10]'} //int类型, >= 5 , <= 10
  30. * 'percent' : {type : 'float', range : '[5.1, 10.9]'} //float类型,>= 5.1 , <= 10.9
  31. * 'appName' : {type : 'string'} //string类型
  32. * 'appName2' : {type : 'string', reg : '[^0-9A-Za-z]', 'len' : '[1, 10]', 'nullable' : true} //string类型,支持正则
  33. * }
  34. * @param args 参数合集
  35. * @param exitError 遇到错误是否直接exit
  36. * @static
  37. * @return boolean 是否检查通过
  38. */
  39. Controller.prototype.checkParam = function(rules, args, exitError) {
  40. this._objParam.checkParam(rules, args, exitError);
  41. }
  42. Controller.prototype.checkParam2 = function(rules, args, exitError) {
  43. this._objParam.checkParam2(rules, args, exitError);
  44. }
  45. Controller.prototype.assign = function (key, value) {
  46. if (typeof key == 'object') {
  47. for (var k in key) {
  48. this._tpl_data[k] = key[k];
  49. }
  50. } else {
  51. this._tpl_data[key] = value;
  52. }
  53. };
  54. Controller.prototype.display = function (tpl) {
  55. tpl = tpl || CONTROLLER_NAME + '/' + ACTION_NAME;
  56. this._res.render(tpl, this._tpl_data);
  57. }
  58. module.exports = Controller;