LikeService.class.php
6.61 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/**
* LikeService.class.php
* 活动、评论点赞信息表
* @author: daijun
* @copyright: vchangyi.com
*/
namespace Common\Service;
use Common\Common\User;
use Common\Model\ActivityModel;
use Common\Model\CommentModel;
use Common\Model\LikeModel;
class LikeService extends AbstractService
{
// 活动点赞类型
const ACTIVITY_TYPE = 1;
// 评论点赞类型
const COMMENT_TYPE = 2;
// 发布活动
public static $levels = array(
self::ACTIVITY_TYPE,
self::COMMENT_TYPE,
);
/**
* @var ActivityModel 活动model
*/
protected $_d_activity;
/**
* @var CommentModel 评论model
*/
protected $_d_comment;
// 构造方法
public function __construct()
{
$this->_d = new LikeModel();
$this->_d_activity = new ActivityModel();
$this->_d_comment = new CommentModel();
parent::__construct();
}
/**
* 获取点赞列表
* @param array $params 查询参数
* @return array
*/
public function get_like_list($params = [])
{
// 每页条数
$limit = empty($params['limit']) ? self::DEFAULT_LIMIT : intval($params['limit']);
$page = empty($params['page']) ? 1 : intval($params['page']);
// 参数校验
$cid = intval($params['cid']);
$type = intval($params['type']);
if (!in_array($type, self::$levels)) {
E('_ERR_AC_TYPE');
}
if (!$cid) {
E('_EMPTY_AC_CID');
}
list($start, $limit, $page) = page_limit($page, $limit);
// 分页参数
$page_option = array($start, $limit);
// 排序按照发布时间参数
$order_option = array('created' => 'DESC');
/** 组装搜索条件 */
$conds = array('cid' => $cid, 'type' => $type);
// 返回参数
$fields = 'like_id,uid,cid,type,created';
// 查询总条数
$total = $this->_d->count_by_conds($conds);
$list = [];
$user_list = [];
if ($total > 0) {
// 列表和总数
$list = $this->_d->list_by_conds($conds, $page_option, $order_option, $fields);
$uids = array_column($list, 'uid');
// 查询用户集合
$user = new User();
$users = $user->listAll(['memUids' => $uids]);
$this->user_list($users, $uids);
$user_list = array_combine_by_key($users, 'memUid');
}
// 循环格式化列表数据
$arr = [];
foreach ($list as $val) {
$value = [];
$value['created'] = $val['created'];
$value['like_id'] = $val['like_id'];
$value['uid'] = $val['uid'];
$value['username'] = $user_list[$val['uid']]['memUsername'];
$value['avatar'] = $user_list[$val['uid']]['memFace'];
$arr[] = $value;
}
return array(
'page' => $page,
'limit' => $limit,
'total' => intval($total),
'cid' => $cid,
'type' => $type,
'list' => $arr,
);
}
/**
* 点赞接口
* @param array $param
* @return bool
*/
public function add_like_data($param = [])
{
$type = intval($param['type']);
$cid = intval($param['cid']);
// 验证参数
if (!in_array($type, self::$levels)) {
E('_ERR_AC_TYPE');
}
if (!$cid) {
E('_EMPTY_AC_CID');
}
if (empty($param['uid'])) {
E('_ERR_AC_UID_EMPTY');
}
// 查询点赞记录
$count = $this->count_by_conds(
array(
'uid' => $param['uid'],
'cid' => $cid,
'type' => $type,
)
);
// 已点赞
if ($count) {
E('_ERR_AC_LIKE_END');
}
//点赞操作
try {
// 开始事务
$this->_d->start_trans();
// 插入点赞记录表
$this->_d->insert(array("uid" => $param['uid'], 'cid' => $cid, 'type' => $type));
// 更新主表统计数据
if (self::ACTIVITY_TYPE == $type) {
//活动点赞加一
$this->_d_activity->update($cid, array("likes=likes+(?)" => 1));
} else {
//评论点赞加一
$this->_d_comment->update($cid, array("likes=likes+(?)" => 1));
}
// 提交修改
$this->_d->commit();
} catch (\Think\Exception $e) {
\Think\Log::record($e);
$this->_d->rollback();
E('_ERR_ADD_LIKE_FAILED');
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_d->rollback();
E('_ERR_ADD_LIKE_FAILED');
}
return true;
}
/**
* 取消点赞接口
* @param array $param
* @return bool
*/
public function del_like_data($param = [])
{
$type = intval($param['type']);
$cid = intval($param['cid']);
// 参数校验
if (!in_array($type, self::$levels)) {
E('_ERR_AC_TYPE');
}
if (!$cid) {
E('_EMPTY_AC_CID');
}
if (empty($param['uid'])) {
E('_ERR_AC_UID_EMPTY');
}
// 查询点赞记录
$count = $this->count_by_conds(
array(
'uid' => $param['uid'],
'cid' => $cid,
'type' => $type,
)
);
// 没有点赞记录
if (!$count) {
E('_ERR_AC_UNLIKE_END');
}
//取消点赞操作
try {
// 开始事务
$this->_d->start_trans();
// 删除点赞记录表
$this->_d->delete_by_conds(array('uid' => $param['uid'], 'cid' => $cid, 'type' => $type));
// 更新主表统计数据
if (self::ACTIVITY_TYPE == $type) {
//取消活动点赞减一
$this->_d_activity->update($cid, array("likes=likes-(?)" => 1));
} else {
//取消评论点赞减加一
$this->_d_comment->update($cid, array("likes=likes-(?)" => 1));
}
// 提交修改
$this->_d->commit();
} catch (\Think\Exception $e) {
\Think\Log::record($e);
$this->_d->rollback();
E('_ERR_ADD_LIKE_FAILED');
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_d->rollback();
E('_ERR_ADD_LIKE_FAILED');
}
return true;
}
}