BaseController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. use think\facade\Lang;
  11. /**
  12. * 控制器基础类
  13. */
  14. abstract class BaseController
  15. {
  16. /**
  17. * Request实例
  18. * @var \think\Request
  19. */
  20. protected $request;
  21. /**
  22. * 应用实例
  23. * @var \think\App
  24. */
  25. protected $app;
  26. /**
  27. * 是否批量验证
  28. * @var bool
  29. */
  30. protected $batchValidate = false;
  31. /**
  32. * 控制器中间件
  33. * @var array
  34. */
  35. protected $middleware = [];
  36. /**
  37. * 是否批量验证
  38. * @var bool
  39. */
  40. protected $userInfo = [];
  41. protected $admin_id = 0;
  42. /**
  43. * 接收的post数据
  44. * @var bool
  45. */
  46. protected $postData = [];
  47. protected $uid = 0;
  48. protected $globalConfig = [];
  49. protected $chatSetting = [];
  50. protected $lang = 'en';
  51. /**
  52. * 构造方法
  53. * @access public
  54. * @param App $app 应用对象
  55. */
  56. public function __construct(App $app)
  57. {
  58. $this->app = $app;
  59. $this->request = $this->app->request;
  60. // 控制器初始化
  61. $this->initialize();
  62. $lang = request()->header('Lang', 'en');
  63. $this->lang = $lang;
  64. Lang::setLangSet($lang);
  65. }
  66. // 初始化
  67. protected function initialize()
  68. {
  69. $this->userInfo=$this->request->userInfo;
  70. $this->admin_id = $this->request->adminInfo ? $this->request->adminInfo->id : 0;
  71. $this->uid=$this->userInfo['user_id'] ?? 0;
  72. $config=Config::getSystemInfo();
  73. if($config){
  74. $this->globalConfig = $config;
  75. $this->chatSetting = $config['chatInfo'] ?? [];
  76. }
  77. // 验证版本,如果不一致,就需要退出重新登陆
  78. $version =config('app.app_version');
  79. $oldVersion=Cache::get('app_version');
  80. if($version!=$oldVersion){
  81. Cache::set('app_version',$version);
  82. JWTAuth::refresh();
  83. Cache::delete('systemInfo_config');
  84. }
  85. }
  86. /**
  87. * 验证数据
  88. * @access protected
  89. * @param array $data 数据
  90. * @param string|array $validate 验证器名或者验证规则数组
  91. * @param array $message 提示信息
  92. * @param bool $batch 是否批量验证
  93. * @return array|string|true
  94. * @throws ValidateException
  95. */
  96. protected function validate(array $data, $validate, array $message = [], bool $batch = false)
  97. {
  98. if (is_array($validate)) {
  99. $v = new Validate();
  100. $v->rule($validate);
  101. } else {
  102. if (strpos($validate, '.')) {
  103. // 支持场景
  104. [$validate, $scene] = explode('.', $validate);
  105. }
  106. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  107. $v = new $class();
  108. if (!empty($scene)) {
  109. $v->scene($scene);
  110. }
  111. }
  112. $v->message($message);
  113. // 是否批量验证
  114. if ($batch || $this->batchValidate) {
  115. $v->batch(true);
  116. }
  117. return $v->failException(true)->check($data);
  118. }
  119. /**
  120. * 自动获取前端传递的分页数量
  121. * @param \think\Model|\think\model\relation\HasMany $model
  122. * @return \think\Paginator
  123. */
  124. protected function paginate($model)
  125. {
  126. $limit = $this->request->param('limit', 20);
  127. return $model->paginate($limit);
  128. }
  129. protected function success($data = [], $msg = 'ok')
  130. {
  131. return json([
  132. 'code' => 0,
  133. 'timestamp' => time(),
  134. 'msg' => Lang::get($msg),
  135. 'data' => $data
  136. ]);
  137. }
  138. /**
  139. * @apiError (错误) {String} msg 错误信息
  140. * @apiError (错误) {Array} [data] 数据 若code!=0 则为错误数据,code=101009 该值为验证失败的详情
  141. * @apiError (错误) {Int} code 错误代码
  142. * @apiVersion 1.0.0
  143. */
  144. protected function error(string $msg = "请求错误", $data = [], int $code = -1)
  145. {
  146. $code = intval($code);
  147. if ($code === 0) $code = -1;
  148. $a = Lang::get("{$msg}", $data);
  149. if ($a == $msg) {
  150. $a = $msg;
  151. }
  152. return json([
  153. 'code' => $code,
  154. 'timestamp' => time(),
  155. 'msg' => $a,
  156. 'data' => $data
  157. ]);
  158. }
  159. }