ListController.class.php 4.46 KB
<?php
/**
 * 直播活动列表
 * Created by PhpStorm.
 * User: yingcai
 * Date: 2018/1/8
 * Time: 下午4:23
 */

namespace Api\Controller\Room;

use Common\Common\Constant;
use Common\Common\User;
use Common\Service\MainService;
use Common\Service\RoleService;

class ListController extends AbstractController
{
    /**
     * List
     * @author houyingcai
     * @desc 直播活动列表接口
     * @param Int page:false:1 页码
     * @param Int limit:false:20 每页记录数
     * @return array|bool 直播列表
                array(
                    'page' => 1, // 页码
                    'limit' => 5, // 每页记录数
                    'total' => 20, // 记录总数
                    'list' => array(
                        array(
                            'lm_id' => 1, // 直播ID
                            'name' => '产品培训讲解', // 直播名称
                            'pic' => 'b3ddbc502e307665f346cbd6e52cc10d', // 封面ID
                            'pic_url' => 'http://qy.vchangyi.org', // 封面图片URL
                            'live_status' => 1, // 直播状态(1=草稿;2=未开始;3=进行中;4=已结束)
                            'lecturer' => '王宇', // 讲师姓名
                            'avatar' => 'http://qy.vchangyi.org', // 讲师头像URL
                            'lecturer_title' => '金牌讲师', // 讲师头衔
                            'watched_total' => 150, // 当前观看直播总人数
                            'estimated_duration' => 45, // 预计时长(单位:分钟)
                        ),
                    ),
                )
     */
    public function Index_post()
    {
        // 判断分页参数是否为空,为空赋默认值
        $page = I('post.page', Constant::PAGING_DEFAULT_PAGE, 'rintval');
        $limit = I('post.limit', Constant::PAGING_DEFAULT_LIMIT, 'rintval');
        $liveMainServ = new MainService();

        // 查询条件
        $conditions = [
            'live_status > ?' => Constant::LIVE_STATUS_DEAFT,
        ];

        // 获取记录总数
        $total = $liveMainServ->count_by_conds($conditions);
        $list = [];
        // 记录总数不为空:有数据
        if ($total) {
            // 分页
            list($start, $limit) = page_limit($page, $limit);
            // 分页参数
            $pageOption = [$start, $limit];
            // 排序
            $orderOption['live_status'] = 'ASC';
            // 查询字段
            $fields = 'lm_id,name,pic,live_status,start_time,estimated_duration,watched_total';
            // 获取列表数据
            $list = $liveMainServ->list_by_conds($conditions, $pageOption, $orderOption, $fields);

            $roleServ = new RoleService();
            // 直播ID列表
            $lmIds = array_column($list, 'lm_id');
            // 获取讲师列表
            $roleList = $roleServ->list_by_conds([
                'lm_id' => $lmIds
            ]);
            // 转换成以直播ID为键的数组
            $roleList = array_combine_by_key($roleList, 'lm_id');

            // 讲师UID列表
            $roleUids = array_column($roleList, 'obj_id');
            // 获取讲师信息
            $userServ = &User::instance();
            $roleInfoList = $userServ->listByUid($roleUids);

            // 格式化列表数据
            foreach ($list as &$v) {
                $lecturer_uid = $roleList[$v['lm_id']]['obj_id'];
                if ($roleList[$v['lm_id']]['type'] == Constant::LIVE_ROLE_TYPE_DEFAULT) {
                    $v['lecturer'] = $lecturer_uid;
                    // 系统管理员没有头像
                    $v['avatar'] =  '';
                } else {
                    // 获取讲师个人信息
                    $v['lecturer'] = $roleInfoList[$lecturer_uid]['memUsername'];
                    $v['avatar'] = $userServ->avatar($lecturer_uid, $roleInfoList[$lecturer_uid]);
                }

                $v['lecturer_title'] = $roleList[$v['lm_id']]['title'];
                // 封面图URL
                $v['pic_url'] = $liveMainServ->formatCover($v['pic']);
                // 转换状态
                $v['live_status'] = $liveMainServ->live_status($v['live_status'], $v['start_time']);
            }
        }

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

        return true;
    }
}