LikeService.class.php
8.03 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?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;
use Common\Model\ReplyModel;
class LikeService extends AbstractService
{
// 发布活动
public static $levels = [
self::LIKE_ACTIVITY_TYPE, // 点赞活动
self::LIKE_COMMENT_TYPE, // 点赞回帖
self::LIKE_REPLY_TYPE, // 点赞回复
];
/**
* @var ActivityModel 活动model
*/
protected $_d_activity;
/**
* @var CommentModel 评论model
*/
protected $_d_comment;
/**
* @var ReplyModel 回复model
*/
protected $_d_reply;
// 构造方法
public function __construct()
{
$this->_d = new LikeModel();
$this->_d_activity = new ActivityModel();
$this->_d_comment = new CommentModel();
$this->_d_reply = new ReplyModel();
parent::__construct();
}
/**
* 获取点赞列表
*
* @param array $params 查询参数
* +++ int limit 每页条数
* +++ int page 页码
* +++ int type 点赞类型
* +++ int obj_id 点赞对象ID
*
* @return array
*/
public function get_like_list($params = [])
{
// 每页条数
$limit = !isset($params['limit']) ? self::DEFAULT_LIMIT : intval($params['limit']);
$page = !isset($params['page']) ? self::DEFAULT_PAGE : intval($params['page']);
// 参数校验
$obj_id = intval($params['obj_id']);
$type = intval($params['type']);
if (!in_array($type, self::$levels)) {
// 点赞类型不正确
E('_ERR_LIKE_TYPE');
}
if (!$obj_id) {
// 数据id不能为空
E('_EMPTY_DATA_ID');
}
list($start, $limit, $page) = page_limit($page, $limit);
// 分页参数
$page_option = [$start, $limit];
// 排序按照发布时间参数
$order_option = ['like_id' => 'DESC'];
// 组装查询条件
$conds = ['obj_id' => $obj_id, 'type' => $type];
// 查询总条数
$total = $this->_d->count_by_conds($conds);
$res_list = [];
if ($total > 0) {
// 返回参数
$fields = 'like_id,uid,created';
// 列表和总数
$list = $this->_d->list_by_conds($conds, $page_option, $order_option, $fields);
$uids = array_column($list, 'uid');
$user = User::instance();
// 查询用户集合(包含删除用户)
$user_list = $user->listUsersAll(['memUids' => $uids, 'memAll' => 1]);
// 循环格式化列表数据
foreach ($list as $val) {
$user_info = $user_list[$val['uid']];
$value = [
'like_id' => $val['like_id'],
'uid' => $val['uid'],
'username' => strval($user_info['memUsername']),
'avatar' => $user->avatar($user_info['memUid'], $user_info),
'created' => $val['created']
];
$res_list[] = $value;
}
}
return [
'page' => $page,
'limit' => $limit,
'total' => intval($total),
'list' => $res_list,
];
}
/**
* 点赞接口
*
* @param array $param
* +++ int type 点赞类型
* +++ int obj_id 点赞对象数据ID
* +++ string uid 点赞人uid
*
* @return bool
* @throws \Com\service_exception
*/
public function add_like_data($param = [])
{
$type = intval($param['type']);
$obj_id = intval($param['obj_id']);
if (!in_array($type, self::$levels)) {
// 点赞类型不对
E('_ERR_LIKE_TYPE');
}
if (!$obj_id) {
// 数据id不为空
E('_EMPTY_DATA_ID');
}
// 查询点赞记录
$count = $this->count_by_conds(
[
'uid' => $param['uid'],
'obj_id' => $obj_id,
'type' => $type,
]
);
if ($count) {
// 已经点过赞
E('_ERR_AC_LIKE_END');
}
try {
// 开始事务
$this->_d->start_trans();
// 插入点赞记录表
$this->_d->insert(['uid' => $param['uid'], 'obj_id' => $obj_id, 'type' => $type]);
// 更新主表统计数据
if (self::LIKE_ACTIVITY_TYPE == $type) {
// 活动点赞+1
$num = $this->_d_activity->update($obj_id, ['likes=likes+(?)' => 1]);
} elseif (self::LIKE_COMMENT_TYPE == $type) {
// 回帖点赞+1
$num = $this->_d_comment->update($obj_id, ['likes=likes+(?)' => 1]);
} else {
// 回复点赞+1
$num = $this->_d_reply->update($obj_id, ['likes=likes+(?)' => 1]);
}
if (!$num) {
// 未更新成功【逻辑回滚】
$this->_d->rollback();
E('_ERR_ADD_LIKE_FAILED');
}
// 提交修改
$this->_d->commit();
} catch (\Think\Exception $e) {
\Think\Log::record($e);
$this->_d->rollback();
E('_ERR_ADD_LIKE_FAILED');
return false;
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_d->rollback();
E('_ERR_ADD_LIKE_FAILED');
return false;
}
return true;
}
/**
* 取消点赞接口
*
* @param array $param
* +++ int type 点赞类型
* +++ int obj_id 点赞对象数据ID
* +++ string uid 点赞人uid
*
* @return bool
*
* @throws \Com\service_exception
*/
public function del_like_data($param = [])
{
$type = intval($param['type']);
$obj_id = intval($param['obj_id']);
if (!in_array($type, self::$levels)) {
// 点赞类型不对
E('_ERR_LIKE_TYPE');
}
if (!$obj_id) {
// 数据id不为空
E('_EMPTY_DATA_ID');
}
// 查询点赞记录
$count = $this->count_by_conds(
[
'uid' => $param['uid'],
'obj_id' => $obj_id,
'type' => $type,
]
);
if (!$count) {
// 没有点赞记录
E('_ERR_AC_UNLIKE_END');
}
try {
// 开始事务
$this->_d->start_trans();
// 删除点赞记录表
$del_num = $this->_d->delete_by_conds(['uid' => $param['uid'], 'obj_id' => $obj_id, 'type' => $type]);
// 更新主表统计数据
if (self::LIKE_ACTIVITY_TYPE == $type) {
// 活动点赞数-1
$num = $this->_d_activity->update($obj_id, ['likes=likes-(?)' => 1]);
} elseif (self::LIKE_COMMENT_TYPE == $type) {
// 回帖点赞数-1
$num = $this->_d_comment->update($obj_id, ['likes=likes-(?)' => 1]);
} else {
// 回复点赞数-1
$num = $this->_d_reply->update($obj_id, ['likes=likes-(?)' => 1]);
}
if (!$num || !$del_num) {
// 未更新成功【逻辑回滚】
$this->_d->rollback();
E('_ERR_UN_LIKE_FAILED');
}
// 提交修改
$this->_d->commit();
} catch (\Think\Exception $e) {
\Think\Log::record($e);
$this->_d->rollback();
E('_ERR_UN_LIKE_FAILED');
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_d->rollback();
E('_ERR_UN_LIKE_FAILED');
}
return true;
}
}