base_controller.js 980 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * 控制器基类
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/6/29
  6. * @version
  7. */
  8. import Moment from "moment";
  9. import Url from "url";
  10. class BaseController {
  11. /**
  12. * 页面title
  13. *
  14. * @var string
  15. */
  16. title = '';
  17. /**
  18. * 构造函数
  19. *
  20. * @return {void}
  21. */
  22. constructor() {
  23. if (new.target === BaseController) {
  24. throw new Error('BaseController不能实例化,只能继承使用。');
  25. }
  26. }
  27. /**
  28. * 初始化函数
  29. *
  30. * @param {object} request
  31. * @param {object} response
  32. * @param {function} next
  33. * @return {void}
  34. */
  35. init(request, response, next) {
  36. response.locals.title = 'test';
  37. // moment工具
  38. response.locals.moment = Moment;
  39. // url相关数据
  40. let urlInfo = Url.parse(request.originalUrl, true);
  41. response.locals.urlQuery = JSON.stringify(urlInfo.query);
  42. next();
  43. }
  44. }
  45. export default BaseController;