url_parse.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. // url类
  3. const Url = require('url');
  4. module.exports = options => {
  5. /**
  6. * 控制器以及动作的获取
  7. *
  8. * @param {function} next - 中间件继续执行的方法
  9. * @return {void}
  10. */
  11. return function* urlParse(next) {
  12. this.actionName = '';
  13. // 获取当前控制器和动作名称
  14. const urlInfo = Url.parse(this.request.originalUrl, true);
  15. const url = urlInfo.pathname.substr(1);
  16. const actionInfo = url.split('/');
  17. if (actionInfo[0] === 'sp') {
  18. this.isProjectController = false;
  19. this.controllerName = typeof actionInfo === 'object' && actionInfo[2] !== undefined ? actionInfo[2] : '';
  20. this.actionName = typeof actionInfo === 'object' && actionInfo[3] !== undefined ? actionInfo[3] : '';
  21. } else {
  22. this.controllerName = typeof actionInfo === 'object' && actionInfo[0] !== undefined ? actionInfo[0] : '';
  23. this.actionName = typeof actionInfo === 'object' && actionInfo[1] !== undefined ? actionInfo[1] : '';
  24. }
  25. this.urlInfo = urlInfo;
  26. // 防止为空
  27. this.request.headers.referer = this.request.headers.referer === undefined ? '/dashboard' : this.request.headers.referer;
  28. // 清空原Message---有bug,不应该在有message未展示前被清除
  29. if (this.session) {
  30. this.session.message = null;
  31. }
  32. yield next;
  33. };
  34. };