ListController.class.php 4.95 KB
<?php
/**
 * 【手机端】08-评价列表
 * Created by PhpStorm.
 * @author:wanghuan
 * @date:2017-08-31
 */

namespace Api\Controller\Comment;

use Common\Common\User;
use Common\Service\CommentService;
use Common\Service\LikeService;

class ListController extends \Api\Controller\AbstractController
{

    /** @var CommentService */
    protected $comment_s;
    /** @var LikeService */
    protected $like_s;

    public function before_action($action = '')
    {

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

            return false;
        }

        // 实例化评论service
        $this->comment_s = new CommentService();
        // 实例化点赞service
        $this->like_s = new LikeService();

        return true;
    }

    /**
     * 评价列表
     */
    public function Index_post()
    {

        // 接收post参数
        $params = I('post.');

        // 培训id
        $ed_id = I('post.ed_id', 0, 'intval');
        // 培训id为空
        if (!$ed_id) {

            E('_EMPTY_ED_ID');
        }

        // 培训详情
        $education = $this->get_education($ed_id);

        // 判断分页参数是否为空,为空赋默认值
        $page = !empty($params['page']) ? intval($params['page']) : self::DEFAULT_PAGE;
        $limit = !empty($params['limit']) ? intval($params['limit']) : self::DEFAULT_LIMIT;
        // 分页
        list($start, $limit) = page_limit($page, $limit);

        // 评价查询条件
        $cond = [
            'ed_id' => $ed_id,
            'c_content != ?' => ''
        ];
        // 判断是否只能看到本人的评论
        if ($education['ed_comment_show_self']) {

            $cond['c_uid'] = $this->uid;
        }

        // 获取记录总数
        $total = $this->comment_s->count_by_conds($cond);

        $list = [];
        // 记录总数不为空,查询列表数据
        if ($total) {
            // 分页参数
            $page_option = [$start, $limit];
            // 排序:点赞量倒序、上传时间倒序
            $order_option = [
                'c_likes' => 'DESC',
                'c_id' => 'DESC'
            ];
            // 获取列表数据
            $list = $this->comment_s->list_by_conds($cond, $page_option, $order_option);
        }

        // 评价列表不为空
        if (!empty($list)) {
            // 数据格式化
            $list = $this->format_data($list);
        }

        // 返回结果
        $this->_result = [
            'total' => intval($total),
            'page' => intval($page),
            'limit' => intval($limit),
            'list' => $list
        ];
    }

    /**
     * 格式化评价列表数据
     *
     * @param array $comments 待格式化数据
     *
     * @return array 格式化后数据
     */
    protected function format_data($comments)
    {

        // 获取关注企业员工列表
        $user_list = User::instance()->listAll(['memSubscribeStatus' => 1]);
        $user_list = array_combine_by_key($user_list, 'memUid');

        // 点赞查询条件
        $cond = [
            'like_type' => LikeService::LIKE_TYPE_COMMENT,
            'like_uid' => $this->uid,
            'ed_id' => $comments[0]['ed_id']
        ];
        // 当前登录用户点赞的数据
        $likes = $this->like_s->list_by_conds($cond);
        $likes = array_combine_by_key($likes, 'like_obj_id');

        $result = [];
        foreach ($comments as $key => $val) {
            // 用户信息
            $user = $user_list[$val['c_uid']];
            // 用户头像
            $avatar = $this->format_avatar($user['memFace']);
            // 用户名
            $username = $user['memUsername'];

            // 匿名
            if (CommentService::ANONYMOUS == $val['c_is_anonymous']) {
                // 自己发的评价
                if ($this->uid == $val['c_uid']) {
                    // 名称显示格式为:用户名(匿名)
                    $username = $username . '(匿名)';
                } else { // 不是自己发的评价
                    // 前端判断为空显示缺省头像
                    $avatar = '';
                    // 名称显示为:匿名
                    $username = '匿名';
                }
            }

            // 点赞详情
            $like = $likes[$val['c_id']];
            // 默认未点赞
            $is_like = LikeService::USER_UNLIKE;
            // 点赞存在
            if (!empty($like)) {
                // 已点赞
                $is_like = LikeService::USER_LIKE;
            }

            $result[$key] = [
                'c_id' => intval($val['c_id']),
                'c_uid' => $val['c_uid'],
                'avatar' => $avatar,
                'username' => $username,
                'c_content' => $this->comment_s->Enter($val['c_content']),
                'created' => $val['created'],
                'c_likes' => intval($val['c_likes']),
                'is_like' => $is_like
            ];
        }

        return $result;
    }
}