| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // +----------------------------------------------------------------------
- // | 跳转traits
- // +----------------------------------------------------------------------
- namespace app\common\library\traits;
- use think\App;
- use think\exception\HttpResponseException;
- use think\Response;
- trait Jump
- {
- /**
- * Request实例
- * @var \think\Request
- */
- protected $request;
- /**
- * 应用实例
- * @var \think\App
- */
- protected $app;
- /**
- * 构造方法
- * @access public
- * @param App $app 应用对象
- */
- public function __construct(App $app)
- {
- $this->app = $app;
- $this->request = $this->app->request;
- }
- /**
- * 操作成功跳转的快捷方法
- * @access protected
- * @param mixed $msg 提示信息
- * @param string|null $url 跳转的URL地址
- * @param mixed $data 返回的数据
- * @param integer $wait 跳转等待时间
- * @param array $header 发送的Header信息
- * @return void
- */
- 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, '://') || str_starts_with($url, '/')) ? $url : (string) $this->app->route->buildUrl($url);
- }
- $result = [
- 'code' => 1,
- 'msg' => $msg,
- 'data' => $data,
- 'url' => $url,
- 'wait' => $wait,
- ];
- $type = $this->getResponseType();
- // 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
- if ('html' == strtolower($type)) {
- $type = 'view';
- $response = Response::create($this->app->config->get('app.dispatch_success_tmpl'), $type)->assign($result)->header($header);
- } else {
- $response = Response::create($result, $type)->header($header);
- }
- throw new HttpResponseException($response);
- }
- /**
- * 操作错误跳转的快捷方法
- * @access protected
- * @param mixed $msg 提示信息
- * @param string|null $url 跳转的URL地址
- * @param mixed $data 返回的数据
- * @param integer $wait 跳转等待时间
- * @param array $header 发送的Header信息
- * @return void
- */
- 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 = (strpos($url, '://') || str_starts_with($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('app.dispatch_error_tmpl'), $type)->assign($result)->header($header);
- } else {
- $response = Response::create($result, $type)->header($header);
- }
- throw new HttpResponseException($response);
- }
- /**
- * 返回封装后的API数据到客户端
- * @access protected
- * @param mixed $data 要返回的数据
- * @param integer $code 返回的code
- * @param mixed $msg 提示信息
- * @param string $type 返回数据格式
- * @param array $header 发送的Header信息
- * @return void
- */
- protected function result($data, $code = 0, $msg = '', string $type = '', array $header = [])
- {
- $result = [
- 'code' => $code,
- 'msg' => $msg,
- 'time' => time(),
- 'data' => $data,
- ];
- $type = $type ?: $this->getResponseType();
- $response = Response::create($result, $type)->header($header);
- throw new HttpResponseException($response);
- }
- /**
- * URL重定向
- * @access protected
- * @param string $url 跳转的URL表达式
- * @param integer $code http code
- * @param array $with 隐式传参
- * @return void
- */
- protected function redirect($url, int $code = 302, $with = [])
- {
- $response = Response::create($url, 'redirect');
- $response->code($code)->with($with);
- throw new HttpResponseException($response);
- }
- /**
- * 获取当前的response 输出类型
- * @access protected
- * @return string
- */
- protected function getResponseType(): string
- {
- return $this->request->isJson() || $this->request->isAjax() ? 'json' : 'html';
- }
- }
|