DepartmentListController.class.php
2.67 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
<?php
/**
* 微信端选部门接口
* DepartmentListController.class.php
* $author$ 鲜彤
* $date$ 2017年06月08日
*/
namespace Api\Controller\ChooseMem;
use Common\Common\Department;
class DepartmentListController extends AbstractController
{
// 默认页码
const DEFAULT_PAGE = 1;
// 默认页大小
const DEFAULT_LIMIT = 100;
/**
* 部门公共类
*
* @type Department
*/
protected $_department;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
$this->_department = new Department();
return true;
}
public function Index()
{
$dpID = I('post.dpId');
$result = [];
if (empty($dpID)) {
$user = $this->_login->user;
// 由于登录信息中返回的‘isChildDepartment’字段错误,故重新查询保证数据正确
$myDp = $this->_department->listById(array_column($user['dpName'], 'dpId'));
foreach ($myDp as $k => $v) {
$result['list'][] = [
'dpId' => $v['dpId'],
'dpName' => $v['dpName'],
'isChildDepartment' => $v['isChildDepartment'],
];
}
} else {
// 查询部分指定部门
list($departments, $data) = $this->partdp($dpID, self::DEFAULT_PAGE, self::DEFAULT_LIMIT);
$result = array(
'list' => $departments,
);
}
$this->_result = $result;
return true;
}
/**
* 获取部门列表
*
* @param string $dpParentId 上级部门ID
* @param int $page 页码
* @param int $limit 每页条数
*
* @return array
*/
private function partdp($dpParentId, $page, $limit)
{
// 初始化
$data = array();
// 获取部门详情
$dpIds = array_values($this->_department->list_childrens_by_cdid($dpParentId, false, false));
$departments = $this->_department->listAll();
$start = ($page - 1) * $limit;
$end = $start + $limit;
for (; $start < $end; $start++) {
if (empty($dpIds[$start])) {
break;
}
$currentDp = $departments[$dpIds[$start]];
$data[$currentDp['dpId']] = array(
'dpId' => $currentDp['dpId'],
'dpName' => $currentDp['dpName'],
'isChildDepartment' => $currentDp['isChildDepartment'],
);
}
return array(array_values($data), array('total' => count($dpIds), 'pageNum' => $page, 'pageSize' => $limit));
}
}