BaseController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/GPL-3.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\base;
  9. use think\App;
  10. use think\exception\HttpResponseException;
  11. use think\facade\Cache;
  12. use think\facade\Db;
  13. use think\facade\Request;
  14. use think\facade\Session;
  15. use think\facade\View;
  16. use systematic\Systematic;
  17. /**
  18. * 控制器基础类
  19. */
  20. abstract class BaseController
  21. {
  22. /**
  23. * Request实例
  24. * @var \think\Request
  25. */
  26. protected $request;
  27. /**
  28. * 应用实例
  29. * @var \think\App
  30. */
  31. protected $app;
  32. /**
  33. * 是否批量验证
  34. * @var bool
  35. */
  36. protected $batchValidate = false;
  37. /**
  38. * 控制器中间件
  39. * @var array
  40. */
  41. protected $middleware = [];
  42. /**
  43. * 构造方法
  44. * @access public
  45. * @param App $app 应用对象
  46. */
  47. public function __construct(App $app)
  48. {
  49. $this->app = $app;
  50. $this->request = $this->app->request;
  51. $this->module = strtolower(app('http')->getName());
  52. $this->controller = strtolower($this->request->controller());
  53. $this->action = strtolower($this->request->action());
  54. $this->uid = 0;
  55. $this->did = 0;
  56. $this->pid = 0;
  57. // 控制器初始化
  58. $this->initialize();
  59. }
  60. // 初始化
  61. protected function initialize()
  62. {
  63. // 检测权限
  64. $this->checkLogin();
  65. }
  66. /**
  67. *验证用户登录
  68. */
  69. protected function checkLogin()
  70. {
  71. if ($this->controller !== 'login' && $this->controller !== 'captcha') {
  72. $session_admin = get_config('app.session_admin');
  73. if (!Session::has($session_admin)) {
  74. if ($this->request->isAjax()) {
  75. return to_assign(404, '请先登录');
  76. } else {
  77. redirect('/home/login/index.html')->send();
  78. exit;
  79. }
  80. } else {
  81. $this->uid = Session::get($session_admin);
  82. $login_admin = Db::name('Admin')->where(['id' => $this->uid])->find();
  83. $this->did = $login_admin['did'];
  84. $this->pid = $login_admin['position_id'];
  85. View::assign('login_admin', $login_admin);
  86. // $is_lock = $login_admin['is_lock'];
  87. // if($is_lock==1){
  88. // redirect('/home/login/lock.html')->send();
  89. // exit;
  90. // }
  91. // 验证用户访问权限
  92. if (($this->module == 'api') || ($this->module == 'message') || ($this->module == 'home' && $this->controller == 'index')) {
  93. return true;
  94. }
  95. // else{
  96. // $reg_pwd = $login_admin['reg_pwd'];
  97. // if($reg_pwd!==''){
  98. // redirect('/home/index/edit_password.html')->send();
  99. // exit;
  100. // }
  101. // if (!$this->checkAuth()) {
  102. // if ($this->request->isAjax()) {
  103. // return to_assign(405, '你没有权限,请联系管理员或者HR');
  104. // } else {
  105. // echo '<div style="text-align:center;color:red;margin-top:20%;">你没有权限访问,请联系管理员或者人事部</div>';exit;
  106. // }
  107. // }
  108. // }
  109. }
  110. }
  111. }
  112. /**
  113. * 验证用户访问权限
  114. * @DateTime 2020-12-21
  115. * @param string $controller 当前访问控制器
  116. * @param string $action 当前访问方法
  117. * @return [type]
  118. */
  119. protected function checkAuth()
  120. {
  121. //Cache::delete('RulesSrc' . $uid);
  122. $uid = $this->uid;
  123. $GOUGU = new Systematic();
  124. $GOUGU->auth($uid);
  125. $auth_list_all = Cache::get('RulesSrc0');
  126. $auth_list = Cache::get('RulesSrc' . $uid);
  127. $pathUrl = $this->module . '/' . $this->controller . '/' . $this->action;
  128. if (!in_array($pathUrl, $auth_list)) {
  129. return false;
  130. } else {
  131. return true;
  132. }
  133. }
  134. }