ListController.class.php
6.12 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
<?php
/**
* 【手机端】04-查看照片墙接口
* Created by PhpStorm.
* @author:wanghuan
* @date:2017-08-30
*/
namespace Api\Controller\Pic;
use Common\Service\PlanService;
use Common\Service\PlanPicService;
use Common\Service\LikeService;
use Common\Service\PlanDoService;
use Common\Service\RightUsersService;
class ListController extends \Api\Controller\AbstractController
{
/** @var PlanService */
protected $plan_s;
/** @var PlanPicService */
protected $pic_s;
/** @var LikeService */
protected $like_s;
/** @var PlanDoService */
protected $plan_do_s;
/** @var RightUsersService */
protected $right_users_s;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
// 实例化培训计划service
$this->plan_s = new PlanService();
// 实例化照片service
$this->pic_s = new PlanPicService();
// 实例化点赞service
$this->like_s = new LikeService();
// 实例化计划完成记录service
$this->plan_do_s = new PlanDoService();
// 实例化员工名单service
$this->right_users_s = new RightUsersService();
return true;
}
/**
* 照片墙-照片列表
*/
public function Index_post()
{
// 接收post参数
$params = I('post.');
// 培训id
$ed_id = $params['ed_id'];
// 培训id为空
if (!$ed_id) {
E('_EMPTY_ED_ID');
}
// 培训详情
$this->get_education($ed_id);
// 适用范围查询条件
$right_cond = [
'ru_uid' => $this->uid,
'ed_id' => $ed_id
];
// 查询当前员工适用范围数据
$user_right = $this->right_users_s->get_by_conds($right_cond);
// 当前员工不在适用范围
if (empty($user_right)) {
E('_ERR_ED_NO_RIGHT');
}
// 照片墙id
$plan_id = $params['plan_id'];
// 照片墙id为空
if (!$plan_id) {
E('_EMPTY_PIC_PLAN_ID');
}
// 照片墙信息
$plan = $this->plan_s->get($plan_id);
if (!$plan) {
E('_ERR_PIC_PLAN_EXIST');
}
// 判断分页参数是否为空,为空赋默认值
$page = !empty($params['page']) ? intval($params['page']) : self::DEFAULT_PAGE;
$limit = !empty($params['limit']) ? intval($params['limit']) : self::DEFAULT_LIMIT;
// 分页
list($start, $limit) = page_limit($page, $limit);
// 照片查询条件
$cond = [
'ed_id' => $ed_id,
'plan_id' => $plan_id
];
// 获取记录总数
$total = $this->pic_s->count_by_conds($cond);
$list = [];
// 记录总数不为空,查询列表数据
if ($total) {
// 分页参数
$page_option = [$start, $limit];
// 排序:点赞量倒序、上传时间倒序
$order_option = [
'pic_likes' => 'DESC',
'pic_id' => 'DESC'
];
// 获取列表数据
$list = $this->pic_s->list_by_conds($cond, $page_option, $order_option);
}
if (!empty($list)) {
// 待新增数据
$plan_do_data = [
'pd_uid' => $this->uid,
'plan_id' => $plan_id,
'ed_id' => $ed_id
];
// plan_do表中添加数据
$this->plan_do_s->insert($plan_do_data);
// 数据格式化
$list = $this->format_data($list);
}
// 返回结果
$this->_result = [
'total' => intval($total),
'page' => intval($page),
'limit' => intval($limit),
'ed_id' => intval($plan['ed_id']),
'plan_id' => intval($plan['plan_id']),
'plan_name' => $plan['plan_name'],
'plan_allow_upload' => intval($plan['plan_allow_upload']),
'pic_list' => $list
];
}
/**
* 格式化照片数据
*
* @param array $pics 待格式化数据
*
* @return array 格式化后数据
*/
protected function format_data($pics)
{
// 获取关注企业员工列表
$user_list = $this->pic_s->get_all_user_by_cache();
// 点赞查询条件
$cond = [
'like_type' => LikeService::LIKE_TYPE_PIC,
'like_uid' => $this->uid,
'ed_id' => $pics[0]['ed_id'],
];
// 当前登录用户点赞的数据
$likes = $this->like_s->list_by_conds($cond);
$likes = array_combine_by_key($likes, 'like_obj_id');
$result = [];
foreach ($pics as $key => $val) {
// 用户头像
$avatar = '';
// 头像显示缺省头像,名称显示"管理员"
$username = '管理员';
// 手机端用户上传
if (PlanPicService::PIC_UPLOAD_TYPE_USER == $val['pic_type']) { // 后台不做关联判断
// 用户信息
$user = $user_list[$val['pic_uid']];
// 用户头像
$avatar = $this->format_avatar($user['memFace']);
// 用户名
$username = $user['memUsername'];
}
// 点赞详情
$like = $likes[$val['pic_id']];
// 默认未点赞
$pic_is_like = LikeService::USER_UNLIKE;
// 点赞存在
if (!empty($like)) {
// 已点赞
$pic_is_like = LikeService::USER_LIKE;
}
$result[$key] = [
'pic_id' => intval($val['pic_id']),
'pic_name' => $this->pic_s->Enter($val['pic_name']),
'pic_url' => imgUrlReal($val['pic_at_id']),
'pic_uid' => $val['pic_uid'],
'pic_avatar' => $avatar,
'pic_username' => $username,
'pic_is_like' => $pic_is_like,
'pic_likes' => intval($val['pic_likes'])
];
}
return $result;
}
}