Base.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use app\index\model\RoleModel;
  5. class Base extends Controller{
  6. public function initialize(){
  7. if(empty(session('username')) || empty(session('id'))){
  8. //登录超时
  9. json(json_error_exception('1005'))->send();
  10. }
  11. $this->cacheCheck();
  12. // 检测权限
  13. $control = lcfirst(request()->controller());
  14. $action = lcfirst(request()->action());
  15. if(empty(authCheck($control . '/' . $action))){
  16. json(json_error_exception('1000'))->send();
  17. }
  18. }
  19. private function cacheCheck()
  20. {
  21. $action = cache(session('role_id'));
  22. if(is_null($action) || empty($action)){
  23. // 获取该管理员的角色信息
  24. $roleModel = new RoleModel();
  25. $info = $roleModel->getRoleInfo(session('role_id'));
  26. cache(session('role_id'), $info['action']);
  27. }
  28. }
  29. protected function removRoleCache()
  30. {
  31. $roleModel = new RoleModel();
  32. $roleList = $roleModel->getRole();
  33. foreach ($roleList as $value) {
  34. cache($value['id'], null);
  35. }
  36. }
  37. }