Authored by 周奇琪

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

... ... @@ -32,12 +32,12 @@ $('.leftpanel .nav .parent>a').click(function() {
});
$(".leftpanel .nav .children a").each(function() {
var attr = $(this).attr('href');
if (attr == window.location.pathname + window.location.search) {
$(this).parents(".children").show();
$(this).parent("li").addClass("active");
$(this).parents(".parent").addClass("parent-focus");
}
});
\ No newline at end of file
// $(".leftpanel .nav .children a").each(function() {
// var attr = $(this).attr('href');
// if (attr == window.location.pathname + window.location.search) {
// $(this).parents(".children").show();
// $(this).parent("li").addClass("active");
// $(this).parents(".parent").addClass("parent-focus");
// }
// });
\ No newline at end of file
... ...
... ... @@ -54,11 +54,14 @@ module.exports = function(req, res, next) {
if(guestAccessList[method+":"+path]||req.session.user) {
//判断是否已存在appendData
if(!res.appendData) {
res.appendData = req.session.user;
res.appendData = _.cloneDeep(req.session.user);
} else {
res.appendData = _.merge(res.appendData,req.session.user);
res.appendData = _.merge({},res.appendData,req.session.user);
}
//添加当前菜单激活
addActiveMenu(req,res);
//添加管理员信息到http头
if(req.session.user) {
appendAdminInfo(req,req.session.user.auth);
... ... @@ -73,6 +76,40 @@ module.exports = function(req, res, next) {
}
/**
* 添加当前激活的菜单
* @param {Object} req 请求对象
* @param {Object} res 返回对象
*/
function addActiveMenu(req,res) {
var curUrl = req.originalUrl;
var lastOpen = req.headers['referer'];
var hasActive = false;
if(!res.appendData||!res.appendData.menu) {
return;
}
var menu = _.cloneDeep(res.appendData.menu);
function loop(list,condition) {
_.forEach(menu,function(v,k) {
_.forEach(v.menu,function(val,key) {
if(condition(val.href,curUrl)) {
v.active = 'active';
val.active = 'active';
hasActive = true;
}
});
});
}
loop(menu,function(url) { return url === curUrl;});
if(!hasActive && lastOpen) {
loop(menu,function(url) { return lastOpen.indexOf(url)>-1;});
}
res.appendData.menu = menu;
}
/**
* 检查路径是否没有权限
* @param {String} path
* @param {Object} req
... ... @@ -98,7 +135,7 @@ function checkPath(path,req) {
* @return {Object} 用户信息对象
*/
function appendAdminInfo(req,auth) {
var ipObject = ipaddr.process(req.ip).octets
var ipObject = ipaddr.process(req.ip).octets;
var ip = ipObject?ipObject.join('.'):req.ip;
req._yoheaders = {
'x-user-id':auth.pid,
... ...
... ... @@ -13,7 +13,7 @@
"ipaddr.js": "^1.1.0",
"lodash": "^3.10.0",
"request": "^2.55.0",
"yo.js": "^1.1.3"
"yo.js": "^1.1.5"
},
"devDependencies": {
"connect-multiparty": "^2.0.0"
... ...
{{#menu}}
<li {{#if parent}}class="parent"{{/if}}>
<li {{#if parent}}class="parent {{active}}"{{/if}}>
<a href="javascript:;">
<i class="fa fa-suitcase"></i>
<span>{{title}}</span>
... ... @@ -7,7 +7,7 @@
{{#if menu}}
<ul class="children">
{{#menu}}
<li>
<li class="{{active}}">
<a {{#if href}}href="{{href}}"{{/if}}>
<i class="fa fa-suitcase"></i>
<span>{{title}}</span>
... ...