AuthRangeController.class.php 2.15 KB
<?php
/**
 * 【考试中心-手机端】获取考试范围接口
 * AuthRangeController.class.php
 */

namespace Api\Controller\Paper;

use Common\Common\Cache;
use Common\Service\PaperService;
use Common\Service\RightService;

class AuthRangeController extends AbstractController
{

    public function Index_post()
    {
        // 接收post参数
        $ep_id = I('post.ep_id');
        // 试卷id为空
        if (empty($ep_id)) {

            E('_EMPTY_EP_ID');
        }

        // 实例化缓存
        $cache =&Cache::instance();
        // 初始化缓存信息
        $auth_info = [];

        try {
            // 获取当前试卷缓存信息
            $auth_info = $cache->get('Common.Exam_wx_' . $ep_id . '_auth');
        } catch (\Think\Exception $e) {

            \Think\Log::record($e);
        } catch (\Exception $e) {

            \Think\Log::record($e);
        }

        // 实例化试卷service
        $paper_s = new PaperService();
        // 根据试卷id查询试卷信息
        $data = $paper_s->get($ep_id);

        // 试卷信息不存在
        if (empty($data)) {

            E('_EMPTY_PAPER_DATA');
        }

        // 如果缓存不存在
        if (empty($auth_info)) {

            // 试卷权限
            $right_data = [];
            // 非全公司
            if (PaperService::NO_AUTH_ALL == $data['is_all']) {
                // 实例化权限service
                $right_s = new RightService();
                // 权限查询条件:试卷id,权限表类型标识(试卷权限)
                $right_conds = [
                    'epc_id' => $ep_id,
                    'er_type' => PaperService::RIGHT_PAPER
                ];
                // 格式化权限数据
                list($list, $right_data) = $right_s->get_right_data($right_conds, true);
            }

            $auth_info = [
                'right' => $right_data,
                'is_all' => (int)$data['is_all']
            ];

            // 如果没有缓存新增缓存
            $cache->set('Common.Exam_wx_' . $ep_id . '_auth', $auth_info, 600);

        }

        // 返回结果
        $this->_result = $auth_info;
    }
}