Router.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. var php = require('phpjs');
  2. function Router(url) {
  3. //var parts = url.match("http[s]*://[^/]+/([^/]+)/([^/?]+)");
  4. var parts = url.match("(http[s]*://[^/]+)?/([^/]+)/([^/?]+)");
  5. if (parts && parts.length >= 4) {
  6. this.controllerName = parts[2];
  7. this.actionName = parts[3];
  8. } else {
  9. this.controllerName = 'default';
  10. this.actionName = 'index';
  11. }
  12. global['CONTROLLER_NAME'] = this.controllerName;
  13. global['ACTION_NAME'] = this.actionName;
  14. }
  15. Router.prototype.getControllerName = function() {
  16. return this.controllerName;
  17. };
  18. Router.prototype.getActionName = function() {
  19. return this.actionName;
  20. }
  21. Router.prototype.getController = function() {
  22. var controllerName = php.ucfirst(this.controllerName) ;
  23. var objController;
  24. try {
  25. objController = require('../../controllers/' + controllerName + 'Controller.js');
  26. return objController;
  27. } catch(ex) {
  28. console.error(ex);
  29. return null;
  30. }
  31. }
  32. Router.prototype.getFullActionName = function () {
  33. return 'action' + php.ucfirst(this.actionName);
  34. }
  35. /**
  36. * 文档处理方法
  37. * @param type 查询对象类型【module, class, func】
  38. */
  39. Router.prototype.genDoc = function(type) {
  40. var className = this.controllerName,
  41. funcName = this.getFullActionName();
  42. className = className.charAt(0).toUpperCase() + className.slice(1);
  43. var Doc = require('../Doc.js');
  44. var Controller = require('../Controller.js');
  45. var doc = new Doc();
  46. var docController = new Controller;
  47. switch (type) {
  48. case "module" :
  49. var classInfos = doc.getClassInfos(__dirname + '/../../controllers');
  50. docController.assign({classInfos : classInfos});
  51. docController.display('doc');
  52. break;
  53. case "class" :
  54. var classInfo = doc.getClassInfo(className);
  55. docController.assign({classInfos : classInfo});
  56. docController.display('doc');
  57. break;
  58. case "func" :
  59. var params = doc.getFuncInfo(className, funcName) || {
  60. params : {},
  61. rules : {}
  62. };
  63. var args = {
  64. "__getRules" : true,
  65. "__params" : params
  66. }
  67. var Controller2 = require('../../controllers/' + className + 'Controller.js');
  68. var _controller = new Controller2;
  69. _controller[funcName].apply(this, [args]);
  70. break;
  71. default :
  72. var msg = 'can not find doc type ' + type;
  73. Response.error(CODE_NOT_EXIST_INTERFACE, msg);
  74. }
  75. return;
  76. }
  77. module.exports = Router;