role.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. $(document).ready(function () {
  2. initRoleTable();
  3. initTree();
  4. });
  5. var roleTable;
  6. var size;
  7. var number;
  8. function initRoleTable() {
  9. var queryUrl = '/Admin/GetRoles';
  10. roleTable = $("#role_table").bootstrapTable({
  11. url: queryUrl,
  12. columns: [
  13. { field: "id", title: "ID", visible: false },
  14. { field: "name", title: "角色名称" },
  15. { field: "createTime", title: "创建时间" },
  16. {
  17. title: '操作',
  18. field: "id",
  19. formatter: operation,
  20. width: 300
  21. }
  22. ],
  23. pagination: true,
  24. pageNumber: 1,
  25. pageSize: size,
  26. pageList: [10, 25, 50, 100],
  27. smartDisplay: false,
  28. sidePagination: "server",
  29. queryParams: function (params) {
  30. var temp = {
  31. pageNumber: (params.offset / params.limit) + 1,
  32. pageSize: params.limit,
  33. keyword: $("#keyword").val()
  34. };
  35. number = temp.pageNumber;
  36. return temp;
  37. }
  38. });
  39. }
  40. function operation(value, row, index) {
  41. var htm =
  42. "<a class='label' onclick='modelPermissions(\"" + value + "\")'> <i class='fa fa-bookmark-o'> 设置权限</i></a >" +
  43. "<a class='label' onclick='modelUpdateRole(\"" + value + "\")'> <i class='fa fa-edit'> 编辑</i></a >" +
  44. "<a class='label' onclick='deleteRole(\"" + value + "\")'> <i class='fa fa-trash-o'> 删除</i></a >";
  45. return htm;
  46. }
  47. function modelCreateRole() {
  48. $("#Id").val("");
  49. $("#modal_role_add").modal("show");
  50. }
  51. function saveRole() {
  52. var id = $("#Id").val();
  53. var url;
  54. if (id === null || id === "") {
  55. url = "/Admin/CreateRole";
  56. } else {
  57. url = "/Admin/UpdateRole/" + id;
  58. }
  59. $.ajax({
  60. url: url,
  61. method: "POST",
  62. headers: { "Content-Type": "application/json" },
  63. async: true,
  64. data: JSON.stringify({
  65. "name": $("#Name").val()
  66. }),
  67. success: function (result) {
  68. if (result.code === 0) {
  69. $("#modal_role_add").modal("hide");
  70. if (id === null || id === "") {
  71. refreshRoleTable();
  72. } else {
  73. selectPageRoleTable();
  74. }
  75. } else if (result.code === 1) {
  76. alert(result.message);
  77. }
  78. }
  79. });
  80. }
  81. function modelUpdateRole(id) {
  82. $("#Id").val(id);
  83. $.ajax({
  84. url: "/Admin/Role/" + id,
  85. method: "GET",
  86. headers: { "Content-Type": "application/json" },
  87. async: true,
  88. success: function (result) {
  89. if (result.code === 0) {
  90. $("#Name").val(result.data.name);
  91. } else if (result.code === 1) {
  92. alert(result.message);
  93. }
  94. }
  95. });
  96. $("#modal_role_add").modal("show");
  97. }
  98. function deleteRole(id) {
  99. $.ajax({
  100. url: "/Admin/DeleteRole/" + id,
  101. method: "POST",
  102. headers: { "Content-Type": "application/json" },
  103. async: true,
  104. success: function (result) {
  105. if (result.code === 0) {
  106. if (id === null || id === "") {
  107. refreshRoleTable();
  108. } else {
  109. selectPageRoleTable();
  110. }
  111. } else if (result.code === 1) {
  112. alert(result.message);
  113. }
  114. }
  115. });
  116. }
  117. function refreshRoleTable() {
  118. roleTable.bootstrapTable("refreshOptions", { pageNubmer: 1 });
  119. }
  120. function selectPageRoleTable() {
  121. roleTable.bootstrapTable("selectPage", number);
  122. }
  123. function modelPermissions(id) {
  124. $("#Id").val(id);
  125. $.ajax({
  126. url: "/Admin/Role/" + id,
  127. method: "GET",
  128. headers: { "Content-Type": "application/json" },
  129. async: true,
  130. success: function (result) {
  131. if (result.code === 0) {
  132. $("#Name2").val(result.data.name);
  133. } else if (result.code === 1) {
  134. alert(result.message);
  135. }
  136. }
  137. });
  138. $.ajax({
  139. url: "/Admin/Permissions/" + id,
  140. method: "POST",
  141. headers: { "Content-Type": "application/json" },
  142. async: true,
  143. success: function (result) {
  144. zTreeObj.checkAllNodes(false);
  145. for (var i = 0; i < result.length; i++) {
  146. var node = zTreeObj.getNodeByParam("id", result[i].id);
  147. zTreeObj.checkNode(node, true, true);
  148. }
  149. }
  150. });
  151. $("#modal_permissions").modal("show");
  152. }
  153. var zTreeObj;
  154. var setting = {
  155. check: {
  156. enable: true
  157. },
  158. data: {
  159. simpleData: {
  160. enable: true
  161. }
  162. }
  163. };
  164. //var zNodes = [
  165. // { id: "1", pId: "0", name: "数据一览" },
  166. // { id: "2", pId: "0", name: "计划监控" },
  167. // { id: "3", pId: "0", name: "产线一览" },
  168. // { id: "4", pId: "0", name: "产线详情" },
  169. // { id: "5", pId: "0", name: "数据报表" },
  170. // { id: "6", pId: "0", name: "Cim监控" },
  171. // { id: "7", pId: "0", name: "配置管理", open: true },
  172. // { id: "701", pId: "7", name: "机种管理" },
  173. // { id: "6", pId: "0", name: "系统管理", open: true },
  174. // { id: "601", pId: "6", name: "用户管理" },
  175. // { id: "602", pId: "6", name: "角色管理" },
  176. // { id: "7", pId: "0", name: "故障统计" },
  177. // { id: "8", pId: "0", name: "能耗统计" },
  178. // { id: "9", pId: "0", name: "运维管理" }
  179. //];
  180. var zNodes = [];
  181. function initTree() {
  182. $.ajax({
  183. url: "/Admin/Menu",
  184. method: "GET",
  185. headers: { "Content-Type": "application/json" },
  186. async: false,
  187. success: function (result) {
  188. zNodes = result;
  189. }
  190. });
  191. zTreeObj = $.fn.zTree.init($("#permissions"), setting, zNodes);
  192. }
  193. class Menu {
  194. constructor(id, name) {
  195. this.id = id;
  196. this.name = name;
  197. }
  198. }
  199. function savePermissions() {
  200. var id = $("#Id").val();
  201. var nodes = zTreeObj.getCheckedNodes(true);
  202. var dto = new Array();
  203. for (var i = 0; i < nodes.length; i++) {
  204. var d = new Menu(nodes[i].id, nodes[i].name);
  205. dto.push(d);
  206. }
  207. $.ajax({
  208. url: "/Admin/SetPermissions/" + id,
  209. method: "POST",
  210. headers: { "Content-Type": "application/json" },
  211. async: true,
  212. data: JSON.stringify(dto),
  213. success: function (result) {
  214. if (result.code === 0) {
  215. $("#modal_permissions").modal("hide");
  216. } else if (result.code === 1) {
  217. alert(result.message);
  218. }
  219. }
  220. });
  221. }