VisibleSearchRangeController.class.php 2.76 KB
<?php

/**
 * 获取管理者可查看的岗位或角色范围
 *
 * @auther zs_anything
 * @date 2017/08/04
 */
namespace Api\Controller\Integral;


use Common\Common\Cache;
use Common\Common\Job;
use Common\Common\Role;
use Common\Model\AuthorityMainModel;
use Common\Model\PluginModel;
use Common\Model\SettingModel;
use Common\Service\SettingService;

class VisibleSearchRangeController extends AbstractController
{

    public function Index()
    {

        $authorRange = $this->checkAuthRange(PluginModel::INTEGRAL_CENTER);

        // 查询系统当前配置的积分排名筛选类型
        $visibleRangeType = AuthorityMainModel::VISIBLE_RANGE_TYPE_JOB;
        $settings = Cache::instance()->get('Common.AppSetting');
        if (!empty($settings) && array_key_exists("rank_filter", $settings)) {
            $visibleRangeType = $settings['rank_filter']['value'];
        }

        $this->formatResultData(intval($visibleRangeType), $authorRange);

        return true;
    }

    /**
     * @param $visibleRangeType
     * @param $authorRange
     */
    private function formatResultData($visibleRangeType, $authorRange)
    {

        // 当前用户没有配置可见的岗位或角色筛选条件
        if (empty($authorRange[PluginModel::INTEGRAL_CENTER])) {
            $this->_result = [
                'list' => [],
                'type' => $visibleRangeType
            ];
        }

        // 按岗位筛选
        if (AuthorityMainModel::VISIBLE_RANGE_TYPE_JOB == $visibleRangeType) {

            $jobUtil = new Job();
            $jobList = $jobUtil->listById($authorRange[PluginModel::INTEGRAL_CENTER]);

            $resJobList = $this->formatJobOrRole($jobList, 'jobId', 'jobName');

            $this->_result = [
                'list' => $resJobList,
                'type' => $visibleRangeType
            ];
        }

        // 按角色筛选
        if (AuthorityMainModel::VISIBLE_RANGE_TYPE_ROLE == $visibleRangeType) {

            $roleUtil = new Role();
            $roleList = $roleUtil->listById($authorRange[PluginModel::INTEGRAL_CENTER]);

            $resRoleList = $this->formatJobOrRole($roleList, 'roleId', 'roleName');

            $this->_result = [
                'list' => $resRoleList,
                'type' => $visibleRangeType
            ];
        }
    }

    /**
     * 格式化岗位或角色返回信息
     * @param $list
     * @param $id
     * @param $name
     * @return array
     */
    private function formatJobOrRole($list, $id, $name)
    {
        $resList = [];

        if (empty($list)) {
            return $resList;
        }

        foreach ($list as $item) {
            $resList[] = [
                'id' => $item[$id],
                'name' => $item[$name],
            ];
        }

        return $resList;
    }


}