From 92e722c674fdf96e1c90bee479212ddee0689f5b Mon Sep 17 00:00:00 2001 From: Karson Date: Thu, 11 Sep 2025 14:24:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=9D=83=E9=99=90=E8=A7=92?= =?UTF-8?q?=E8=89=B2=E7=BB=84=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/controller/auth/Group.php | 5 +- application/admin/view/auth/group/add.html | 10 +-- application/admin/view/auth/group/edit.html | 10 +-- public/assets/js/backend/auth/group.js | 71 ++++++++++++--------- 4 files changed, 58 insertions(+), 38 deletions(-) diff --git a/application/admin/controller/auth/Group.php b/application/admin/controller/auth/Group.php index be4c2c62..aec0cd5c 100644 --- a/application/admin/controller/auth/Group.php +++ b/application/admin/controller/auth/Group.php @@ -258,7 +258,10 @@ class Group extends Backend } if (($pid || $parentGroupModel) && (!$id || $currentGroupModel)) { $id = $id ? $id : null; - $ruleList = collection(model('AuthRule')->order('weigh', 'desc')->order('id', 'asc')->select())->toArray(); + $ruleList = Db::name("auth_rule") + ->field('id,pid,name,title,icon,ismenu,status,weigh') + ->order('weigh DESC,id ASC') + ->select(); //读取父类角色所有节点列表 $parentRuleList = []; if (in_array('*', explode(',', $parentGroupModel->rules))) { diff --git a/application/admin/view/auth/group/add.html b/application/admin/view/auth/group/add.html index 3c281ee0..7e0a4644 100644 --- a/application/admin/view/auth/group/add.html +++ b/application/admin/view/auth/group/add.html @@ -1,6 +1,6 @@
{:token()} - +
@@ -10,14 +10,16 @@
- +
- - +
+ + +
diff --git a/application/admin/view/auth/group/edit.html b/application/admin/view/auth/group/edit.html index a062e43c..12e48a20 100644 --- a/application/admin/view/auth/group/edit.html +++ b/application/admin/view/auth/group/edit.html @@ -1,6 +1,6 @@ {:token()} - +
@@ -10,14 +10,16 @@
- +
- - +
+ + +
diff --git a/public/assets/js/backend/auth/group.js b/public/assets/js/backend/auth/group.js index 8002fd1b..34cfc237 100755 --- a/public/assets/js/backend/auth/group.js +++ b/public/assets/js/backend/auth/group.js @@ -47,7 +47,8 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ( {field: 'state', checkbox: true,}, {field: 'id', title: 'ID'}, {field: 'pid', title: __('Parent')}, - {field: 'name', title: __('Name'), align: 'left', formatter:function (value, row, index) { + { + field: 'name', title: __('Name'), align: 'left', formatter: function (value, row, index) { return value.toString().replace(/(&|&)nbsp;/g, ' '); } }, @@ -79,14 +80,17 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ( }, api: { bindevent: function () { + var treeview = $("#treeview"); + + // 表单提交前设定规则集合 Form.api.bindevent($("form[role=form]"), null, null, function () { - if ($("#treeview").length > 0) { - var r = $("#treeview").jstree("get_all_checked"); + if (treeview.length > 0) { + var r = treeview.jstree("get_all_checked"); $("input[name='row[rules]']").val(r.join(',')); } return true; }); - //渲染权限节点树 + //变更级别后需要重建节点树 $(document).on("change", "select[name='row[pid]']", function () { var pid = $(this).data("pid"); @@ -96,37 +100,21 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ( Backend.api.toastr.error(__('Can not change the parent to self')); return false; } - $.ajax({ - url: "auth/group/roletree", - type: 'post', - dataType: 'json', - data: {id: id, pid: $(this).val()}, - success: function (ret) { - if (ret.hasOwnProperty("code")) { - var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : ""; - if (ret.code === 1) { - //销毁已有的节点树 - $("#treeview").jstree("destroy"); - Controller.api.rendertree(data); - } else { - Backend.api.toastr.error(ret.msg); - } - } - }, error: function (e) { - Backend.api.toastr.error(e.message); - } - }); + treeview.jstree(true).refresh(false); }); + //全选和展开 $(document).on("click", "#checkall", function () { - $("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all"); + treeview.jstree($(this).prop("checked") ? "check_all" : "uncheck_all"); }); $(document).on("click", "#expandall", function () { - $("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all"); + treeview.jstree($(this).prop("checked") ? "open_all" : "close_all"); }); - $("select[name='row[pid]']").trigger("change"); + + //首次渲染 + Controller.api.rendertree(); }, - rendertree: function (content) { + rendertree: function () { $("#treeview") .on('redraw.jstree', function (e) { $(".layer-footer").attr("domrefresh", Math.random()); @@ -150,7 +138,32 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ( "plugins": ["checkbox", "types"], "core": { 'check_callback': true, - "data": content + 'strings': { + 'Loading ...': __('Loading') + }, + "data": function (obj, callback) { + var pidObj = $("select[name='row[pid]']"); + $.ajax({ + url: "auth/group/roletree", + type: 'post', + dataType: 'json', + data: {id: pidObj.data('id'), pid: pidObj.val()}, + success: function (ret) { + if (ret.hasOwnProperty("code")) { + var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : ""; + if (ret.code === 1) { + callback(data); + } else { + Backend.api.toastr.error(ret.msg); + callback([]); + } + } + }, error: function (e) { + Backend.api.toastr.error(e.message); + callback([]); + } + }); + } } }); }