save
This commit is contained in:
parent
e40adb0247
commit
f9b4394d20
145
src/plugin/admin/app/common/Tree.php
Normal file
145
src/plugin/admin/app/common/Tree.php
Normal file
@ -0,0 +1,145 @@
|
||||
<?php
|
||||
namespace plugin\admin\app\common;
|
||||
|
||||
|
||||
class Tree
|
||||
{
|
||||
/**
|
||||
* 数据
|
||||
* @var array
|
||||
*/
|
||||
protected $data = [];
|
||||
|
||||
/**
|
||||
* 哈希树
|
||||
* @var array
|
||||
*/
|
||||
protected $hashTree = [];
|
||||
|
||||
/**
|
||||
* 父级字段名
|
||||
* @var string
|
||||
*/
|
||||
protected $pidName = 'pid';
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @param string $pid_name
|
||||
*/
|
||||
public function __construct($data, string $pid_name = 'pid')
|
||||
{
|
||||
$this->pidName = $pid_name;
|
||||
if (is_object($data) && method_exists($data, 'toArray')) {
|
||||
$this->data = $data->toArray();
|
||||
} else {
|
||||
$this->data = (array)$data;
|
||||
}
|
||||
$this->hashTree = $this->getHashTree();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取子孙节点
|
||||
* @param int $id
|
||||
* @param bool $with_self
|
||||
* @return array
|
||||
*/
|
||||
public function getDescendants(int $id, bool $with_self = false): array
|
||||
{
|
||||
if (!isset($this->hashTree[$id])) {
|
||||
return [];
|
||||
}
|
||||
$items = [];
|
||||
if ($with_self) {
|
||||
$item = $this->hashTree[$id];
|
||||
unset($item['children']);
|
||||
$items[$item['id']] = $item;
|
||||
}
|
||||
foreach ($this->hashTree[$id]['children'] ?? [] as $item) {
|
||||
unset($item['children']);
|
||||
$items[$item['id']] = $item;
|
||||
$items = array_merge($items, $this->getDescendants($item['id']));
|
||||
}
|
||||
return array_values($items);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取哈希树
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
protected function getHashTree(array $data = []): array
|
||||
{
|
||||
$data = $data ?: $this->data;
|
||||
$hash_tree = [];
|
||||
foreach ($data as $item) {
|
||||
$hash_tree[$item['id']] = $item;
|
||||
}
|
||||
foreach ($hash_tree as $index => $item) {
|
||||
if ($item[$this->pidName] && isset($hash_tree[$item[$this->pidName]])) {
|
||||
$hash_tree[$item[$this->pidName]]['children'][$hash_tree[$index]['id']] = &$hash_tree[$index];
|
||||
}
|
||||
}
|
||||
return $hash_tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取树
|
||||
* @param array $include
|
||||
* @return array|null
|
||||
*/
|
||||
public function getTree(array $include = []): ?array
|
||||
{
|
||||
$hash_tree = $this->hashTree;
|
||||
$items = [];
|
||||
if ($include) {
|
||||
$map = [];
|
||||
foreach ($include as $id) {
|
||||
if (!isset($hash_tree[$id])) {
|
||||
continue;
|
||||
}
|
||||
$item = $hash_tree[$id];
|
||||
$max_depth = 100;
|
||||
while ($max_depth-- > 0 && $item[$this->pidName] && isset($hash_tree[$item[$this->pidName]])) {
|
||||
$last_item = $item;
|
||||
$item = $hash_tree[$item[$this->pidName]];
|
||||
$item_id = $item['id'];
|
||||
if (empty($map[$item_id])) {
|
||||
$map[$item_id] = 1;
|
||||
$item['children'] = [];
|
||||
}
|
||||
$item['children'][$last_item['id']] = $last_item;
|
||||
}
|
||||
$items[$item['id']] = $item;
|
||||
}
|
||||
} else {
|
||||
$items = $hash_tree;
|
||||
}
|
||||
$formatted_items = [];
|
||||
foreach ($items as $item) {
|
||||
if (!$item[$this->pidName]) {
|
||||
$formatted_items[] = $item;
|
||||
}
|
||||
}
|
||||
$formatted_items = array_values($formatted_items);
|
||||
foreach ($formatted_items as &$item) {
|
||||
$this->arrayValues($item);
|
||||
}
|
||||
return $formatted_items;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归重建数组下标
|
||||
* @return void
|
||||
*/
|
||||
protected function arrayValues(&$array)
|
||||
{
|
||||
if (!is_array($array) || !isset($array['children'])) {
|
||||
return;
|
||||
}
|
||||
$array['children'] = array_values($array['children']);
|
||||
foreach ($array['children'] as &$child) {
|
||||
$this->arrayValues($child);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -302,7 +302,7 @@ class Crud extends Base
|
||||
* @param $items
|
||||
* @return Response
|
||||
*/
|
||||
protected function formatTree($items): Response
|
||||
protected function formatTree($items, array $include = []): Response
|
||||
{
|
||||
$items_map = [];
|
||||
foreach ($items as $item) {
|
||||
@ -314,7 +314,7 @@ class Crud extends Base
|
||||
];
|
||||
}
|
||||
$tree = new Tree($items_map);
|
||||
return $this->json(0, 'ok', $tree->getTree());
|
||||
return $this->json(0, 'ok', $tree->getTree($include));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -110,7 +110,7 @@ class RoleController extends Crud
|
||||
*/
|
||||
public function rules(Request $request): Response
|
||||
{
|
||||
$role_id = $request->get('role');
|
||||
$role_id = $request->get('id');
|
||||
if (empty($role_id)) {
|
||||
return $this->json(0, 'ok', []);
|
||||
}
|
||||
@ -119,12 +119,11 @@ class RoleController extends Crud
|
||||
return $this->json(0, 'ok', []);
|
||||
}
|
||||
$rules = Rule::get();
|
||||
$ids = [];
|
||||
$include = [];
|
||||
if ($rule_id_string !== '*') {
|
||||
$ids = explode(',', $rule_id_string);
|
||||
$include = explode(',', $rule_id_string);
|
||||
}
|
||||
$tree = new Tree($rules);
|
||||
return $this->json(0, 'ok', $tree->getTree($ids));
|
||||
return $this->formatTree($rules, $include);
|
||||
}
|
||||
|
||||
}
|
||||
|
31
src/plugin/admin/app/model/AdminRole.php
Normal file
31
src/plugin/admin/app/model/AdminRole.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace plugin\admin\app\model;
|
||||
|
||||
use plugin\admin\app\model\Base;
|
||||
|
||||
/**
|
||||
* @property integer $id ID(主键)
|
||||
* @property string $admin_id 管理员id
|
||||
* @property string $role_id 角色id
|
||||
*/
|
||||
class AdminRole extends Base
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'wa_admin_roles';
|
||||
|
||||
/**
|
||||
* The primary key associated with the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -284,9 +284,7 @@
|
||||
|
||||
// 刷新表格数据
|
||||
window.refreshTable = function(param) {
|
||||
table.reloadData("data-table", {
|
||||
scrollPos: "fixed"
|
||||
});
|
||||
treeTable.reload("#data-table");
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -63,7 +63,7 @@
|
||||
// 字段 权限 rules
|
||||
layui.use(["jquery", "xmSelect"], function() {
|
||||
layui.$.ajax({
|
||||
url: "/app/admin/rule/get?type=0,1,2",
|
||||
url: "/app/admin/role/rules?id=1",
|
||||
dataType: "json",
|
||||
success: function (e) {
|
||||
let value = layui.$("#rules").attr("value");
|
||||
@ -93,13 +93,23 @@
|
||||
name: "pid",
|
||||
initValue: initValue,
|
||||
tips: "请选择",
|
||||
toolbar: {show: true, list: ["CLEAR"]},
|
||||
data: e.data,
|
||||
value: "0",
|
||||
model: {"icon":"hidden","label":{"type":"text"}},
|
||||
clickClose: true,
|
||||
radio: true,
|
||||
tree: {show: true,"strict":false,"clickCheck":true,"clickExpand":false,expandedKeys:true},
|
||||
on: function(data){
|
||||
let id = data.arr[0] ? data.arr[0].value : "";
|
||||
if (!id) return;
|
||||
layui.$.ajax({
|
||||
url: '/app/admin/role/rules?id=' + id,
|
||||
dataType: 'json',
|
||||
success: function (res) {
|
||||
layui.xmSelect.get('#rules')[0].update({data:res.data});
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
});
|
||||
|
@ -87,7 +87,7 @@
|
||||
// 字段 权限 rules
|
||||
layui.use(["jquery", "xmSelect"], function() {
|
||||
layui.$.ajax({
|
||||
url: "/app/admin/rule/get?type=0,1,2",
|
||||
url: "/app/admin/role/rules" + location.search,
|
||||
dataType: "json",
|
||||
success: function (e) {
|
||||
let value = layui.$("#rules").attr("value");
|
||||
@ -124,6 +124,17 @@
|
||||
clickClose: true,
|
||||
radio: true,
|
||||
tree: {show: true,"strict":false,"clickCheck":true,"clickExpand":false,expandedKeys:true},
|
||||
on: function(data){
|
||||
let id = data.arr[0] ? data.arr[0].value : "";
|
||||
if (!id) return;
|
||||
layui.$.ajax({
|
||||
url: '/app/admin/role/rules?id=' + id,
|
||||
dataType: 'json',
|
||||
success: function (res) {
|
||||
layui.xmSelect.get('#rules')[0].update({data:res.data});
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user