<?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; } }