SearchController.class.php
4.21 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
<?php
/**
* 选人组件-搜索接口
* Created by PhpStorm.
* User: 鲜彤
* Date: 2016/8/26
* Time: 15:00
*/
namespace Apicp\Controller\ChooseMem;
use VcySDK\Service;
use VcySDK\Member;
use Common\Common\Department;
use Common\Common\User;
class SearchController extends AbstractController
{
/**
* 成员类型
*/
const TYPE_MEMBER = 1;
/**
* 部门类型
*/
const TYPE_DEPARTMENT = 2;
/**
* 标签类型
*/
const TYPE_TAG = 3;
/**
* 常用类型
*/
const TYPE_COMMONLY_USED = 4;
/**
* 岗位类型
*/
const TYPE_JOB = 5;
/**
* 角色类型
*/
const TYPE_ROLE = 6;
/**
* 类型数字标识(UC用) => 类型字符串标识(前端用)
*/
const TYPE_NUMBER_WITH_STRING = [
self::TYPE_MEMBER => 'member',
self::TYPE_DEPARTMENT => 'department',
self::TYPE_TAG => 'tag',
self::TYPE_COMMONLY_USED => 'commonly_used',
self::TYPE_JOB => 'job',
self::TYPE_ROLE => 'role',
];
public function Index()
{
$role = $this->_login->role;
$search = I("post.search");
// 获取参数key
$key = I('post.key', '', 'trim');
$field = I('post.field', []);
// 调用SDK获取列表
$sdk = new Member(Service::instance());
$result = $sdk->searchList(array('name' => $key, 'flags' => $field, 'pageSize' => 50));
// 循环返回列表,更改类型字段值
foreach ($result['list'] as $k => $v) {
// 如果有需要查询某类 并且 返回值的类型不在内, 则剔除
if (!empty($field) && !in_array($v['flag'], $field)) {
unset($result['list'][$k]);
continue;
}
$flag = $v['flag'];
$type = self::TYPE_NUMBER_WITH_STRING[$flag];
// 删掉UC返回的flag键
unset($result['list'][$k]['flag']);
// 添加type键
$result['list'][$k]['type'] = $type;
}
// skip_role 用于跳过角色权限限制
// 默认按管理员可见部门范围进行搜索
if (empty(I('get.skip_role')) && empty($search) && !empty($role['readDpIdList'])) {
$search = [];
$dpServ = &Department::instance();
// 获取管理员可见部门ID,包括子级部门ID
$search['dpIds'] = $dpServ->list_childrens_by_cdid($role['readDpIdList'], true);
$search['dpIds'] = array_values($search['dpIds']);
// 获取管理员可见部门下的所有人员
$userServ = &User::instance();
$users = $userServ->listAll(['dpIdList' => $search['dpIds']]);
if (!empty($users)) {
$search['uids'] = array_column($users, 'memUid');
}
}
// 特殊处理
if (!empty($search)) {
$result['list'] = $this->search($result['list'], $search);
$result['total'] = count($result['list']);
}
$result['list'] = array_values($result['list']);
$this->_result = $result;
return true;
}
/**
* 特殊查询处理
* @param array $list 搜索后的数据列表
* @param array $search
* +array dpIds 部门IDS
* +array uids 人员UIDS
* +array tagIds 标签IDS
* @return array
*/
private function search($list = array(), $search = array())
{
// 初始化返回值
$data = array();
// 遍历结果集
foreach ($list as $key => $v) {
// 如果是人员
if ($v['type'] == 'member') {
if (in_array($v['id'], $search['uids'])) {
$data[] = $v;
}
}
// 如果是部门
if ($v['type'] == 'department') {
if (in_array($v['id'], $search['dpIds'])) {
$data[] = $v;
}
}
// 如果是标签
if ($v['type'] == 'tag') {
if (in_array($v['id'], $search['tagIds'])) {
$data[] = $v;
}
}
}
return $data;
}
}