ListController.class.php 2.57 KB
<?php
/**
 * 【业绩比拼-手机端】回复列表接口
 *
 * User: daijun
 * Date: 2017-11-05
 */

namespace Api\Controller\Reply;

use Common\Service\CommentService;
use Common\Service\ReplyService;

class ListController extends \Api\Controller\AbstractController
{

    /** @var ReplyService */
    protected $_reply_serv;
    /** @var CommentService */
    protected $_comment_serv;

    public function before_action($action = '')
    {

        if (!parent::before_action($action)) {

            return false;
        }

        // 实例化活动信息表
        $this->_reply_serv = new ReplyService();
        // 分享信息表
        $this->_comment_serv = new CommentService();

        return true;
    }

    public function Index_get()
    {

        // 获取参数
        $params = I('get.');
        $comment_id = $params['comment_id'];

        if (empty($this->uid)) {
            // 判断是否外部人员
            E('_EMPTY_USER_ID');
        }

        if (empty($comment_id)) {
            // 回帖id不能为空
            E('_EMPTY_ACTIVITY_ID');
        }

        // 获取分享详细内容(已审核通过)
        $info = $this->_comment_serv->get_by_conds(['comment_id' => $comment_id, 'check_status' => CommentService::CHECK_OK]);

        if (empty($info)) {
            // 数据不存在时抛错
            E('_ERR_COMMENT_NOT_EXIST');
        }

        // 默认值
        $page = isset($params['page']) ? intval($params['page']) : ReplyService::DEFAULT_PAGE;
        $limit = isset($params['limit']) ? intval($params['limit']) : ReplyService::DEFAULT_LIMIT;
        // 分页
        list($start, $limit) = page_limit($page, $limit);

        // 查询条件
        $conds = ['comment_id' => $comment_id];
        // 查询总记录数
        $total = $this->_reply_serv->count_by_conds($conds);

        $list = [];
        if ($total > 0) {
            // 排序:发布时间倒序
            $order_option = ['created' => 'DESC'];
            // 分页
            $page_option = [$start, $limit];
            // 查询字段
            $fields = 'reply_id,uid,content,pic_ids,likes,created';
            // 查询列表
            $reply_list = $this->_reply_serv->list_by_conds($conds, $page_option, $order_option, $fields);
            // 格式化列表数据
            $list = $this->_reply_serv->format_reply_list($reply_list, $this->uid);
        }

        $this->_result = [
            'total' => intval($total),
            'limit' => intval($limit),
            'page' => intval($page),
            'list' => $list
        ];

        return true;
    }
}