'use strict'; var app = angular.module('app', ["angularUtils.directives.dirPagination"]); app.controller("roleCtrl", ["$scope", "$http", "$compile", "$timeout", function ($scope, $http, $compile, $timeout) { var self = this; var s4 = new SM4Util(); self.userId = $.cookie("GlWorkPlatform-userid"); self.userName = $.cookie("GlWorkPlatform-username"); self.userCName = $.cookie("GlWorkPlatform-chineseName"); self.get_AccessToken = localStorage.getItem("GlWorkPlatform-AccessToken"); self.userTypeEng = $.cookie("GlWorkPlatform-userTypeEng"); var postCfg = { headers: { 'Content-Type': 'application/json', 'Authorization': "Bearer " + self.get_AccessToken } }; self.loading = false; ///加载机构 self.unitList = []; ///数组 self.unit = ""; ///机构名称 self.getUnit = function () { self.loading = true; $http.post(apiurljs.login + "g2work/sect/querySectAdmin", { data: s4.encryptData_CBC("") }, postCfg) .success(function (obj) { var res = strToJson(s4.decryptData_CBC(obj.data)); ///console.log(res); if (res.success) { if (sp.isval(res.data)) { self.unitList = res.data; self.unit = res.data[0].CODE; self.getRole();///根据当前第一个机构进行加载其下面的部门数据 self.getPersonData(self.unit, "0"); ///加载机构下方的人员 } else { self.unitList = []; self.loading = false; } } else { sp.dialog(res.message); self.unitList = []; self.loading = false; } }) .error(function (XMLHttpRequest, textStatus, errorThrown) { if (textStatus != 200) { self.loading = false; } }) }; self.getUnit(); self.rows = []; self.users = []; self.getRole = function () { var time1 = sp.getLocalDate(); self.activeRole = -1; self.edit = { roleID: "", roleName: "" } var postData = { unitid: self.unit } $http.post(apiurljs.login + "g2work/datazu/queryData", { data: s4.encryptData_CBC(JSON.stringify(postData)) }, postCfg) .success(function (obj) { var res = strToJson(s4.decryptData_CBC(obj.data)); ///console.log(JSON.stringify(res)); if (res.success) { if (sp.isval(res.data)) { self.rows = res.data; } else { self.rows = []; } } else { sp.dialog(res.message); } }); } ////加载用户列表 self.getPerson = [];///声明用户列表数组 self.getPersonData = function (zid, levels) { var postData = { zid: zid,///角色id为空加载全部用户数据 levels: levels ///以几级的形式传 } $http.post(apiurljs.login + "g2work/datazu/queryDataUser", { data: s4.encryptData_CBC(JSON.stringify(postData)) }, postCfg) .success(function (obj) { var res = strToJson(s4.decryptData_CBC(obj.data)); self.loading = false; ///console.log(JSON.stringify(res)); if (res.success) { if (sp.isval(res.data)) { self.isSelectedAll = true; self.getPerson = res.data; self.getPerson.forEach(function (itm) { itm.users.forEach(function (itm2) { if (itm2.auth == "1") { itm2.check = true; } else { itm2.check = false; self.isSelectedAll = false; //有一个未选中就是未全选 } }); }); } else { self.getPerson = []; } } else { sp.dialog(res.message); } }) .error(function (XMLHttpRequest, textStatus, errorThrown) { if (textStatus != 200) { self.loading = false; } }) }; // 点击选中角色管理事件 self.activeRole = -1; self.chooseRole = function (index, $item) { self.activeRole = index; self.edit = { roleID: $item.ZID, roleName: $item.ZUM }; self.getPersonData(self.edit.roleID, "0"); } //增加角色 self.addRoleName = ''; self.addRole = function () { ///增加角色 var postData = { unitid: self.unit,//机构编码 zum: self.addRoleName//角色名称 } $http.post(apiurljs.login + "g2work/datazu/insertData", { data: s4.encryptData_CBC(JSON.stringify(postData)) }, postCfg) .success(function (obj) { var res = strToJson(s4.decryptData_CBC(obj.data)); if (res.success) { self.getRole(); self.addRoleName = ''; self.activeRole = -1; self.getPersonData(self.unit, "0"); ///加载机构下方的人员 sp.dialoghide(); } else { sp.dialog(res.message); } }); } //修改角色 self.edit = { roleID: "", roleName: "" } self.editRole = function () { var postData = { zid: self.edit.roleID,//角色编码 zum: self.edit.roleName//角色名称 } $http.post(apiurljs.login + "g2work/datazu/updateDataByZid", { data: s4.encryptData_CBC(JSON.stringify(postData)) }, postCfg) .success(function (obj) { var res = strToJson(s4.decryptData_CBC(obj.data)); if (res.success) { self.getRole(); sp.dialoghide(); } else { sp.dialog(res.message); } }); } //删除角色 self.removeRole = function ($item) { var getDelID = $item.ZID; var dialogHead = "温馨提示×"; var dialogBody = "
删除后不可恢复,您确认要删除吗?
"; var dialogFoot = "
"; $('.sp-dialog').html(dialogHead + dialogBody + dialogFoot); sp.openCenter(".sp-dialog"); ///取消按钮 $('.sp-closeDialog').click(function (event) { sp.closeCenter(".sp-dialog"); event.preventDefault(); event.stopPropagation(); }); //post消息的id $("#delDataTrue").click(function () { var getid = $(this).attr("data-id"); sp.dialoghide(); var data = { zid: getid///角色编码 }; $http.post(apiurljs.login + "g2work/datazu/deleteByZid", { data: s4.encryptData_CBC(JSON.stringify(data)) }, postCfg) .success(function (obj) { var res = strToJson(s4.decryptData_CBC(obj.data)); if (res.success) { self.getRole(); self.addRoleName = ''; self.activeRole = -1; self.getPersonData(self.unit, "0"); ///加载机构下方的人员 sp.dialoghide(); } else { sp.dialog(res.message); } }); }); // if (confirm('确定要删除么')) { // self.unit.ROLELIST.splice(index, 1); // self.activeRole = 0; // } } //上移一位 self.moveUp = function () { if (sp.isval(self.edit.roleID)) { if (self.activeRole == 0) { sp.dialog("已处于最顶部,无法上移!"); } else { var set_index = parseInt(self.activeRole - 1); var postData = { "zid1": self.rows[set_index].ZID, "zid2": self.edit.roleID }; $http.post(apiurljs.login + "g2work/datazu/swapSortidByZid", { data: s4.encryptData_CBC(JSON.stringify(postData)) }, postCfg) .success(function (obj) { var res = strToJson(s4.decryptData_CBC(obj.data)); if (res.success) { self.getRole(); self.activeRole = -1; self.getPersonData(self.unit, "0"); ///加载机构下方的人员 } else { sp.dialog(res.message); } }); } } else { sp.dialog("请先点击选择角色!"); } } //下移一位 self.moveDown = function () { if (sp.isval(self.edit.roleID)) { if (self.activeRole == self.rows.length - 1) { sp.dialog("已处于最底部,无法下移!"); } else { var set_index = parseInt(self.activeRole + 1); var postData = { "zid1": self.rows[set_index].ZID, "zid2": self.edit.roleID }; $http.post(apiurljs.login + "g2work/datazu/swapSortidByZid", { data: s4.encryptData_CBC(JSON.stringify(postData)) }, postCfg) .success(function (obj) { var res = strToJson(s4.decryptData_CBC(obj.data)); if (res.success) { self.getRole(); self.activeRole = -1; self.getPersonData(self.unit, "0"); ///加载机构下方的人员 } else { sp.dialog(res.message); } }); } } else { sp.dialog("请先点击选择角色!"); } } ///点击用户checkbox选中事件 self.isSelected = function () { self.isSelectedAll = true; self.getPerson.forEach(function (itm) { itm.users.forEach(function (item2) { if (!item2.check) { return self.isSelectedAll = false; } }) }) } //全选 self.isSelectedAll = false; self.selectAll = function () { if (self.isSelectedAll) { self.getPerson.forEach(function (itm) { itm.users.forEach(function (item2) { item2.check = true; }) }) } else { self.getPerson.forEach(function (itm) { itm.users.forEach(function (item2) { item2.check = false; }) }) } } //保存人员设置 self.saveRole = function () { var get_checkedUser = []; self.getPerson.forEach(function (itm) { itm.users.forEach(function (item2) { if (item2.check) { get_checkedUser.push({ "zid": self.edit.roleID, "userid": item2.userid }) } }); }) // console.log(get_checkedUser); if (sp.isval(get_checkedUser)) { var postData = { "typeid": "zid", typevalue: self.edit.roleID, data: JSON.stringify(get_checkedUser) } // console.log(postData); $http.post(apiurljs.login + "g2work/datazu/updateDataUser", { data: s4.encryptData_CBC(JSON.stringify(postData)) }, postCfg) .success(function (obj) { var res = strToJson(s4.decryptData_CBC(obj.data)); if (res.success) { sp.dialog(res.message); $timeout(function () { self.getPersonData(self.edit.roleID, "0"); sp.dialoghide(); }, 600); } else { sp.dialog(res.message); } }); } else { sp.dialog("人员不能为空!"); } } }]) app.directive('repeatFinish', function () { return { link: function (scope) { if (scope.$last == true) { $(".tab").eq(0).addClass("active"); } } } })