DataController.class.php 7.65 KB
<?php
/**
 * DataController.class.php
 * 推荐数据获取
 * @author Deepseath
 * @version $Id$
 */
namespace Api\Controller\Recommender;

use Common\Common\DataCenter;
use Common\Model\CommonRecommenderModel;
use Common\Service\CommonRecommenderService;
use Common\Common\DataRight;
use Common\Service\CommonRecommenderRightService;

class DataController extends AbstractController
{

    /**
     * 获取数据的条数
     * @var int
     */
    private $__limit = 6;

    /**
     * 当前获取数据开始行数
     * @var int
     */
    private $__start = 0;

    /**
     * 当前获取的数据类型
     * @see \Common\Model\CommonRecommenderModel
     * @var int
     */
    private $__type = 0;

    /**
     * 权限操作对象
     * @var object
     */
    protected $_dataRightService;

    /**
     * 构造方法
     */
    public function __construct()
    {
        // 当前请求的类型
        $type = I('type', 0, 'intval');
        // 当前请求的条数
        $limit = I('limit', 6, 'intval');
        // 当前请求的页码
        $page = I('page', 1, 'intval');

        // 定义有效的数据类型列表
        $this->_recommenderService = new CommonRecommenderService();
        // 规范请求类型值
        if ($this->_recommenderService->is_type($type)) {
            $this->__type = $type;
        }

        // 规范请求的数据条数
        if ($limit >= CommonRecommenderModel::LIMIT_MIN && $limit <= CommonRecommenderModel::LIMIT_MAX) {
            $this->__limit = $limit;
        }

        // 请求数据开始的行号
        if ($page > 1) {
            $this->__start = ($page - 1) * $this->__limit;
        }

        // 调用底层初始化
        if (!parent::__construct()) {

            return false;
        }

        return true;
    }

    /**
     * 获取推荐数据
     * @desc 可获取指定类型的推荐数据用于前端展示
     *
     * @param string type:false:0 获取的数据类型:0=全部;1=banner;2=icon;3=内容推荐
     * @param int limit:false:6 获取的数据条数
     * @param int page:false:1 获取数据的页码,<strong style="color: red">注意:设置该值后,type 参数值不能为0</strong>
     * @param int source:false:0 访问来源:1=微信;2=企业微信
     * @return <pre>
     * array(
     *  'banner' => array(
     *      'total' => 9,// 数据总数
     *      'pages' => 2, // 总页码
     *      'list' => array(
     *          array(
     *              'title' => '标题文字', // 标题
     *              'pic' => 'http://img', // 图片 URL
     *              'url' => '/new/html/', // 链接地址
     *              'time' => 1494406601000, // 发布时间戳
     *              'key' => '9f80d3441314eb53856d8ebb42e60a30' // 系统数据唯一键值,用于前端标记之用
     *          ),
     *          array()
     *      )
     *  ),
     *  'icon' => array(
     *      'total' => 9,// 数据总数
     *      'pages' => 2, // 总页码
     *      'list' => array(
     *          array(
     *              'title' => '标题文字', // 标题
     *              'pic' => 'http://img', // 图片 URL
     *              'url' => '/new/html/', // 链接地址
     *              'time' => 1494406601000, // 发布时间戳
     *              'key' => '9f80d3441314eb53856d8ebb42e60a30' // 系统数据唯一键值,用于前端标记之用
     *          ),
     *          array()
     *      )
     *  ),
     *  'article' => array(
     *      'total' => 9,// 数据总数
     *      'pages' => 2, // 总页码
     *      'list' => array(
     *          array(
     *              'title' => '标题文字', // 标题
     *              'pic' => 'http://img', // 图片 URL
     *              'url' => '/new/html/', // 链接地址
     *              'time' => 1494406601000, // 发布时间戳
     *              'key' => '9f80d3441314eb53856d8ebb42e60a30' // 系统数据唯一键值,用于前端标记之用
     *          ),
     *          array()
     *      )
     *  )
     * )
     * </pre>
     */
    public function Index()
    {
        $objService = new CommonRecommenderRightService();
        $this->_dataRightService = new DataRight($objService, ['field_data_id' => 'recommender_id']);

        // 遍历所有类型来获取数据
        foreach ($this->_recommenderService->typeList as $_type) {
            if ($this->__type && $_type != $this->__type) {
                // 如果指定了获取具体类型的数据,且不是当前进程的类型,则忽略
                continue;
            }
            // 取出指定类型的数据列表
            $data = $this->_getType($_type);
            $this->_result[$_type] = $this->__format($data);
        }
        unset($_type);

        // 数据中心记录活跃用户
        $source = I('source', 0, 'intval');
        if ($source) {
            $dataCenter = &DataCenter::instance();
            $dataCenter->addUserActive($this->_login->user, $source);
        }

        return $this->_result;
    }

    /**
     * 不考虑权限范围获取数据
     * @desc v1.0.0 功能
     */
    private function _getAll()
    {
        // 遍历所有类型来获取数据
        foreach ($this->_recommenderService->typeList as $_type) {
            if ($this->__type && $_type != $this->__type) {
                // 如果指定了获取具体类型的数据,且不是当前进程的类型,则忽略
                continue;
            }
            // 取出指定类型的数据列表
            $data = $this->_recommenderService->listByType($_type, CommonRecommenderModel::HIDE_NO, $this->__start, $this->__limit);
            $this->_result[$_type] = $this->__format($data);
        }
        unset($_type);

        return $this->_result;
    }

    /**
     * 根据类型获取对应数据
     * @param integer $type
     * @return array
     */
    private function _getType($type)
    {
        if ($type == CommonRecommenderModel::TYPE_BANNER || $type == CommonRecommenderModel::TYPE_ICON) {
            return $this->_recommenderService->listByType($type, CommonRecommenderModel::HIDE_NO, $this->__start, $this->__limit);
        }
        $conds = [
            'type' => $type,
            'hide' => CommonRecommenderModel::HIDE_NO
        ];
        // 初始化返回结果
        $result = [
            'type' => $type,
            'total' => 0,
            'pages' => 1,
            'list' => []
        ];

        // 计算有权限的数据总数
        $result['total'] = $this->_dataRightService->countByConds($conds, $this->_login->user);
        if ($result['total'] < 1) {
            // 如果没有数据,则直接返回
            return $result;
        }

        // 总页码
        $result['pages'] = ceil($result['total'] / $this->__limit);

        // 获取具有权限的数据 ID
        $ids = $this->_dataRightService->listIdsByConds($conds, $this->_login->user, [
                $this->__start,
                $this->__limit
            ], [
                'displayorder' => 'DESC',
                'dateline' => 'DESC'
            ]);
        // 根据数据 ID 获取数据详情列表
        $result['list'] = $this->_recommenderService->listByIds($ids);
        unset($ids);

        return $result;
    }

    /**
     * 格式化列表数据
     *
     * @param array $data
     */
    private function __format($data)
    {
        $result = [
            'type' => $data['type'],
            'total' => $data['total'],
            'pages' => $data['pages'],
            'list' => []
        ];
        foreach ($data['list'] as $_data) {
            $result['list'][] = $this->_recommenderService->format($_data);
        }

        return $result;
    }
}