修改菜单指派角色功能

This commit is contained in:
Argo-Lenovo 2016-10-28 14:45:09 +08:00
parent 35c915c3e8
commit 83bdfe3b71
7 changed files with 110 additions and 135 deletions

View File

@ -221,7 +221,15 @@
data: data.data,
type: data.method,
success: function (result) {
if ($.isFunction(data.callback)) data.callback(result);
if ($.isFunction(data.callback)) {
if ($.isArray(result)) {
var html = $.map(result, function (element, index) {
return $.format('<div class="checkbox col-lg-3 col-xs-4"><label title="{3}"><input type="checkbox" value="{0}" {2}>{1}</label></div>', element.ID, element.RoleName, element.Checked, element.Description);
}).join('');
data.callback(html);
}
}
else { data.callback(false); }
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
if ($.isFunction(data.callback)) data.callback(false);
@ -236,39 +244,14 @@
Role.getRolesByGroupId = function (groupId) {
processRolesData({ Id: groupId, callback: callback, data: { type: "group" } });
};
//查询菜单对应角色
Role.getRolesByMenuId = function (menuId, callback) {
$.ajax({
url: '../api/Roles/' + menuId,
data: { "": "menu" },
type: 'POST',
success: function (result) {
callback(result);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
callback();
}
});
processRolesData({ Id: menuId, callback: callback, data: { type: "menu" } });
};
Role.saveRolesByUserId = function (userId, roleIds, callback) {
processRolesData({ Id: userId, callback: callback, method: "PUT", data: { type: "user", roleIds: roleIds } });
}
//保存菜单对应角色
Role.saveRolesByMenuId = function (menuId, roleIds, callback) {
$.ajax({
url: '../api/Roles/' + menuId,
data: { "roleIds": roleIds, "type": "menu" },
type: 'PUT',
success: function (result) {
callback(result);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
callback();
}
});
processRolesData({ Id: menuId, callback: callback, method: "PUT", data: { type: "menu", roleIds: roleIds } });
};
Group = {};

View File

@ -37,15 +37,13 @@ namespace Bootstrap.Admin.Controllers
case "group":
ret = RoleHelper.RetrieveRolesByGroupId(id).ToList();
break;
case "menu"
ret = RoleHelper.RetrieveRolesByMenuId(id.ToString());
case "menu":
ret = RoleHelper.RetrieveRolesByMenuId(id).ToList();
break;
default:
break;
}
else
{
return null;
}
return ret;
}
/// <summary>
///
@ -66,8 +64,9 @@ namespace Bootstrap.Admin.Controllers
case "group":
ret = RoleHelper.SaveRolesByGroupId(id, roleIds);
break;
case "menu":
ret = RoleHelper.SavaRolesByMenuId(id, roleIds);
case "menu":
ret = RoleHelper.SavaRolesByMenuId(id, roleIds);
break;
default:
break;
}

View File

@ -16,15 +16,8 @@
assign: [{
id: 'btn_assignRole',
click: function (row) {
Role.getRolesByMenuId(row.ID, function (roles) {
Role.getRolesByMenuId(row.ID, function (data) {
$("#dialogRole .modal-title").text($.format('{0}-角色授权窗口', row.Name));
var data = $.map(roles, function (element, index) {
if (element.IsSelect == 1) {
return $.format('<div class="checkbox"><label><input type="checkbox" value="{0}" checked="checked">{1}</label></div>', element.ID, element.RoleName);
} else if (element.IsSelect == 0) {
return $.format('<div class="checkbox"><label><input type="checkbox" value="{0}">{1}</label></div>', element.ID, element.RoleName);
}
}).join('');
$('#dialogRole form').html(data);
$('#dialogRole').modal('show');
});

View File

@ -13,11 +13,8 @@
assign: [{
id: 'btn_assignRole',
click: function (row) {
Role.getRolesByUserId(row.ID, function (roles) {
Role.getRolesByUserId(row.ID, function (data) {
$("#dialogRole .modal-title").text($.format('{0}-角色授权窗口', row.DisplayName));
var data = $.map(roles, function (element, index) {
return $.format('<div class="checkbox col-lg-3 col-xs-4"><label title="{3}"><input type="checkbox" value="{0}" {2}>{1}</label></div>', element.ID, element.RoleName, element.Checked, element.Description);
}).join('');
$('#dialogRole form').html(data);
$('#dialogRole').modal('show');
});

View File

@ -1,76 +1,81 @@
@model NavigatorBarModel
@{
ViewBag.Title = "菜单管理";
Layout = "~/Views/Shared/_Default.cshtml";
}
@section css {
<link href="~/Content/css/fa.css" rel="stylesheet" />
}
@section Javascript {
<script src="~/scripts/Menus.js"></script>
}
@section header {
@Html.Partial("Header", Model)
}
@section navigator {
@Html.Partial("Navigator", Model)
}
@section query {
<form class="form-inline" role="form">
<div class="form-group col-lg-5">
<label class="control-label" for="txt_menus_name">菜单名称</label>
<input type="text" class="form-control" id="txt_menus_name" />
</div>
<div class="form-group col-lg-5">
<label class="control-label" for="txt_menus_category">菜单类别</label>
<input type="text" class="form-control" id="txt_menus_category" />
</div>
<div class="form-group col-lg-2">
<button type="button" id="btn_query" class="btn btn-primary"><span class="glyphicon glyphicon-search" aria-hidden="true"></span>查询</button>
</div>
</form>
}
@section toolbar {
<button id="btn_assignRole" type="button" class="btn btn-info">
<span class="fa fa-sitemap" aria-hidden="true"></span>指派角色
</button>
}
@section modal {
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title" id="myModalLabel">用户编辑窗口</h4>
</div>
<div class="modal-body">
<form class="form-inline" id="dataForm" name="dataForm" role="form">
<div class="form-group col-lg-6">
<label class="control-label" for="parentId">父级ID</label>
<input type="text" class="form-control" id="parentId" name="parentId" maxlength="50" />
</div>
<div class="form-group col-lg-6">
<label class="control-label" for="name">菜单名称</label>
<input type="text" class="form-control" id="name" name="name" maxlength="50" />
<input type="text" class="form-control hide" id="menuID" name="menuID" />
</div>
<div class="form-group col-lg-6">
<label class="control-label" for="order">菜单序号</label>
<input type="text" class="form-control" id="order" name="order" />
</div>
<div class="form-group col-lg-6">
<label class="control-label" for="icon">菜单图标</label>
<input type="text" class="form-control" id="icon" name="icon" maxlength="50" />
</div>
<div class="form-group col-lg-6">
<label class="control-label" for="url">路径</label>
<input type="text" class="form-control" id="url" name="url" maxlength="50" />
</div>
<div class="form-group col-lg-6">
<label class="control-label" for="category">类别</label>
<input type="text" class="form-control" id="category" name="category" maxlength="50" />
</div>
</form>
</div>
}
@section customModal {
@Html.Partial("RoleConfig")
@Html.Partial("IconView")
}
@model NavigatorBarModel
@{
ViewBag.Title = "菜单管理";
Layout = "~/Views/Shared/_Default.cshtml";
}
@section css {
<link href="~/Content/css/fa.css" rel="stylesheet" />
}
@section Javascript {
<script src="~/scripts/Menus.js"></script>
}
@section header {
@Html.Partial("Header", Model)
}
@section navigator {
@Html.Partial("Navigator", Model)
}
@section query {
<form class="form-inline" role="form">
<div class="form-group col-lg-5">
<label class="control-label" for="txt_menus_name">菜单名称</label>
<input type="text" class="form-control" id="txt_menus_name" />
</div>
<div class="form-group col-lg-5">
<label class="control-label" for="txt_menus_category">菜单类别</label>
<input type="text" class="form-control" id="txt_menus_category" />
</div>
<div class="form-group col-lg-2">
<button type="button" id="btn_query" class="btn btn-primary"><span class="glyphicon glyphicon-search" aria-hidden="true"></span>查询</button>
</div>
</form>
}
@section toolbar {
<button id="btn_assignRole" type="button" class="btn btn-info">
<span class="fa fa-sitemap" aria-hidden="true"></span>指派角色
</button>
}
@section modal {
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title" id="myModalLabel">用户编辑窗口</h4>
</div>
<div class="modal-body">
<form class="form-inline" id="dataForm" name="dataForm" role="form">
<div class="form-group col-lg-6">
<label class="control-label" for="parentId">父级ID</label>
<input type="text" class="form-control" id="parentId" name="parentId" maxlength="50" />
</div>
<div class="form-group col-lg-6">
<label class="control-label" for="name">菜单名称</label>
<input type="text" class="form-control" id="name" name="name" maxlength="50" />
<input type="text" class="form-control hide" id="menuID" name="menuID" />
</div>
<div class="form-group col-lg-6">
<label class="control-label" for="order">菜单序号</label>
<input type="text" class="form-control" id="order" name="order" />
</div>
<div class="form-group col-lg-6">
<label class="control-label" for="icon">菜单图标</label>
<div class="input-group">
<input type="text" class="form-control disabled" disabled id="icon" name="icon" maxlength="50" />
<span class="input-group-btn">
<button class="btn btn-default" type="button"><i></i>选择</button>
</span>
</div>
</div>
<div class="form-group col-lg-6">
<label class="control-label" for="url">路径</label>
<input type="text" class="form-control" id="url" name="url" maxlength="50" />
</div>
<div class="form-group col-lg-6">
<label class="control-label" for="category">类别</label>
<input type="text" class="form-control" id="category" name="category" maxlength="50" />
</div>
</form>
</div>
}
@section customModal {
@Html.Partial("RoleConfig")
@Html.Partial("IconView")
}

View File

@ -29,7 +29,7 @@
<add key="UserData-CodeUserHelper-" interval="600" desc="用户信息缓存" />
<add key="RoleData-CodeRoleHelper" interval="600" desc="角色信息缓存" />
<add key="RoleData-CodeRoleHelper-" interval="600" desc="角色信息缓存" />
<add key="RoleData-CodeRoleHelper-Navigation" interval="600" desc="菜单角色信息缓存"/>
<add key="RoleData-CodeRoleHelper-Navigation-" interval="600" desc="菜单角色信息缓存"/>
<add key="MenuData-CodeMenuHelper" interval="600" desc="菜单信息缓存" />
</cacheManager>

View File

@ -18,10 +18,9 @@ namespace Bootstrap.DataAccess
/// </summary>
public class RoleHelper
{
// UNDONE: 两个缓存考虑可以共用,待完善
private const string RoleDataKey = "RoleData-CodeRoleHelper";
private const string RoleUserIDDataKey = "RoleData-CodeRoleHelper-";
private const string RoleNavigationIDDataKey = "RoleData-CodeRoleHelper-Navigation";
private const string RoleNavigationIDDataKey = "RoleData-CodeRoleHelper-Navigation-";
/// <summary>
/// 查询所有角色
/// </summary>
@ -222,17 +221,17 @@ namespace Bootstrap.DataAccess
/// </summary>
/// <param name="menuId"></param>
/// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByMenuId(string menuId)
public static IEnumerable<Role> RetrieveRolesByMenuId(int menuId)
{
string sql = "select *,case when (ID in( select RoleID from NavigationRole where NavigationID=@NavigationID)) then 1 else 0 end as IsSelect from Roles";
string k = string.Format("{0}{1}", RoleNavigationIDDataKey, menuId);
var ret = CacheManager.GetOrAdd(k, CacheSection.RetrieveIntervalByKey(RoleNavigationIDDataKey), key =>
return CacheManager.GetOrAdd(k, CacheSection.RetrieveIntervalByKey(RoleUserIDDataKey), key =>
{
List<Role> Roles = new List<Role>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@NavigationID", menuId, ParameterDirection.Input));
string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join NavigationRole ur on r.ID = ur.RoleID and NavigationID = @NavigationID";
try
{
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@NavigationID", menuId, ParameterDirection.Input));
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{
while (reader.Read())
@ -242,15 +241,14 @@ namespace Bootstrap.DataAccess
ID = (int)reader[0],
RoleName = (string)reader[1],
Description = (string)reader[2],
IsSelect = (int)reader[3]
Checked = (string)reader[3]
});
}
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Roles;
}, CacheSection.RetrieveDescByKey(RoleNavigationIDDataKey));
return ret;
}, CacheSection.RetrieveDescByKey(RoleUserIDDataKey));
}
/// <summary>