UserBaseController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\User as UserModel;
  16. /**
  17. * 控制器基础类
  18. */
  19. abstract class UserBaseController
  20. {
  21. /**
  22. * Request实例
  23. * @var \think\Request
  24. */
  25. protected $request;
  26. /**
  27. * 应用实例
  28. * @var \think\App
  29. */
  30. protected $app;
  31. protected $user;
  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 == 2) {
  70. my_redirect('/emp/index');
  71. }
  72. $this->user = UserModel::where('id',$userId)->find();
  73. if (empty($this->user)) {
  74. my_redirect('/login/index');
  75. }
  76. if ($this->user->is_perfect == 2) {
  77. my_redirect('/login/perfect');
  78. }
  79. }
  80. /**
  81. * 验证数据
  82. * @access protected
  83. * @param array $data 数据
  84. * @param string|array $validate 验证器名或者验证规则数组
  85. * @param array $message 提示信息
  86. * @param bool $batch 是否批量验证
  87. * @return array|string|true
  88. * @throws ValidateException
  89. */
  90. protected function validate(array $data, $validate, array $message = [], bool $batch = false)
  91. {
  92. if (is_array($validate)) {
  93. $v = new Validate();
  94. $v->rule($validate);
  95. } else {
  96. if (strpos($validate, '.')) {
  97. // 支持场景
  98. list($validate, $scene) = explode('.', $validate);
  99. }
  100. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  101. $v = new $class();
  102. if (!empty($scene)) {
  103. $v->scene($scene);
  104. }
  105. }
  106. $v->message($message);
  107. // 是否批量验证
  108. if ($batch || $this->batchValidate) {
  109. $v->batch(true);
  110. }
  111. return $v->failException(true)->check($data);
  112. }
  113. /**
  114. * 错误提示
  115. */
  116. protected function jump($msg = '', $url = null, $wait = 3)
  117. {
  118. if (is_null($url)) {
  119. $url = 'javascript:history.back(-1);';
  120. } else {
  121. $url = "location.href = '" . url($url) . "'";
  122. }
  123. $result = [
  124. 'msg' => $msg,
  125. 'url' => $url,
  126. 'wait' => $wait,
  127. ];
  128. return view('/public/jump', $result);
  129. }
  130. }