1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace app\admin\model;
use think\Exception;
use think\model\concern\SoftDelete;
class PermissionModel extends BaseModel
{
use SoftDelete;
protected $deleteTime = 'delete_time';
protected $table = 'permission';
private $fields = 'id,title,pid,model_name,controller_name,method_name,params,is_nav,sort,status,update_time';
private $ids = [];
private function getPermissionTree($fields = '',$isNav = false)
{
if($fields != ''){
$this->fields = $fields;
}
$map = [];
if($isNav){
$map = ['is_nav'=>1];
}
$data = $this->where($map)->field($this->fields)->select()->order('id asc,sort desc')->toArray();
if(empty($data) || false == $data){
throw new Exception('没有权限数据!');
}
$data = getTree($data);
foreach ($data as &$value){
$value['name'] = str_repeat('--', $value['level']).$value['title'];
}
return $data;
}
public function getPermissionTreeAll(){
return $this->getPermissionTree();
}
public function getPermissionTreePart($fields = 'id,pid,title'){
return $this->getPermissionTree($fields);
}
public function getPermissionTreeNav($fields='id,pid,title,is_nav'){
return $this->getPermissionTree($fields,true);
}
public function getIds($id=0){
//通过递归循环取出所有id数据
//循环取出所有pid=id的ids
$this->ids[] = $id;
$this->getModelIds($id);
return $this->ids;
}
public function delRowByIds($id,$type=true){
$ids = $this->getIds($id);
return self::destroy($ids,$type);
}
/**
* 获取包含所有子节点的ID数组
* @param $pid
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
private function getModelIds($pid){
$data = $this->field('id,pid')->where(['pid'=>$pid])->select();
if($data){
foreach ($data as $value){
$this->ids[] = $value['id'];
$this->getModelIds($value['id']);
}
} else {
return ;
}
}
public function getFeaturePermissionList(){
$permissionList = [];
$oneLevelPermission = $this->field('id,title')->where(['pid'=>0])->select()->toArray();
foreach ($oneLevelPermission as $value){
$twoLevelPermission = $this->field('id,title')->where(['pid'=>$value['id']])->select()->toArray();
$permissionList[] = [
'parent'=> $value,
'child'=>$twoLevelPermission
];
}
return $permissionList;
}
public function getNodesTree($fields = 'id,pid,title',$isNav = false){
if($fields != ''){
$this->fields = $fields;
}
$data = $this->field($this->fields)->select()->order('id asc')->toArray();
if(empty($data) || false == $data){
throw new Exception('没有权限数据!');
}
$data = getNodesTree($data);
// return json_encode((object)$data);
return $data;
}
public function getPermissionstatusById($id){
$Permissions = $this->field('status')->where(['id'=>$id])->find()->toArray();
return $Permissions;
}
public function setPermissionStatus($id,$status){
$Permissions = $this->allowField(true)->save(['status'=>$status],['id' => $id]);
return $Permissions;
}
}