(function ($app) { $app.module('ylmis').controller('editRoleCtrl', function ($scope, $http, $filter, $modal, $ocLazyLoad, $alert, $loading, $state, AuthUser, $bsRouterState) { $scope.params = $scope.$parent.$parent.editData.params; $scope.parentLoad = $scope.$parent.$parent.editData.parentLoad; $scope.role = {}; $scope.roleFunctionCode = {}; $scope.functionCodeList = {}; $scope.delRole = function () { if (confirm('是否确定删除发布角色?')) { for (var i = $scope.roleList.length - 1; i >= 0; i--) { if ($scope.roleList[i].rowChecked) { $scope.roleList.splice(i, 1); } } } } $scope.delUser = function () { if (confirm('是否确定删除发布用户?')) { for (var i = $scope.userList.length - 1; i >= 0; i--) { if ($scope.userList[i].rowChecked) { $scope.userList.splice(i, 1); } } } } $scope.loadData = function () { $loading.show(); $http({ method: "post", url: "../../api/systemsetting/Role/GetRole", data: { roleID: $scope.params.roleID } }).then(function (result) { $loading.hide(); $scope.role = result.data.Data; }); } $scope.loadFunctionCodeList = function () { $loading.show(); $http({ method: "post", url: "../../api/systemsetting/Role/GetFunctionCodeList" }).then(function (result) { $loading.hide(); $scope.functionCodeList = result.data.Data; $scope.loadRoleFunctionCode(); }); } $scope.loadRoleFunctionCode = function () { $loading.show(); $http({ method: "post", url: "../../api/systemsetting/Role/GetRoleFunctionCode", data: { roleID: $scope.params.roleID } }).then(function (result) { $loading.hide(); $scope.roleFunctionCode = result.data.Data; //循环遍历所有的Menulist与返回结果的content的值进行比对,然后设置MenuList的isChecked=true angular.forEach($scope.roleFunctionCode, function (val, key) { var data = $filter('filter')($scope.functionCodeList, function (value) { return value.FunctionCode == val }); if (data.length > 0) { data[0].isChecked = true; } }); $scope.PcodeLists = []; $scope.getPcodeLists($scope.PcodeLists, ''); }); } //使用递归将所有节点的内容填好 $scope.getPcodeLists = function (dataList, functionCode) { angular.forEach($scope.functionCodeList, function (data) { if (data.ParentFunctionCode == functionCode) { var childList = []; $scope.getPcodeLists(childList, data.FunctionCode); dataList.push({ myCode: data, isChecked: data.isChecked, childCode: childList, changeShow: false, changeClass: 'glyphicon-chevron-right' }); } }); }; //根据父节点来显示对应的子节点 $scope.getCodeByPCode = function (item) { //改变样式 item.changeClass = item.changeClass == 'glyphicon-chevron-down' ? 'glyphicon-chevron-right' : 'glyphicon-chevron-down'; //改变child的显示 angular.forEach(item.childCode, function (data) { data.changeShow = data.changeShow == true ? false : true; }); }; //父节点的过滤 $scope.codefilter = function (value) { return !value.ParentFunctionCode; } //checkbox的判断 $scope.chkchildren = function (item) { if (angular.isDefined(item.childCode)) { angular.forEach(item.childCode, function (val, key) { val.isChecked = item.isChecked; $scope.chkchildren(val); }); } $scope.chkparent(item); }; $scope.chkparent = function (item) { $scope.tempCode = ""; dataList = $scope.PcodeLists; functionCode = item.myCode.ParentFunctionCode; $scope.getPcode = function (dataList, functionCode) { var flag = true; angular.forEach(dataList, function (val) { if (flag) { if (functionCode == val.myCode.FunctionCode) { //找到父级 $scope.tempCode = val; //选择一个全部勾选父节点 if (item.isChecked) { val.isChecked = item.isChecked; } else { $scope.Amt = 0; var flag2 = true; angular.forEach(val.childCode, function (val_child) { if (flag2) { if (val_child.isChecked == item.isChecked) { $scope.Amt = $scope.Amt + 1; } flag = false; } }) if (val.childCode.length == $scope.Amt) { // val.isChecked = item.isChecked; } } flag = false; } $scope.getPcode(val.childCode, functionCode); } }); } if (functionCode) { $scope.getPcode(dataList, functionCode); } if ($scope.tempCode && $scope.tempCode.myCode.ParentFunctionCode != null) { $scope.getPcode(dataList, $scope.tempCode.myCode.ParentFunctionCode); //var grandChk = $scope.tempCode; //$scope.chkparent(grandChk); } }; $scope.saveData = function (isflag) { if (isflag) { //遍历所有的isChecked的属性,用,去拼接; var functionCodeArr = []; $scope.GetPcodeChecked = function (dataList) { angular.forEach(dataList, function (data) { if (data.isChecked) { this.push(data.myCode.FunctionCode); } $scope.GetPcodeChecked(data.childCode); }, functionCodeArr); } $scope.GetPcodeChecked($scope.PcodeLists); $loading.show(); var url = "../../api/systemsetting/Role/Add"; if ($scope.role.RoleID) { url = "../../api/systemsetting/Role/Edit"; } $http({ method: "post", url: url, data: { role: $scope.role, "functionCodes": functionCodeArr } }).then(function (result) { $loading.hide(); if (result.data.IsSuccess) { $scope.showMsg('成功', '保存成功。'); //重新刷新列表 $scope.parentLoad(); $scope.$hide(); } else { $scope.showMsg('成功', result.data.Message); } }, function (resp) { $loading.hide(); $scope.showMsg('错误', '服务器错误'); }); } } if ($scope.params.roleID) { $scope.loadData(); } $scope.loadFunctionCodeList(); }); })(angular);