123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace traits\controller;
- use think\Config;
- use think\exception\HttpResponseException;
- use think\Request;
- use think\Response;
- use think\response\Redirect;
- use think\Url;
- use think\View as ViewTemplate;
- trait Jump
- {
-
- protected function success($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
- {
- if (is_null($url) && !is_null(Request::instance()->server('HTTP_REFERER'))) {
- $url = Request::instance()->server('HTTP_REFERER');
- } elseif ('' !== $url && !strpos($url ?? '', '://') && 0 !== strpos($url ?? '', '/')) {
- $url = Url::build($url ?? '');
- }
- $type = $this->getResponseType();
- $result = [
- 'code' => 1,
- 'msg' => $msg,
- 'data' => $data,
- 'url' => $url,
- 'wait' => $wait,
- ];
- if ('html' == strtolower($type)) {
- $template = Config::get('template');
- $view = Config::get('view_replace_str');
- $result = ViewTemplate::instance($template, $view)
- ->fetch(Config::get('dispatch_success_tmpl'), $result);
- }
- $response = Response::create($result, $type)->header($header);
- throw new HttpResponseException($response);
- }
-
- protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
- {
- if (is_null($url)) {
- $url = Request::instance()->isAjax() ? '' : 'javascript:history.back(-1);';
- } elseif ('' !== $url && !strpos($url, '://') && 0 !== strpos($url, '/')) {
- $url = Url::build($url);
- }
- $type = $this->getResponseType();
- $result = [
- 'code' => 0,
- 'msg' => $msg,
- 'data' => $data,
- 'url' => $url,
- 'wait' => $wait,
- ];
- if ('html' == strtolower($type)) {
- $template = Config::get('template');
- $view = Config::get('view_replace_str');
- $result = ViewTemplate::instance($template, $view)
- ->fetch(Config::get('dispatch_error_tmpl'), $result);
- }
- $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' => Request::instance()->server('REQUEST_TIME'),
- 'data' => $data,
- ];
- $type = $type ?: $this->getResponseType();
- $response = Response::create($result, $type)->header($header);
- throw new HttpResponseException($response);
- }
-
- protected function redirect($url, $params = [], $code = 302, $with = [])
- {
- if (is_integer($params)) {
- $code = $params;
- $params = [];
- }
- $response = new Redirect($url);
- $response->code($code)->params($params)->with($with);
- throw new HttpResponseException($response);
- }
-
- protected function getResponseType()
- {
- return Request::instance()->isAjax()
- ? Config::get('default_ajax_return')
- : Config::get('default_return_type');
- }
- }
|