BaseController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. declare (strict_types = 1);
  14. namespace app\base;
  15. use think\facade\Cache;
  16. use think\facade\Db;
  17. use think\facade\Request;
  18. use think\facade\Session;
  19. use think\facade\View;
  20. use systematic\Systematic;
  21. /**
  22. * 控制器基础类
  23. */
  24. abstract class BaseController
  25. {
  26. /**
  27. * 是否批量验证
  28. * @var bool
  29. */
  30. protected $batchValidate = false;
  31. /**
  32. * 分页数量
  33. * @var string
  34. */
  35. protected $pageSize = 20;
  36. /**
  37. * 控制器中间件
  38. * @var array
  39. */
  40. protected $middleware = [];
  41. protected $module;
  42. protected $controller;
  43. protected $action;
  44. protected $uid;
  45. protected $did;
  46. protected $pid;
  47. /**
  48. * 构造方法
  49. * @access public
  50. * @param App $app 应用对象
  51. */
  52. protected $model;
  53. public function __construct()
  54. {
  55. $this->module = strtolower(app('http')->getName());
  56. $this->controller = strtolower(Request::controller());
  57. $this->action = strtolower(Request::action());
  58. $this->uid = 0;
  59. $this->did = 0;
  60. $this->pid = 0;
  61. // 控制器初始化
  62. $this->initialize();
  63. }
  64. // 初始化
  65. protected function initialize()
  66. {
  67. // 检测权限
  68. $this->checkLogin();
  69. //每页显示数据量
  70. $this->pageSize = Request::param('limit', \think\facade\Config::get('app.page_size'));
  71. }
  72. /**
  73. *验证用户登录
  74. */
  75. protected function checkLogin()
  76. {
  77. if ($this->controller !== 'login' && $this->controller !== 'captcha') {
  78. $session_admin = get_config('app.session_admin');
  79. if (!Session::has($session_admin)) {
  80. if (request()->isAjax()) {
  81. return to_assign(404, '请先登录');
  82. } else {
  83. redirect('/home/login/index.html')->send();
  84. exit;
  85. }
  86. } else {
  87. $this->uid = Session::get($session_admin);
  88. $login_admin = get_admin($this->uid);
  89. $this->did = $login_admin['did'];
  90. $this->pid = $login_admin['pid'];
  91. $is_lock = $login_admin['is_lock'];
  92. $last_login_time = Db::name('Admin')->where(['id' => $this->uid])->value('last_login_time');
  93. $timeDiff = time() - $last_login_time;
  94. // 如果超过10小时(36000秒),则用户需要重新登录
  95. if ($timeDiff > 36000) {
  96. Session::delete($session_admin);
  97. redirect('/home/login/index.html')->send();
  98. exit;
  99. }
  100. Db::name('Admin')->where(['id' => $this->uid])->update(['last_login_time' => time()]);
  101. if($is_lock==1){
  102. redirect('/home/login/lock.html')->send();
  103. exit;
  104. }
  105. View::assign('login_admin', $login_admin);
  106. $not_check=['index','leaves','outs','overtimes','trips','message'];
  107. // 验证用户访问权限
  108. if ($this->module == 'home' && in_array($this->controller, $not_check)) {
  109. return true;
  110. }
  111. else{
  112. $regPwd = $login_admin['reg_pwd'];
  113. if($regPwd!==''){
  114. redirect('/home/index/edit_password.html')->send();
  115. exit;
  116. }
  117. if (!$this->checkAuth()) {
  118. if (request()->isAjax()) {
  119. return to_assign(405, '你没有权限,请联系管理员或者人事部');
  120. } else {
  121. redirect('/home/index/role')->send();
  122. exit;
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. /**
  130. * 验证用户访问权限
  131. * @DateTime 2020-12-21
  132. * @param string $controller 当前访问控制器
  133. * @param string $action 当前访问方法
  134. * @return [type]
  135. */
  136. protected function checkAuth()
  137. {
  138. //Cache::delete('RulesSrc' . $uid);
  139. $uid = $this->uid;
  140. $GOUGU = new Systematic();
  141. $GOUGU->auth($uid);
  142. $auth_list_all = Cache::get('RulesSrc0');
  143. $auth_list = Cache::get('RulesSrc' . $uid);
  144. $pathUrl = $this->module . '/' . $this->controller . '/' . $this->action;
  145. if (!in_array($pathUrl, $auth_list)) {
  146. return false;
  147. } else {
  148. return true;
  149. }
  150. }
  151. }