Router.js 2.8 KB

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