DataController.class.php
7.65 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/**
* DataController.class.php
* 推荐数据获取
* @author Deepseath
* @version $Id$
*/
namespace Api\Controller\Recommender;
use Common\Common\DataCenter;
use Common\Model\CommonRecommenderModel;
use Common\Service\CommonRecommenderService;
use Common\Common\DataRight;
use Common\Service\CommonRecommenderRightService;
class DataController extends AbstractController
{
/**
* 获取数据的条数
* @var int
*/
private $__limit = 6;
/**
* 当前获取数据开始行数
* @var int
*/
private $__start = 0;
/**
* 当前获取的数据类型
* @see \Common\Model\CommonRecommenderModel
* @var int
*/
private $__type = 0;
/**
* 权限操作对象
* @var object
*/
protected $_dataRightService;
/**
* 构造方法
*/
public function __construct()
{
// 当前请求的类型
$type = I('type', 0, 'intval');
// 当前请求的条数
$limit = I('limit', 6, 'intval');
// 当前请求的页码
$page = I('page', 1, 'intval');
// 定义有效的数据类型列表
$this->_recommenderService = new CommonRecommenderService();
// 规范请求类型值
if ($this->_recommenderService->is_type($type)) {
$this->__type = $type;
}
// 规范请求的数据条数
if ($limit >= CommonRecommenderModel::LIMIT_MIN && $limit <= CommonRecommenderModel::LIMIT_MAX) {
$this->__limit = $limit;
}
// 请求数据开始的行号
if ($page > 1) {
$this->__start = ($page - 1) * $this->__limit;
}
// 调用底层初始化
if (!parent::__construct()) {
return false;
}
return true;
}
/**
* 获取推荐数据
* @desc 可获取指定类型的推荐数据用于前端展示
*
* @param string type:false:0 获取的数据类型:0=全部;1=banner;2=icon;3=内容推荐
* @param int limit:false:6 获取的数据条数
* @param int page:false:1 获取数据的页码,<strong style="color: red">注意:设置该值后,type 参数值不能为0</strong>
* @param int source:false:0 访问来源:1=微信;2=企业微信
* @return <pre>
* array(
* 'banner' => array(
* 'total' => 9,// 数据总数
* 'pages' => 2, // 总页码
* 'list' => array(
* array(
* 'title' => '标题文字', // 标题
* 'pic' => 'http://img', // 图片 URL
* 'url' => '/new/html/', // 链接地址
* 'time' => 1494406601000, // 发布时间戳
* 'key' => '9f80d3441314eb53856d8ebb42e60a30' // 系统数据唯一键值,用于前端标记之用
* ),
* array()
* )
* ),
* 'icon' => array(
* 'total' => 9,// 数据总数
* 'pages' => 2, // 总页码
* 'list' => array(
* array(
* 'title' => '标题文字', // 标题
* 'pic' => 'http://img', // 图片 URL
* 'url' => '/new/html/', // 链接地址
* 'time' => 1494406601000, // 发布时间戳
* 'key' => '9f80d3441314eb53856d8ebb42e60a30' // 系统数据唯一键值,用于前端标记之用
* ),
* array()
* )
* ),
* 'article' => array(
* 'total' => 9,// 数据总数
* 'pages' => 2, // 总页码
* 'list' => array(
* array(
* 'title' => '标题文字', // 标题
* 'pic' => 'http://img', // 图片 URL
* 'url' => '/new/html/', // 链接地址
* 'time' => 1494406601000, // 发布时间戳
* 'key' => '9f80d3441314eb53856d8ebb42e60a30' // 系统数据唯一键值,用于前端标记之用
* ),
* array()
* )
* )
* )
* </pre>
*/
public function Index()
{
$objService = new CommonRecommenderRightService();
$this->_dataRightService = new DataRight($objService, ['field_data_id' => 'recommender_id']);
// 遍历所有类型来获取数据
foreach ($this->_recommenderService->typeList as $_type) {
if ($this->__type && $_type != $this->__type) {
// 如果指定了获取具体类型的数据,且不是当前进程的类型,则忽略
continue;
}
// 取出指定类型的数据列表
$data = $this->_getType($_type);
$this->_result[$_type] = $this->__format($data);
}
unset($_type);
// 数据中心记录活跃用户
$source = I('source', 0, 'intval');
if ($source) {
$dataCenter = &DataCenter::instance();
$dataCenter->addUserActive($this->_login->user, $source);
}
return $this->_result;
}
/**
* 不考虑权限范围获取数据
* @desc v1.0.0 功能
*/
private function _getAll()
{
// 遍历所有类型来获取数据
foreach ($this->_recommenderService->typeList as $_type) {
if ($this->__type && $_type != $this->__type) {
// 如果指定了获取具体类型的数据,且不是当前进程的类型,则忽略
continue;
}
// 取出指定类型的数据列表
$data = $this->_recommenderService->listByType($_type, CommonRecommenderModel::HIDE_NO, $this->__start, $this->__limit);
$this->_result[$_type] = $this->__format($data);
}
unset($_type);
return $this->_result;
}
/**
* 根据类型获取对应数据
* @param integer $type
* @return array
*/
private function _getType($type)
{
if ($type == CommonRecommenderModel::TYPE_BANNER || $type == CommonRecommenderModel::TYPE_ICON) {
return $this->_recommenderService->listByType($type, CommonRecommenderModel::HIDE_NO, $this->__start, $this->__limit);
}
$conds = [
'type' => $type,
'hide' => CommonRecommenderModel::HIDE_NO
];
// 初始化返回结果
$result = [
'type' => $type,
'total' => 0,
'pages' => 1,
'list' => []
];
// 计算有权限的数据总数
$result['total'] = $this->_dataRightService->countByConds($conds, $this->_login->user);
if ($result['total'] < 1) {
// 如果没有数据,则直接返回
return $result;
}
// 总页码
$result['pages'] = ceil($result['total'] / $this->__limit);
// 获取具有权限的数据 ID
$ids = $this->_dataRightService->listIdsByConds($conds, $this->_login->user, [
$this->__start,
$this->__limit
], [
'displayorder' => 'DESC',
'dateline' => 'DESC'
]);
// 根据数据 ID 获取数据详情列表
$result['list'] = $this->_recommenderService->listByIds($ids);
unset($ids);
return $result;
}
/**
* 格式化列表数据
*
* @param array $data
*/
private function __format($data)
{
$result = [
'type' => $data['type'],
'total' => $data['total'],
'pages' => $data['pages'],
'list' => []
];
foreach ($data['list'] as $_data) {
$result['list'][] = $this->_recommenderService->format($_data);
}
return $result;
}
}