Authored by 周奇琪

实现子级页面,保留父级菜单选项展开和选中,实现方法,采用referer去掉了客户端的实现

@@ -32,12 +32,12 @@ $('.leftpanel .nav .parent>a').click(function() { @@ -32,12 +32,12 @@ $('.leftpanel .nav .parent>a').click(function() {
32 }); 32 });
33 33
34 34
35 -$(".leftpanel .nav .children a").each(function() {  
36 - var attr = $(this).attr('href');  
37 -  
38 - if (attr == window.location.pathname + window.location.search) {  
39 - $(this).parents(".children").show();  
40 - $(this).parent("li").addClass("active");  
41 - $(this).parents(".parent").addClass("parent-focus");  
42 - }  
43 -});  
  35 +// $(".leftpanel .nav .children a").each(function() {
  36 +// var attr = $(this).attr('href');
  37 +
  38 +// if (attr == window.location.pathname + window.location.search) {
  39 +// $(this).parents(".children").show();
  40 +// $(this).parent("li").addClass("active");
  41 +// $(this).parents(".parent").addClass("parent-focus");
  42 +// }
  43 +// });
@@ -54,11 +54,14 @@ module.exports = function(req, res, next) { @@ -54,11 +54,14 @@ module.exports = function(req, res, next) {
54 if(guestAccessList[method+":"+path]||req.session.user) { 54 if(guestAccessList[method+":"+path]||req.session.user) {
55 //判断是否已存在appendData 55 //判断是否已存在appendData
56 if(!res.appendData) { 56 if(!res.appendData) {
57 - res.appendData = req.session.user; 57 + res.appendData = _.cloneDeep(req.session.user);
58 } else { 58 } else {
59 - res.appendData = _.merge(res.appendData,req.session.user); 59 + res.appendData = _.merge({},res.appendData,req.session.user);
60 } 60 }
61 61
  62 + //添加当前菜单激活
  63 + addActiveMenu(req,res);
  64 +
62 //添加管理员信息到http头 65 //添加管理员信息到http头
63 if(req.session.user) { 66 if(req.session.user) {
64 appendAdminInfo(req,req.session.user.auth); 67 appendAdminInfo(req,req.session.user.auth);
@@ -73,6 +76,40 @@ module.exports = function(req, res, next) { @@ -73,6 +76,40 @@ module.exports = function(req, res, next) {
73 } 76 }
74 77
75 /** 78 /**
  79 + * 添加当前激活的菜单
  80 + * @param {Object} req 请求对象
  81 + * @param {Object} res 返回对象
  82 + */
  83 +function addActiveMenu(req,res) {
  84 + var curUrl = req.originalUrl;
  85 + var lastOpen = req.headers['referer'];
  86 + var hasActive = false;
  87 + if(!res.appendData||!res.appendData.menu) {
  88 + return;
  89 + }
  90 + var menu = _.cloneDeep(res.appendData.menu);
  91 +
  92 + function loop(list,condition) {
  93 + _.forEach(menu,function(v,k) {
  94 + _.forEach(v.menu,function(val,key) {
  95 + if(condition(val.href,curUrl)) {
  96 + v.active = 'active';
  97 + val.active = 'active';
  98 + hasActive = true;
  99 + }
  100 + });
  101 + });
  102 + }
  103 +
  104 + loop(menu,function(url) { return url === curUrl;});
  105 +
  106 + if(!hasActive && lastOpen) {
  107 + loop(menu,function(url) { return lastOpen.indexOf(url)>-1;});
  108 + }
  109 + res.appendData.menu = menu;
  110 +}
  111 +
  112 +/**
76 * 检查路径是否没有权限 113 * 检查路径是否没有权限
77 * @param {String} path 114 * @param {String} path
78 * @param {Object} req 115 * @param {Object} req
@@ -98,7 +135,7 @@ function checkPath(path,req) { @@ -98,7 +135,7 @@ function checkPath(path,req) {
98 * @return {Object} 用户信息对象 135 * @return {Object} 用户信息对象
99 */ 136 */
100 function appendAdminInfo(req,auth) { 137 function appendAdminInfo(req,auth) {
101 - var ipObject = ipaddr.process(req.ip).octets 138 + var ipObject = ipaddr.process(req.ip).octets;
102 var ip = ipObject?ipObject.join('.'):req.ip; 139 var ip = ipObject?ipObject.join('.'):req.ip;
103 req._yoheaders = { 140 req._yoheaders = {
104 'x-user-id':auth.pid, 141 'x-user-id':auth.pid,
@@ -13,7 +13,7 @@ @@ -13,7 +13,7 @@
13 "ipaddr.js": "^1.1.0", 13 "ipaddr.js": "^1.1.0",
14 "lodash": "^3.10.0", 14 "lodash": "^3.10.0",
15 "request": "^2.55.0", 15 "request": "^2.55.0",
16 - "yo.js": "^1.1.3" 16 + "yo.js": "^1.1.5"
17 }, 17 },
18 "devDependencies": { 18 "devDependencies": {
19 "connect-multiparty": "^2.0.0" 19 "connect-multiparty": "^2.0.0"
1 {{#menu}} 1 {{#menu}}
2 -<li {{#if parent}}class="parent"{{/if}}> 2 +<li {{#if parent}}class="parent {{active}}"{{/if}}>
3 <a href="javascript:;"> 3 <a href="javascript:;">
4 <i class="fa fa-suitcase"></i> 4 <i class="fa fa-suitcase"></i>
5 <span>{{title}}</span> 5 <span>{{title}}</span>
@@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
7 {{#if menu}} 7 {{#if menu}}
8 <ul class="children"> 8 <ul class="children">
9 {{#menu}} 9 {{#menu}}
10 - <li> 10 + <li class="{{active}}">
11 <a {{#if href}}href="{{href}}"{{/if}}> 11 <a {{#if href}}href="{{href}}"{{/if}}>
12 <i class="fa fa-suitcase"></i> 12 <i class="fa fa-suitcase"></i>
13 <span>{{title}}</span> 13 <span>{{title}}</span>