报表系统权限控制在中间件的设计中不太合理,需要抽离出来单独设计:

<?php
// 定义 ReportSystemRole 类
class ReportSystemRole
{

    private $cache;

    public function __construct()
    {
        $this->cache = cache('report_system_role');
    }

    // 查询角色列表
    public function whereInAuthName($authName)
    {
        return $this->cache->get('roles', []);
    }

    // 查询角色列表
    public function select()
    {
        return $this->cache->get('roles', []);
    }

    // 检查角色是否已存在
    public function containsKey($authName)
    {
        return $this->cache->get('containsKey', false);
    }

    // 添加角色到缓存中
    public function saveAll($roleData)
    {
        $roleData = array_column($roleData, 'auth_name');
        foreach ($this->roles as $authName) {
            if (!in_array($authName, $roleData)) {
                $roleData[] = ['auth_name' => $authName];
            }
        }
        return $this->cache->save('roles', $roleData);
    }
}

check.php

// 在中间件中使用 ReportSystemRole 类
$reportSystemRole = new ReportSystemRole();
if ($reportSystemRole->containsKey('admin')) {
    // 添加管理员角色
} else {
    // 添加其他角色
}


欢迎关注公-众-号【TaonyDaily】、留言、评论,一起学习。

作者:刘俊涛的博客