BaseController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\worker;
  13. use think\App;
  14. use think\exception\ValidateException;
  15. use think\Validate;
  16. use think\facade\Session;
  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. * @var array
  45. */
  46. protected $access_worker = [];
  47. /**
  48. * 构造方法
  49. * @access public
  50. * @param App $app 应用对象
  51. */
  52. public function __construct(App $app)
  53. {
  54. $this->app = $app;
  55. $this->request = $this->app->request;
  56. // 控制器初始化
  57. $this->initialize();
  58. }
  59. // 初始化
  60. protected function initialize()
  61. {
  62. // 验证登陆信息
  63. $has_access_worker = Session::has('access_worker');
  64. if ( $has_access_worker ) {
  65. $this->access_worker = Session::get('access_worker');
  66. $REQUEST_URI = $_SERVER['REQUEST_URI'];
  67. if($this->access_worker['wtype'] == 1){
  68. if(stripos($REQUEST_URI,'comjobsformWyp') || stripos($REQUEST_URI,'editComjobsWyp') || stripos($REQUEST_URI,'demandFormWyd') || stripos($REQUEST_URI,'editdemandformWyd')){
  69. $rtn['code'] = 1001;
  70. $rtn['msg'] = "You do not have permission to access.";
  71. return $rtn;
  72. }
  73. }
  74. }else{
  75. echo '<script language="javascript" type="text/javascript"> top.location.href="/worker.php/login/index"; </script>';
  76. exit(json_encode(array(
  77. 'code' => 1001,
  78. 'msg' => "You do not have permission to access."
  79. )));
  80. }
  81. }
  82. /**
  83. * 验证数据
  84. * @access protected
  85. * @param array $data 数据
  86. * @param string|array $validate 验证器名或者验证规则数组
  87. * @param array $message 提示信息
  88. * @param bool $batch 是否批量验证
  89. * @return array|string|true
  90. * @throws ValidateException
  91. */
  92. protected function validate(array $data, $validate, array $message = [], bool $batch = false)
  93. {
  94. if (is_array($validate)) {
  95. $v = new Validate();
  96. $v->rule($validate);
  97. } else {
  98. if (strpos($validate, '.')) {
  99. // 支持场景
  100. list($validate, $scene) = explode('.', $validate);
  101. }
  102. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  103. $v = new $class();
  104. if (!empty($scene)) {
  105. $v->scene($scene);
  106. }
  107. }
  108. $v->message($message);
  109. // 是否批量验证
  110. if ($batch || $this->batchValidate) {
  111. $v->batch(true);
  112. }
  113. return $v->failException(true)->check($data);
  114. }
  115. protected function dealEqualInput($result ,$equal_array)
  116. {
  117. foreach ($equal_array as $k => $v) {
  118. $value = input($v);
  119. if (!empty($value)) {
  120. $result[] = [is_numeric($k) ? $v : $k,'=',$value];
  121. }
  122. }
  123. return $result;
  124. }
  125. protected function dealLikeInput($result,$like_array)
  126. {
  127. foreach ($like_array as $k => $v) {
  128. $value = input($v);
  129. if (!empty($value)) {
  130. $result[] = [is_numeric($k) ? $v : $k,'like',"%{$value}%"];
  131. }
  132. }
  133. return $result;
  134. }
  135. }