<?php
namespace app\index\controller;

use think\Controller;
use app\index\model\RoleModel;

class Base extends Controller{

    public function initialize(){

        if(empty(session('username')) || empty(session('id'))){
            //登录超时
            json(json_error_exception('1005'))->send();
        }

        $this->cacheCheck();

        // 检测权限
        $control = lcfirst(request()->controller());
        $action = lcfirst(request()->action());

        if(empty(authCheck($control . '/' . $action))){
            json(json_error_exception('1000'))->send();
        }
    }

    private function cacheCheck()
    {
        $action = cache(session('role_id'));

        if(is_null($action) || empty($action)){

            // 获取该管理员的角色信息
            $roleModel = new RoleModel();
            $info = $roleModel->getRoleInfo(session('role_id'));
            cache(session('role_id'), $info['action']);
        }
    }

    protected function removRoleCache()
    {
        $roleModel = new RoleModel();
        $roleList = $roleModel->getRole();

        foreach ($roleList as $value) {
            cache($value['id'], null);
        }
    }
}