(
function($) {
var Role = MFW.namespace("Authmanager.Role");
var applicationId = $("#applicationid").val();
var datatable = null;
/**
* 角色列表程序 BEGIN
*/
var RoleDataTable = function() {
var $table = null, datatable = null, applicationId = "-1";
var initTable = function(applicationId) {
if (!$().dataTable || applicationId == null) {
return;
}
$table = $('#roleTable');
datatable = $table
.DataTable({
"bLengthChange":false, //关闭每页显示多少条数据
"bPaginate": false, //翻页功能
"bFilter":false, //过滤功能
"bPaginate":false,//是否使用分页搜索
"bSort": true, //排序功能
"bJQueryUI": false,
"bInfo": false,//页脚信息
"bAutoWidth":false,//自动宽度
"bProcessing":false,//正在处理提示
"serverSide": true,
ordering: false,
responsive : true,
"ajax" : {
"url" : CONTEXT_PATH
+ "/basemanager/role/api/query/"+ applicationId
},
"columns" : [
{
"data" : "id",
"mRender" : function(value, type, data) {
return "
";
}
},
{
"data" : "code"
},
{
"data" : "name",
"mRender" : function(value, type, data) {
if (data.isNormal == 1) {
return value + "(当前默认)";
} else {
return value;
}
}
},
{
"data" : "typeName"
},
{
"data" : "id",
"mRender" : function(value, type, data) {
return "
"
+ "
"
+ " "
+ "
"
+ " "
+ "
"
+ "
"
+ " ";
},
"createdCell" : function(td, cellData,
rowData, row, col) {
$(td).css('text-align', 'center');
}
} ],
"pageLength" : 15,
"fnDrawCallback" : function(oSettings) {
$table.find('.checkboxes').uniform();
$table.find('.group-checkable').change(function() {
var set = $(this).attr("data-set");
var checked = $(this).is(":checked");
$(set).each(function() {
if (checked) {
$(this).attr("checked", true);
} else {
$(this).attr("checked", false);
}
});
$(set).uniform();
});
$table
.find('.role-permission-btn')
.each(
function() {
var that = $(this);
$(this)
.click(
function() {
var roleid = $(
this)
.attr(
'data-roleid');
PermissionModal
.load(
CONTEXT_PATH
+ "/basemanager/resource/api/selectView",
{
"applicationId" : $(
"#applicationid")
.val(),
"roleId" : roleid
})
.show();
});
});
/**
* 分配角色数据权限
* */
$table.find(".data-permission-btn").each(function(){
$(this).click(function(){
//alert("999");
var setting = {
check: {
enable: true
},
data: {
simpleData: {
enable: true,
idKey : "id",
}
},
async:{
enable:true,
dataType:'json',
url:CONTEXT_PATH+"/RoleDataPermission/queryAllRoleDataPermission",
autoParam:["id=pId"],
otherParam:["roleId",$(this).attr('data-roleid')],
dataFilter: ajaxDataFilter
}
};
function ajaxDataFilter(treeId, parentNode, responseData) {
if (responseData.result==false)
{
alert('加载数据权限失败');
}
else
{
return responseData.data;
}
}
//初始化ztree
$.fn.zTree.init($("#dataPermission"), setting);
$('#optionsRadios2').click(function(){
$('div.row').show();
});
$('#optionsRadios1').click(function(){
$('div.row').hide();
});
//给模态框设置角色id
$('#dataPermissionModalRoleId').val($(this).attr('data-roleid'));
//打开模态框
openDataPermissionModal();
});
});
//分配数据模态框保存
$('.btn-modal-sbumit','#dataPermissionModal').click(
function(){
//得到选中的节点集合
var treeObj = $.fn.zTree.getZTreeObj("dataPermission");
var nodes = treeObj.getCheckedNodes(true);
//得到选中的子节点
var childNodes=new Array();
for(var i=0;i
if(nodes[i].check_Child_State=='-1')
{
childNodes.push(nodes[i].id);
};
}
$.ajax({
type: "POST",
dataType: "json",
url:CONTEXT_PATH+"/RoleDataPermission/saveAllRoleDataPermission",
data:{
"roleId":$('#dataPermissionModalRoleId').val(),
"isPermanent":$('#dataPermissionModalForm input[name="isPermanent"]:checked').val(),
"startTime":$('#startTime').val(),
"endTime":$('#endTime').val(),
"orgIds":childNodes
},
success: function(msg){
alert(msg.message);
},
error:function(){
alert("新增数据权限失败");
}
});
}
);
//加载人员树并保存
$("#userIdx").click(function(){
orguser.selectUser({title:'选择用户',selectNumber:0},
function(choosed){//回调函数
var userIds="";
var userNames="";
var roleId = $("#roleId").val();
for(var i in choosed){
userNames+=choosed[i].userName+",";
userIds+=choosed[i].userId+",";
}
if(userIds.length>0){
userNames=userNames.substring(0,userNames.length-1);
userIds=userIds.substring(0,userIds.length-1);
}
$.ajax({
type: "POST",
url:CONTEXT_PATH+"/basemanager/userrole/api/add",
dataType:"json",
data:{
"userId":userIds,
"roleId":roleId,
},
success : function(
responseText,
statusText) {
alert(responseText.message);
RoleUserModal.show($("#roleId").val());
},
error : function(
XMLHttpRequest,textStatus,
errorThrown) {
var responseText = $.parseJSON(XMLHttpRequest.responseText);
alert(responseText.message);
}
});
$("#userName").val(userNames);
});
});
$table.find('.btn-role-user').each(function() {
var that = $(this);
$(this).click(function() {
RoleUserModal.resetFormData({
roleId : that.attr('data-roleid'),
roleName : that.attr('data-rolename')
}).show(that.attr('data-roleid'));
});
});
$table.find('.btn-edit-row').each(function() {
var that = $(this);
$(this).click(function() {
RolePage.edit(that.attr('data-value'));
});
});
$table
.find('.delete-row-btn')
.each(
function() {
var that = $(this);
$(this)
.click(
function() {
RolePage
.deleteRole(that
.attr('data-value'));
});
});
}
});
};
return {
init : function(applicationId) {
initTable(applicationId);
},
getSelectedRows : function() {
var selectedRows = [];
$table.find('.checkboxes').each(function() {
var checked = $(this).is(":checked");
if (checked) {
selectedRows.push($(this).val());
}
});
return selectedRows.length > 0 ? selectedRows : null;
},
reload : function(applicationId) {
if (datatable != null) {
datatable.ajax
.url(CONTEXT_PATH + "/basemanager/role/api/query/"+applicationId).load();
$table.find('.group-checkable').attr('checked', false).uniform();
}
}
};
}();
/**
* 角色列表程序 BEGIN
*/
/**
* 角色编辑窗口程序 BEGIN
*/
var RoleModal = function() {
var $modal = null, $form = null, validator = null;
var handleValidation = function() {
var error = $('.alert-danger', $form);
validator = $form.validate({
errorElement : 'span',
errorClass : 'help-block help-block-error',
focusInvalid : false,
ignore : "",
rules : {
"name" : {
required : true
}
},
messages : {
"name" : {
required : "请输入角色名称"
}
},
invalidHandler : function(event, validator) { // 显示错误信息
error.show();
App.scrollTo(error, -200);
},
highlight : function(element) { // 高亮显示
$(element).closest('.form-group').addClass('has-error');
},
unhighlight : function(element) { // 取消高亮显示
$(element).closest('.form-group').removeClass('has-error');
},
success : function(label, element) {
label.closest('.form-group').removeClass('has-error');
}
});
};
var handlerSubmit = function() {
$('.btn-modal-sbumit', $modal).click(function() {
$("#typeName").val($("#typeCode").text());
RoleModal.submitForm();
});
};
return {
init : function() {
$modal = $('#roleModal');
$form = $('#form', $modal);
handleValidation();
handlerSubmit();
},
show : function() {
$modal.modal('show');
return this;
},
hide : function() {
$modal.modal('hide');
validator.resetForm();
return this;
},
resetFormData : function(data) {
if (data == null) {
data = {};
}
$('#id', $modal).val(data.id == null ? '' : data.id);
$('#applicationId', $modal).val(
data.applicationId == null ? '' : data.applicationId);
$('#code', $modal).val(data.code == null ? '' : data.code);
$('#name', $modal).val(data.name == null ? '' : data.name);
$('#typeCode', $modal).val(
data.typeCode == null ? '' : data.typeCode);
$('#typeName', $modal).val(
data.typeName == null ? '' : data.typeName);
return this;
},
setModalTitle : function(title) {
$('.modal-title', $modal).html(title);
return this;
},
setFormAction : function(action) {
$form.attr('action', action);
return this;
},
submitForm : function() {
if ($form == null) {
alert('表单初始化失败,无法执行操作');
return;
}
if ($form.valid() == false) {
return;
}
$('.alert-danger', $form).hide();
$form.ajaxSubmit(MFW.AjaxUtil.getOptions({
success : function(responseText, statusText) {
alert(responseText.message);
RoleModal.hide();
RoleDataTable.reload($("#applicationid").val());
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
var responseText = $
.parseJSON(XMLHttpRequest.responseText);
alert(responseText.message);
}
}));
return this;
}
};
}();
/**
* 角色编辑窗口程序 END
*/
/**
* 角色权限窗口程序 BEGIN
*/
var PermissionModal = function() {
var $modal = null, $container = null;
var initModal = function() {
$modal = $('#permissionModal');
$container = $('#permissionContainer', $modal);
$('.btn-modal-sbumit', $modal)
.click(
function() {
var nodes = Authmanager.ResourceSelector
.getSelected();
if (nodes == null) {
alert('无法获取资源信息,请重新操作');
return;
}
var appId = Authmanager.ResourceSelector
.getApplicationId();
var roleId = Authmanager.ResourceSelector
.getRoleId();
if (nodes.length == 0) {
MFW.AjaxUtil
.post(
CONTEXT_PATH
+ "/basemanager/permission/api/remove",
{
'applicationId' : $(
"#applicationid")
.val(),
'roleId' : roleId
},
function(responseText) {
alert(responseText.message);
},
function(responseText) {
alert(responseText.message);
});
return;
} else {
var resourceIds = [];
for (var i = 0, length = nodes.length; i < length; i++) {
var resourceId = nodes[i].data.resourceId;
if (resourceId != null) {
resourceIds.push(resourceId);
}
}
MFW.AjaxUtil
.post(
CONTEXT_PATH
+ "/basemanager/permission/api/add",
{
'applicationId' : $(
"#applicationid")
.val(),
'roleId' : roleId,
"resourceIds" : resourceIds
},
function(responseText) {
alert(responseText.message);
},
function(responseText) {
alert(responseText.message);
});
}
});
};
return {
init : function() {
initModal();
return this;
},
show : function() {
$modal.modal('show');
return this;
},
load : function(url, data) {
$container.load(url, data);
return this;
}
};
}();
/**
* 角色权限窗口程序 END
*/
/**
* 添加角色人员 BEGIN
*/
var RoleUserModal = function() {
var $modal = null, $form = null, $message = null;
var init = function() {
$modal = $('#roleUserModal');
$form = $('#form', $modal);
$message = $('#roleUserFormMsg', $form);
};
var handlerSubmit = function() {
$('.btn-modal-sbumit', $modal)
.click(
function() {
if ($form == null) {
alert('表单初始化失败,无法执行操作');
return;
}
if ($form.valid() == false) {
return;
}
$('.alert-danger', $form).hide();
$form
.ajaxSubmit(MFW.AjaxUtil
.getOptions({
success : function(
responseText,
statusText) {
// showAlert("#alertMessage",
// responseText.message,
// 'success', 5,
// 'check');
alert(responseText.message);
// RoleUserModal.hide();
$("#userId").val("");
RoleUserModal.show($(
"#roleId")
.val());
},
error : function(
XMLHttpRequest,
textStatus,
errorThrown) {
var responseText = $
.parseJSON(XMLHttpRequest.responseText);
alert(responseText.message);
}
}));
});
};
var handleValidation = function() {
var error = $('.alert-danger', $form);
validator = $form.validate({
errorElement : 'span',
errorClass : 'help-block help-block-error',
focusInvalid : false,
ignore : "",
rules : {
"roleId" : {
required : true
},
"userId" : {
required : true
}
},
messages : {
"roleId" : {
required : '缺失角色信息,无法执行操作'
},
"userId" : {
required : "请输入用户ID"
}
},
invalidHandler : function(event, validator) { // 显示错误信息
error.show();
App.scrollTo(error, -200);
},
highlight : function(element) { // 高亮显示
$(element).closest('.form-group').addClass('has-error');
},
unhighlight : function(element) { // 取消高亮显示
$(element).closest('.form-group').removeClass('has-error');
},
success : function(label, element) {
label.closest('.form-group').removeClass('has-error');
}
});
};
return {
init : function() {
init();
handleValidation();
handlerSubmit();
return this;
},
show : function(roleId) {
$table = $('#roleUserTable');
$modal.modal('show');
// 如果重载先destroy
if (datatable) {
datatable.destroy();
}
// 加载用户列表 BEGIN
datatable = $table.DataTable({
"serverSide" : true,
responsive : true,
ordering : false,
destroy : true,
"ajax" : {
"url" : CONTEXT_PATH
+ "/basemanager/userrole/api/list?roleId="
+ roleId
},
"columns" : [ {
"data" : "userId",
class : "col-md-6"
}, {
"data" : "roleId",
class : "col-md-6"
} ],
"pageLength" : 10
});
// 加载用户列表 END
return this;
},
hide : function() {
$modal.modal('hide');
validator.resetForm();
return this;
},
resetFormData : function(data) {
if (data == null) {
data = {};
}
$('#roleId', $modal)
.val(data.roleId == null ? '' : data.roleId);
$('.role-name', $modal).val(
data.roleName == null ? '' : data.roleName);
return this;
},
reload : function() {
if (datatable != null) {
datatable.ajax.reload();
}
}
};
}();
/**
* 添加角色人员 END
*/
/**
* RolePage BEGIN
*/
var RolePage = function() {
var handlerAddRole = function(applicationId) {
$('#addRoleBtn').click(
function() {
RoleModal.setModalTitle('新建角色').setFormAction(
CONTEXT_PATH + "/basemanager/role/api/add")
.resetFormData({
applicationId : $("#applicationid").val()
}).show();
});
};
var deleteRoleOk = function(ids) {
bootbox.confirm("确认删除数据?", function(result) {
if (result) {
$.post(CONTEXT_PATH + "/basemanager/role/api/remove/"+ids, null, function(result) {
if (result.resultFlag) {
alert(result.message);
RoleDataTable.reload($("#applicationid").val());
} else {
alert(result.message);
}
}, "json");
}
});
};
var handlerDeleteRole = function() {
$('#removeRoleBtn').click(function() {
var rows = RoleDataTable.getSelectedRows();
if (rows == null || rows.length == 0) {
alert("请选择要删除的数据");
return;
}
deleteRoleOk(rows);
});
};
var handlerSetNormalRole = function() {
$('#normalRoleBtn').click(function() {
var rows = RoleDataTable.getSelectedRows();
if (rows == null || rows.length == 0) {
alert("请选择要设置的数据");
return;
}
if (rows == null && rows.length != 1) {
alert("只能选择一条数据");
return;
}
setNormalRoleOk(rows);
});
};
var setNormalRoleOk = function(ids) {
$.post(CONTEXT_PATH + "/basemanager/role/api/setNormalRole", {
"id" : ids[0],
"applicationId" : $("#applicationid").val()
}, function(result) {
if (result.resultFlag) {
alert(result.message);
RoleDataTable.reload($("#applicationid").val());
} else {
alert(result.message);
}
}, "json");
};
return {
init : function(applicationId) {
handlerAddRole(applicationId);
handlerDeleteRole();
handlerSetNormalRole();
},
edit : function(id) {
MFW.AjaxUtil.get(CONTEXT_PATH + "/basemanager/role/api/info/"
+ id, null, function(responseText) {
if (responseText.resultFlag) {
var data = responseText.data;
if (data == null) {
alert('获取角色信息失败,无法执行操作,请联系系统管理员');
return;
}
RoleModal.setModalTitle('更新帐户').setFormAction(
CONTEXT_PATH + "/basemanager/role/api/modify")
.resetFormData(data).show();
} else {
alert(responseText.message);
}
}, function(responseText) {
alert(responseText.message);
});
},
deleteRole : function(id) {
if (id != null) {
deleteRoleOk([ id ]);
}
}
};
}();
/**
* RolePage END
*/
Role = $.extend(true, Role, {
init : function() {
var appId = $("#applicationid").val();
RoleDataTable.init(appId);
RoleModal.init();
PermissionModal.init();
RoleUserModal.init();
RolePage.init(appId);
}
});
window.openDataPermissionModal=function()
{
$('#dataPermissionModal').modal('show');
}
$(function() {
Authmanager.Role.init();
});
})(jQuery);