123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- declare (strict_types = 1);
- namespace app;
- use think\App;
- use think\facade\View;
- use think\exception\HttpException;
- use think\exception\HttpResponseException;
- use think\exception\ValidateException;
- use think\Response;
- use think\Validate;
- use liliuwei\think\Jump;
- use think\facade\Route;
- use think\facade\Config;
- use think\facade\Env;
- abstract class Controller
- {
-
- use Jump;
-
- protected $request;
-
-
- protected $app;
-
-
- protected $batchValidate = false;
-
- protected $middleware = [];
-
- public function __construct()
- {
-
- $this->initialize();
- }
-
- protected function initialize()
- {}
-
- protected function fetch($template = '', $vars = [], $config = [])
- {
- if(!empty($config))
- {
- $config_view = Config::get('view');
- $config_view['tpl_replace_string'] = array_merge($config_view['tpl_replace_string'], $config);
- Config::set($config_view,'view');
- }
- return View::fetch($template, $vars);
- }
-
-
- protected function display($content = '', $vars = [], $config = [])
- {
- return View::display($content, $vars, $config);
- }
-
-
- protected function assign($name, $value = '')
- {
- View::assign($name, $value);
-
- return $this;
- }
-
- 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 = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
- }
-
- $result = [
- 'code' => 1,
- 'msg' => $msg,
- 'data' => $data,
- 'url' => $url,
- 'wait' => $wait,
- ];
-
- $type = 'html';
-
- if ('html' == strtolower($type)) {
- $type = 'view';
- $response = Response::create(config('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 = 'javascript:history.back(-1);';
- } elseif ($url) {
- $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
- }
-
- $result = [
- 'code' => 0,
- 'msg' => $msg,
- 'data' => $data,
- 'url' => $url,
- 'wait' => $wait,
- ];
-
- $type = 'html';
-
- if ('html' == strtolower($type)) {
- $type = 'view';
- $response = Response::create(config('jump.dispatch_error_tmpl'), $type)->assign($result)->header($header);
- } else {
- $response = Response::create($result, $type)->header($header);
- }
-
- throw new HttpResponseException($response);
- }
-
-
- protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
- {
- $result = [
- 'code' => $code,
- 'msg' => $msg,
- 'time' => time(),
- 'data' => $data,
- ];
-
- $type = 'html';
- $response = Response::create($result, $type)->header($header);
-
- throw new HttpResponseException($response);
- }
-
-
- protected function redirect($url, $code = 302, $with = [])
- {
- $response = Response::create($url, 'redirect');
-
- $response->code($code)->with($with);
-
- throw new HttpResponseException($response);
- }
- }
|