修改菜单指派角色功能

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, data: data.data,
type: data.method, type: data.method,
success: function (result) { 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) { error: function (XMLHttpRequest, textStatus, errorThrown) {
if ($.isFunction(data.callback)) data.callback(false); if ($.isFunction(data.callback)) data.callback(false);
@ -236,39 +244,14 @@
Role.getRolesByGroupId = function (groupId) { Role.getRolesByGroupId = function (groupId) {
processRolesData({ Id: groupId, callback: callback, data: { type: "group" } }); processRolesData({ Id: groupId, callback: callback, data: { type: "group" } });
}; };
//查询菜单对应角色
Role.getRolesByMenuId = function (menuId, callback) { Role.getRolesByMenuId = function (menuId, callback) {
$.ajax({ processRolesData({ Id: menuId, callback: callback, data: { type: "menu" } });
url: '../api/Roles/' + menuId,
data: { "": "menu" },
type: 'POST',
success: function (result) {
callback(result);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
callback();
}
});
}; };
Role.saveRolesByUserId = function (userId, roleIds, callback) { Role.saveRolesByUserId = function (userId, roleIds, callback) {
processRolesData({ Id: userId, callback: callback, method: "PUT", data: { type: "user", roleIds: roleIds } }); processRolesData({ Id: userId, callback: callback, method: "PUT", data: { type: "user", roleIds: roleIds } });
} }
//保存菜单对应角色
Role.saveRolesByMenuId = function (menuId, roleIds, callback) { Role.saveRolesByMenuId = function (menuId, roleIds, callback) {
$.ajax({ processRolesData({ Id: menuId, callback: callback, method: "PUT", data: { type: "menu", roleIds: roleIds } });
url: '../api/Roles/' + menuId,
data: { "roleIds": roleIds, "type": "menu" },
type: 'PUT',
success: function (result) {
callback(result);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
callback();
}
});
}; };
Group = {}; Group = {};

View File

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

View File

@ -16,15 +16,8 @@
assign: [{ assign: [{
id: 'btn_assignRole', id: 'btn_assignRole',
click: function (row) { click: function (row) {
Role.getRolesByMenuId(row.ID, function (roles) { Role.getRolesByMenuId(row.ID, function (data) {
$("#dialogRole .modal-title").text($.format('{0}-角色授权窗口', row.Name)); $("#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 form').html(data);
$('#dialogRole').modal('show'); $('#dialogRole').modal('show');
}); });

View File

@ -13,11 +13,8 @@
assign: [{ assign: [{
id: 'btn_assignRole', id: 'btn_assignRole',
click: function (row) { click: function (row) {
Role.getRolesByUserId(row.ID, function (roles) { Role.getRolesByUserId(row.ID, function (data) {
$("#dialogRole .modal-title").text($.format('{0}-角色授权窗口', row.DisplayName)); $("#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 form').html(data);
$('#dialogRole').modal('show'); $('#dialogRole').modal('show');
}); });

View File

@ -57,7 +57,12 @@
</div> </div>
<div class="form-group col-lg-6"> <div class="form-group col-lg-6">
<label class="control-label" for="icon">菜单图标</label> <label class="control-label" for="icon">菜单图标</label>
<input type="text" class="form-control" id="icon" name="icon" maxlength="50" /> <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>
<div class="form-group col-lg-6"> <div class="form-group col-lg-6">
<label class="control-label" for="url">路径</label> <label class="control-label" for="url">路径</label>

View File

@ -29,7 +29,7 @@
<add key="UserData-CodeUserHelper-" interval="600" desc="用户信息缓存" /> <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-" 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="菜单信息缓存" /> <add key="MenuData-CodeMenuHelper" interval="600" desc="菜单信息缓存" />
</cacheManager> </cacheManager>

View File

@ -18,10 +18,9 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
public class RoleHelper public class RoleHelper
{ {
// UNDONE: 两个缓存考虑可以共用,待完善
private const string RoleDataKey = "RoleData-CodeRoleHelper"; private const string RoleDataKey = "RoleData-CodeRoleHelper";
private const string RoleUserIDDataKey = "RoleData-CodeRoleHelper-"; private const string RoleUserIDDataKey = "RoleData-CodeRoleHelper-";
private const string RoleNavigationIDDataKey = "RoleData-CodeRoleHelper-Navigation"; private const string RoleNavigationIDDataKey = "RoleData-CodeRoleHelper-Navigation-";
/// <summary> /// <summary>
/// 查询所有角色 /// 查询所有角色
/// </summary> /// </summary>
@ -222,17 +221,17 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="menuId"></param> /// <param name="menuId"></param>
/// <returns></returns> /// <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); 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>(); List<Role> Roles = new List<Role>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); 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";
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@NavigationID", menuId, ParameterDirection.Input));
try 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)) using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) while (reader.Read())
@ -242,15 +241,14 @@ namespace Bootstrap.DataAccess
ID = (int)reader[0], ID = (int)reader[0],
RoleName = (string)reader[1], RoleName = (string)reader[1],
Description = (string)reader[2], Description = (string)reader[2],
IsSelect = (int)reader[3] Checked = (string)reader[3]
}); });
} }
} }
} }
catch (Exception ex) { ExceptionManager.Publish(ex); } catch (Exception ex) { ExceptionManager.Publish(ex); }
return Roles; return Roles;
}, CacheSection.RetrieveDescByKey(RoleNavigationIDDataKey)); }, CacheSection.RetrieveDescByKey(RoleUserIDDataKey));
return ret;
} }
/// <summary> /// <summary>