123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- declare(strict_types=1);
- namespace app;
- use think\App;
- use think\exception\ValidateException;
- use think\Validate;
- use think\facade\View;
- use think\exception\HttpResponseException;
- use think\Response;
- abstract class BaseController
- {
-
- protected $request;
-
- protected $app;
-
- protected $batchValidate = false;
-
- protected $middleware = [];
-
- public function __construct(App $app)
- {
- $this->app = $app;
- $this->request = $this->app->request;
-
- $this->initialize();
- }
-
- protected function initialize()
- {
- }
-
- protected function validate(array $data, $validate, array $message = [], bool $batch = false)
- {
- if (is_array($validate)) {
- $v = new Validate();
- $v->rule($validate);
- } else {
- if (strpos($validate, '.')) {
-
- [$validate, $scene] = explode('.', $validate);
- }
- $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
- $v = new $class();
- if (!empty($scene)) {
- $v->scene($scene);
- }
- }
- $v->message($message);
-
- if ($batch || $this->batchValidate) {
- $v->batch(true);
- }
- return $v->failException(true)->check($data);
- }
-
- protected function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
- {
- if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
- $url = $_SERVER["HTTP_REFERER"];
- } elseif ($url) {
- $url = (string)$url;
- $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : (string)$this->app->route->buildUrl($url);
- }
- $result = [
- 'code' => 1,
- 'msg' => $msg,
- 'data' => $data,
- 'url' => $url,
- 'wait' => $wait,
- ];
- $type = $this->getResponseType();
-
- if ('html' == strtolower($type)) {
- $type = 'view';
- $response = Response::create($this->app->config->get('jump.dispatch_success_tmpl'), $type)->assign($result)->header($header);
- } else {
- $response = Response::create($result, $type)->header($header);
- }
- throw new HttpResponseException($response);
- }
-
- protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
- {
- if (is_null($url)) {
- $url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);';
- } elseif ($url) {
- $url = (string)$url;
- $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : (string)$this->app->route->buildUrl($url);
- }
- $result = [
- 'code' => 0,
- 'msg' => $msg,
- 'data' => $data,
- 'url' => $url,
- 'wait' => $wait,
- ];
- $type = $this->getResponseType();
- if ('html' == strtolower($type)) {
- $type = 'view';
- $response = Response::create($this->app->config->get('jump.dispatch_error_tmpl'), $type)->assign($result)->header($header);
- } else {
- $response = Response::create($result, $type)->header($header);
- }
- throw new HttpResponseException($response);
- }
-
- protected function redirect($url, $params = [], $code = 302, $with = [])
- {
- $url = (string)$url;
- $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : (string)$this->app->route->buildUrl($url, $params);
- $response = Response::create($url, 'redirect');
- $response->code($code)->with($with);
- throw new HttpResponseException($response);
- }
-
- protected function getResponseType()
- {
- return request()->isAjax()
- ? 'json'
- : 'html';
- }
- }
|