ActiveRankController.class.php
4.5 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
<?php
/**
* Created by PhpStorm.
* User: zhonglei
* Date: 17/10/10
* Time: 16:12
*/
namespace Apicp\Controller\DataRank;
use Com\PackageValidate;
use Common\Common\User;
use Common\Common\Constant;
use Common\Common\DataHelper;
use Common\Model\AnswerAnswerModel;
use Common\Model\LikeModel;
use Common\Model\CommentModel;
class ActiveRankController extends \Apicp\Controller\AbstractController
{
/**
* ActiveRank
* @author zhonglei
* @desc 活跃排行接口
* @param Int starttime:true 开始时间戳
* @param Int endtime:true 结束时间戳
* @return array
array(
'answer' => array( // 问答TOP10
array(
'uid' => 'A', // 用户ID
'username' => '宇宙超级无敌小霸王', // 用户姓名
'face' => '', // 头像
'dp_names' => array( // 部门
'部门1', '部门2'
),
'total' => 100, // 点赞总数
),
),
'like' => array( // 点赞TOP10
array(
'uid' => 'A', // 用户ID
'username' => '宇宙超级无敌小霸王', // 用户姓名
'face' => '', // 头像
'dp_names' => array( // 部门
'部门1', '部门2'
),
'total' => 100, // 点赞总数
),
),
'comment' => array( // 评论TOP10
array(
'uid' => 'A', // 用户ID
'username' => '宇宙超级无敌小霸王', // 用户姓名
'face' => '', // 头像
'dp_names' => array( // 部门
'部门1', '部门2'
),
'total' => 100, // 点赞总数
),
),
)
*/
public function Index_post()
{
// 请求数据
$post_data = I('post.');
// 验证规则
$rules = [
'starttime' => 'require|integer',
'endtime' => 'require|integer',
];
// 验证请求数据
$validate = new PackageValidate();
$validate->postData = $post_data;
$validate->validateParams($rules);
$dataHelper = &DataHelper::instance();
$starttime = $dataHelper->formatStarttime($post_data['starttime']);
$endtime = $dataHelper->formatEndtime($post_data['endtime']);
$limit = Constant::DATARANK_SHOW_TOTAL * 3;
$conds = [
'created >= ?' => $starttime,
'created <= ?' => $endtime,
];
// 获取问答排行
$answerModel = new AnswerAnswerModel();
$answer_list = $answerModel->listUserAnswerRank($conds, $limit);
$uids = array_column($answer_list, 'uid');
// 获取点赞排行
$likeModel = new LikeModel();
$like_list = $likeModel->listUserRank($conds, $limit);
$uids = array_merge($uids, array_column($like_list, 'uid'));
// 获取评论排行
$commentModel = new CommentModel();
$comment_list = $commentModel->listUserRank($conds, $limit);
$uids = array_merge($uids, array_column($comment_list, 'uid'));
// 获取用户(已被删除的用户无法获取)
$userServ = &User::instance();
$users = $userServ->listByUid($uids);
$data = [
'answer' => $answer_list,
'like' => $like_list,
'comment' => $comment_list,
];
$result = [];
foreach ($data as $k => $list) {
$result[$k] = [];
// 遍历数据,获取用户信息
foreach ($list as $v) {
$uid = $v['uid'];
if (!isset($users[$uid])) {
continue;
}
$result[$k][] = [
'uid' => $uid,
'username' => $users[$uid]['memUsername'],
'face' => $users[$uid]['memFace'],
'dp_names' => array_column($users[$uid]['dpName'], 'dpName'),
'total' => $v['total'],
];
if (count($result[$k]) == Constant::DATARANK_SHOW_TOTAL) {
break;
}
}
}
$this->_result = $result;
}
}