EmpBaseController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. namespace app\mobile;
  12. use think\App;
  13. use think\exception\ValidateException;
  14. use think\Validate;
  15. use app\common\model\Worker as WorkerModel;
  16. /**
  17. * 控制器基础类
  18. */
  19. abstract class EmpBaseController
  20. {
  21. /**
  22. * Request实例
  23. * @var \think\Request
  24. */
  25. protected $request;
  26. protected $worker;
  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_admin = [];
  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. $userId = get_user_id();
  63. $type = get_type();
  64. if (empty($userId)) {
  65. my_redirect('/login/index');
  66. }
  67. if (empty($type)) {
  68. my_redirect('/login/type');
  69. } elseif ($type == 1) {
  70. my_redirect('/index/index');
  71. }
  72. $this->worker = WorkerModel::where('userid',$userId)->find();
  73. if (empty($this->worker)) {
  74. my_redirect('/login/empRegister');
  75. }
  76. if ($this->worker->status == 1) {
  77. $this->jump('等待管理员审核',url('/login/setType').'?type=1');
  78. } elseif ($this->worker->status == 2) {
  79. my_redirect('/login/empRegister');
  80. } elseif ($this->worker->status == 3 || $this->worker->status == 4) {
  81. $this->jump('账号异常,请联系管理员',url('/login/setType').'?type=1');
  82. }
  83. }
  84. /**
  85. * 验证数据
  86. * @access protected
  87. * @param array $data 数据
  88. * @param string|array $validate 验证器名或者验证规则数组
  89. * @param array $message 提示信息
  90. * @param bool $batch 是否批量验证
  91. * @return array|string|true
  92. * @throws ValidateException
  93. */
  94. protected function validate(array $data, $validate, array $message = [], bool $batch = false)
  95. {
  96. if (is_array($validate)) {
  97. $v = new Validate();
  98. $v->rule($validate);
  99. } else {
  100. if (strpos($validate, '.')) {
  101. // 支持场景
  102. list($validate, $scene) = explode('.', $validate);
  103. }
  104. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  105. $v = new $class();
  106. if (!empty($scene)) {
  107. $v->scene($scene);
  108. }
  109. }
  110. $v->message($message);
  111. // 是否批量验证
  112. if ($batch || $this->batchValidate) {
  113. $v->batch(true);
  114. }
  115. return $v->failException(true)->check($data);
  116. }
  117. /**
  118. * 错误提示
  119. */
  120. protected function jump($msg = '', $url = null, $wait = 3)
  121. {
  122. if (is_null($url)) {
  123. $url = 'javascript:history.back(-1);';
  124. } else {
  125. $url = "location.href = '" . url($url) . "'";
  126. }
  127. $result = [
  128. 'msg' => $msg,
  129. 'url' => $url,
  130. 'wait' => $wait,
  131. ];
  132. $html = view('/public/jump', $result);
  133. throw new \think\exception\HttpResponseException($html);
  134. }
  135. }