AuthorityListController.class.php
7.41 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* 权限范围列表查询
* AbstractController.class.php
* $author$
*/
namespace Apicp\Controller\Integral;
use Common\Common\Job;
use Common\Common\Role;
use Common\Common\User;
use Common\Model\AuthorityMainModel;
use Common\Model\AuthorityModel;
use Common\Service\AuthorityMainService;
use Common\Service\AuthorityService;
class AuthorityListController extends AbstractController
{
private $page = null;
private $pageSize = null;
private $pageStart = null;
private $rankFilter = null;
public function index()
{
// 分页参数
$this->page = I('post.page', 1);
$this->pageSize = I('post.limit', 15);
list($this->pageStart, $this->pageSize, $this->page) = page_limit($this->page, $this->pageSize);
// 获取类型设置
$this->rankFilter = $this->getRankFilter();
// 获取列表
$this->getList();
$this->_result['list'] = array_intersect_key_reserved($this->_result['list'], [
'am_id',
'visible_range',
'visible_range_type',
'authority_name',
'visible_range_name'
]);
// 获取总数
$authorityMainServ = new AuthorityMainService();
$this->_result['total'] = $authorityMainServ->count_by_conds([
'visible_range_type' => [
AuthorityMainModel::VISIBLE_RANGE_TYPE_DEFAULT,
$this->rankFilter
]
]);
$this->_result['page'] = $this->page;
return true;
}
/**
* 获取列表数据
* @return bool
*/
private function getList()
{
// 获取可见范围
$authorityMainServ = new AuthorityMainService();
$visibleRangeList = $authorityMainServ->list_by_conds(
[
'visible_range_type' => [
AuthorityMainModel::VISIBLE_RANGE_TYPE_DEFAULT,
$this->rankFilter
]
],
[
$this->pageStart,
$this->pageSize
],
[
'created' => 'DESC'
]
);
$amIdList = array_column($visibleRangeList, 'am_id');
// ID 数组
$uidList = [];
$jobIdList = [];
$roleIdList = [];
// ID 对应数据数组
$memberList = [];
$jobList = [];
$roleList = [];
// 获取可见范围 ID
$visibleRangeTypeWith = [
AuthorityMainModel::VISIBLE_RANGE_TYPE_ROLE => [
'idField' => 'roleId',
'name' => 'roleName',
'ids' => &$roleIdList,
'list' => &$roleList
],
AuthorityMainModel::VISIBLE_RANGE_TYPE_JOB => [
'idField' => 'jobId',
'name' => 'jobName',
'ids' => &$jobIdList,
'list' => &$jobList
],
];
foreach ($visibleRangeList as &$item) {
if (empty($item['visible_range'])) {
continue;
}
$item['visible_range'] = explode(',', $item['visible_range']);
// 合并 ID
$visibleRangeIds = $visibleRangeTypeWith[$item['visible_range_type']]['ids'];
$visibleRangeTypeWith[$item['visible_range_type']]['ids'] =
array_unique(array_merge($visibleRangeIds, $item['visible_range']));
}
unset($item);
// 主表 ID 对应对象数据
$authorityAmIdWithObj = [];
// 获取权限对象 ID
$authorityTypeWith = [
AuthorityModel::AUTHORITY_ID_TYPE_MEMBER => [
'name' => 'memUsername',
'ids' => &$uidList,
'list' => &$memberList
],
AuthorityModel::AUTHORITY_ID_TYPE_ROLE => [
'name' => 'roleName',
'ids' => &$roleIdList,
'list' => &$roleList
],
AuthorityModel::AUTHORITY_ID_TYPE_JOB => [
'name' => 'jobName',
'ids' => &$jobIdList,
'list' => &$jobList
],
];
$authorityServ = new AuthorityService();
$authorityList = $authorityServ->list_by_conds(['am_id' => $amIdList]);
foreach ($authorityList as $item) {
$authorityAmIdWithObj[$item['am_id']][] = $item;
$authorityTypeWith[$item['authority_id_type']]['ids'][] = $item['authority_id'];
}
// 根据ID 获取数据 $memberList, $jobList, $roleList 在下面是以传址来用的
list($memberList, $jobList, $roleList) = $this->getIdObj($uidList, $jobIdList, $roleIdList);
// 拼装列表数据
foreach ($visibleRangeList as &$item) {
// 权限对象拼装
foreach ($authorityAmIdWithObj[$item['am_id']] as $obj) {
// 对象数组
$objList = $authorityTypeWith[$obj['authority_id_type']]['list'];
$objName = $authorityTypeWith[$obj['authority_id_type']]['name'];
if (empty($objList[$obj['authority_id']][$objName])) {
continue;
}
// 获取名称
$item['authority_name'][] = $objList[$obj['authority_id']][$objName];
}
// 对象都被删除时 前端判断为空 显示已删除 标识
$item['authority_name'] = empty($item['authority_name']) ?
'' : implode(';', $item['authority_name']);
foreach ($item['visible_range'] as $key => $id) {
// 所有人的情况
if ($id == AuthorityMainModel::VISIBLE_RANGE_ALL) {
$item['visible_range_name'][] = '所有' . AuthorityMainModel::VISIBLE_RANGE_TYPE_CN[$this->rankFilter];
break;
}
// 获取名称
$objList = $visibleRangeTypeWith[$item['visible_range_type']]['list'];
$objName = $visibleRangeTypeWith[$item['visible_range_type']]['name'];
if (empty($objList[$id][$objName])) {
unset($item['visible_range'][$key]);
continue;
}
$item['visible_range_name'][] = $objList[$id][$objName];
}
$item['visible_range_name'] = empty($item['visible_range_name']) ?
'未设置' : implode(';', $item['visible_range_name']);
}
$this->_result['list'] = $visibleRangeList;
return true;
}
/**
* 根据ID 获取数据
* @param $uidList
* @param $jobIdList
* @param $roleIdList
* @return array
*/
private function getIdObj($uidList, $jobIdList, $roleIdList)
{
$memberList = [];
$jobList = [];
$roleList = [];
// 获取人员、岗位、角色 数据
if (!empty($uidList)) {
$memUtil = new User();
$memberList = $memUtil->listByUid($uidList);
$memberList = array_combine_by_key($memberList, 'memUid');
}
if (!empty($jobIdList)) {
$jobUtil = new Job();
$jobList = $jobUtil->listById($jobIdList);
$jobList = array_combine_by_key($jobList, 'jobId');
}
if (!empty($roleIdList)) {
$roleUtil = new Role();
$roleList = $roleUtil->listById($roleIdList);
$roleList = array_combine_by_key($roleList, 'roleId');
}
return [$memberList, $jobList, $roleList];
}
}