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