报表系统代码优化

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

<?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】、留言、评论,一起学习。

文章来源:刘俊涛的博客


若有帮助到您,欢迎点赞、转发、支持,您的支持是对我坚持最好的肯定(_)

posted @ 2023-05-06 11:37  刘俊涛的博客  阅读(11)  评论(0编辑  收藏  举报