12345678910111213141516171819202122232425262728293031323334353637 |
- 'use strict';
- // url类
- const Url = require('url');
- module.exports = options => {
- /**
- * 控制器以及动作的获取
- *
- * @param {function} next - 中间件继续执行的方法
- * @return {void}
- */
- return function* urlParse(next) {
- this.actionName = '';
- // 获取当前控制器和动作名称
- const urlInfo = Url.parse(this.request.originalUrl, true);
- const url = urlInfo.pathname.substr(1);
- const actionInfo = url.split('/');
- if (actionInfo[0] === 'sp') {
- this.isProjectController = false;
- this.controllerName = typeof actionInfo === 'object' && actionInfo[2] !== undefined ? actionInfo[2] : '';
- this.actionName = typeof actionInfo === 'object' && actionInfo[3] !== undefined ? actionInfo[3] : '';
- } else {
- this.controllerName = typeof actionInfo === 'object' && actionInfo[0] !== undefined ? actionInfo[0] : '';
- this.actionName = typeof actionInfo === 'object' && actionInfo[1] !== undefined ? actionInfo[1] : '';
- }
- this.urlInfo = urlInfo;
- // 防止为空
- this.request.headers.referer = this.request.headers.referer === undefined ? '/dashboard' : this.request.headers.referer;
- // 清空原Message---有bug,不应该在有message未展示前被清除
- if (this.session) {
- this.session.message = null;
- }
- yield next;
- };
- };
|