BaseController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app;
  4. use think\App;
  5. use think\exception\ValidateException;
  6. use think\Validate;
  7. use app\manage\model\{Config};
  8. use think\facade\Cache;
  9. use thans\jwt\facade\JWTAuth;
  10. /**
  11. * 控制器基础类
  12. */
  13. abstract class BaseController
  14. {
  15. /**
  16. * Request实例
  17. * @var \think\Request
  18. */
  19. protected $request;
  20. /**
  21. * 应用实例
  22. * @var \think\App
  23. */
  24. protected $app;
  25. /**
  26. * 是否批量验证
  27. * @var bool
  28. */
  29. protected $batchValidate = false;
  30. /**
  31. * 控制器中间件
  32. * @var array
  33. */
  34. protected $middleware = [];
  35. /**
  36. * 是否批量验证
  37. * @var bool
  38. */
  39. protected $userInfo = [];
  40. /**
  41. * 接收的post数据
  42. * @var bool
  43. */
  44. protected $postData = [];
  45. protected $uid = 0;
  46. protected $globalConfig = [];
  47. protected $chatSetting = [];
  48. /**
  49. * 构造方法
  50. * @access public
  51. * @param App $app 应用对象
  52. */
  53. public function __construct(App $app)
  54. {
  55. $this->app = $app;
  56. $this->request = $this->app->request;
  57. // 控制器初始化
  58. $this->initialize();
  59. }
  60. // 初始化
  61. protected function initialize()
  62. {
  63. $this->userInfo=$this->request->userInfo;
  64. $this->uid=$this->userInfo['user_id'] ?? 0;
  65. $config=Config::getSystemInfo();
  66. if($config){
  67. $this->globalConfig = $config;
  68. $this->chatSetting = $config['chatInfo'] ?? [];
  69. }
  70. // 验证版本,如果不一致,就需要退出重新登陆
  71. $version =config('app.app_version');
  72. $oldVersion=Cache::get('app_version');
  73. if($version!=$oldVersion){
  74. Cache::set('app_version',$version);
  75. JWTAuth::refresh();
  76. Cache::delete('systemInfo');
  77. }
  78. }
  79. /**
  80. * 验证数据
  81. * @access protected
  82. * @param array $data 数据
  83. * @param string|array $validate 验证器名或者验证规则数组
  84. * @param array $message 提示信息
  85. * @param bool $batch 是否批量验证
  86. * @return array|string|true
  87. * @throws ValidateException
  88. */
  89. protected function validate(array $data, $validate, array $message = [], bool $batch = false)
  90. {
  91. if (is_array($validate)) {
  92. $v = new Validate();
  93. $v->rule($validate);
  94. } else {
  95. if (strpos($validate, '.')) {
  96. // 支持场景
  97. [$validate, $scene] = explode('.', $validate);
  98. }
  99. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  100. $v = new $class();
  101. if (!empty($scene)) {
  102. $v->scene($scene);
  103. }
  104. }
  105. $v->message($message);
  106. // 是否批量验证
  107. if ($batch || $this->batchValidate) {
  108. $v->batch(true);
  109. }
  110. return $v->failException(true)->check($data);
  111. }
  112. /**
  113. * 自动获取前端传递的分页数量
  114. * @param \think\Model|\think\model\relation\HasMany $model
  115. * @return \think\Paginator
  116. */
  117. protected function paginate($model)
  118. {
  119. $limit = $this->request->param('limit', 20);
  120. return $model->paginate($limit);
  121. }
  122. }