AddController.class.php
3.52 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
<?php
/**
* 【手机端】05-照片、评价点赞接口
* Created by PhpStorm.
* @author:wanghuan
* @date:2017-08-30
*/
namespace Api\Controller\Like;
use Common\Service\LikeService;
use Common\Service\CommentService;
use Common\Service\PlanPicService;
class AddController extends \Api\Controller\AbstractController
{
/** @var LikeService */
protected $like_s;
/** @var CommentService */
protected $comment_s;
/** @var PlanPicService */
protected $pic_s;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
// 实例化点赞service
$this->like_s = new LikeService();
// 实例化评论service
$this->comment_s = new CommentService();
// 实例化照片service
$this->pic_s = new PlanPicService();
return true;
}
/**
* 点赞
*/
public function Index_post()
{
// 培训id
$ed_id = I('post.ed_id', 0, 'intval');
// 培训id为空
if (!$ed_id) {
E('_EMPTY_ED_ID');
}
// 培训详情
$this->get_education($ed_id);
// 点赞类型
$like_type = I('post.like_type', 0, 'intval');
// 点赞类型为空
if (!$like_type) {
E('_EMPTY_LIKE_TYPE');
}
// 点赞对象id
$like_obj_id = I('post.like_obj_id', 0, 'intval');
// 点赞对象id为空
if (!$like_obj_id) {
E('_EMPTY_LIKE_OBJ_ID');
}
// 评价点赞
if (LikeService::LIKE_TYPE_COMMENT == $like_type) {
$comment_info = $this->comment_s->get($like_obj_id);
if (empty($comment_info)) {
E('_EMPTY_COMMENT');
}
}
// 照片点赞
if (LikeService::LIKE_TYPE_PIC == $like_type) {
$pic_info = $this->pic_s->get($like_obj_id);
if (empty($pic_info)) {
E('_EMPTY_PIC');
}
}
// 待新增点赞数据
$data = [
'like_type' => $like_type,
'like_obj_id' => $like_obj_id,
'like_uid' => $this->uid,
'ed_id' => $ed_id
];
// 获取用户点赞数据
$like_info = $this->like_s->get_by_conds($data);
// 已经点赞
if (!empty($like_info)) {
E('_ERR_LIKE_EXIST');
}
try {
// 开始事务
$this->like_s->start_trans();
// 点赞
$result = $this->like_s->insert($data);
// 点赞成功,更新点赞量
if ($result) {
// 评价点赞
if (LikeService::LIKE_TYPE_COMMENT == $data['like_type']) {
// 评价的点赞量累加1
$this->comment_s->update_likes($data['like_obj_id'], self::LIKES_INC);
} elseif (LikeService::LIKE_TYPE_PIC == $data['like_type']) { // 照片点赞
// 照片的点赞量累加1
$this->pic_s->update_likes($data['like_obj_id'], self::LIKES_INC);
}
} else { // 点赞失败
$this->like_s->rollback();
}
$this->like_s->commit();
} catch (\Think\Exception $e) {
$this->like_s->rollback();
E('_ERR_LIKE_FAIL');
return false;
}
// 点赞失败
if (!$result) {
E('_ERR_LIKE_FAIL');
}
return true;
}
}