$(document).ready(function () { initRoleTable(); initTree(); }); var roleTable; var size; var number; function initRoleTable() { var queryUrl = '/Admin/GetRoles'; roleTable = $("#role_table").bootstrapTable({ url: queryUrl, columns: [ { field: "id", title: "ID", visible: false }, { field: "name", title: "角色名称" }, { field: "createTime", title: "创建时间" }, { title: '操作', field: "id", formatter: operation, width: 300 } ], pagination: true, pageNumber: 1, pageSize: size, pageList: [10, 25, 50, 100], smartDisplay: false, sidePagination: "server", queryParams: function (params) { var temp = { pageNumber: (params.offset / params.limit) + 1, pageSize: params.limit, keyword: $("#keyword").val() }; number = temp.pageNumber; return temp; } }); } function operation(value, row, index) { var htm = " 设置权限" + " 编辑" + " 删除"; return htm; } function modelCreateRole() { $("#Id").val(""); $("#modal_role_add").modal("show"); } function saveRole() { var id = $("#Id").val(); var url; if (id === null || id === "") { url = "/Admin/CreateRole"; } else { url = "/Admin/UpdateRole/" + id; } $.ajax({ url: url, method: "POST", headers: { "Content-Type": "application/json" }, async: true, data: JSON.stringify({ "name": $("#Name").val() }), success: function (result) { if (result.code === 0) { $("#modal_role_add").modal("hide"); if (id === null || id === "") { refreshRoleTable(); } else { selectPageRoleTable(); } } else if (result.code === 1) { alert(result.message); } } }); } function modelUpdateRole(id) { $("#Id").val(id); $.ajax({ url: "/Admin/Role/" + id, method: "GET", headers: { "Content-Type": "application/json" }, async: true, success: function (result) { if (result.code === 0) { $("#Name").val(result.data.name); } else if (result.code === 1) { alert(result.message); } } }); $("#modal_role_add").modal("show"); } function deleteRole(id) { $.ajax({ url: "/Admin/DeleteRole/" + id, method: "POST", headers: { "Content-Type": "application/json" }, async: true, success: function (result) { if (result.code === 0) { if (id === null || id === "") { refreshRoleTable(); } else { selectPageRoleTable(); } } else if (result.code === 1) { alert(result.message); } } }); } function refreshRoleTable() { roleTable.bootstrapTable("refreshOptions", { pageNubmer: 1 }); } function selectPageRoleTable() { roleTable.bootstrapTable("selectPage", number); } function modelPermissions(id) { $("#Id").val(id); $.ajax({ url: "/Admin/Role/" + id, method: "GET", headers: { "Content-Type": "application/json" }, async: true, success: function (result) { if (result.code === 0) { $("#Name2").val(result.data.name); } else if (result.code === 1) { alert(result.message); } } }); $.ajax({ url: "/Admin/Permissions/" + id, method: "POST", headers: { "Content-Type": "application/json" }, async: true, success: function (result) { zTreeObj.checkAllNodes(false); for (var i = 0; i < result.length; i++) { var node = zTreeObj.getNodeByParam("id", result[i].id); zTreeObj.checkNode(node, true, true); } } }); $("#modal_permissions").modal("show"); } var zTreeObj; var setting = { check: { enable: true }, data: { simpleData: { enable: true } } }; //var zNodes = [ // { id: "1", pId: "0", name: "数据一览" }, // { id: "2", pId: "0", name: "计划监控" }, // { id: "3", pId: "0", name: "产线一览" }, // { id: "4", pId: "0", name: "产线详情" }, // { id: "5", pId: "0", name: "数据报表" }, // { id: "6", pId: "0", name: "Cim监控" }, // { id: "7", pId: "0", name: "配置管理", open: true }, // { id: "701", pId: "7", name: "机种管理" }, // { id: "6", pId: "0", name: "系统管理", open: true }, // { id: "601", pId: "6", name: "用户管理" }, // { id: "602", pId: "6", name: "角色管理" }, // { id: "7", pId: "0", name: "故障统计" }, // { id: "8", pId: "0", name: "能耗统计" }, // { id: "9", pId: "0", name: "运维管理" } //]; var zNodes = []; function initTree() { $.ajax({ url: "/Admin/Menu", method: "GET", headers: { "Content-Type": "application/json" }, async: false, success: function (result) { zNodes = result; } }); zTreeObj = $.fn.zTree.init($("#permissions"), setting, zNodes); } class Menu { constructor(id, name) { this.id = id; this.name = name; } } function savePermissions() { var id = $("#Id").val(); var nodes = zTreeObj.getCheckedNodes(true); var dto = new Array(); for (var i = 0; i < nodes.length; i++) { var d = new Menu(nodes[i].id, nodes[i].name); dto.push(d); } $.ajax({ url: "/Admin/SetPermissions/" + id, method: "POST", headers: { "Content-Type": "application/json" }, async: true, data: JSON.stringify(dto), success: function (result) { if (result.code === 0) { $("#modal_permissions").modal("hide"); } else if (result.code === 1) { alert(result.message); } } }); }