Jump.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * 用法:
  4. * class index
  5. * {
  6. * use \traits\controller\Jump;
  7. * public function index(){
  8. * $this->error();
  9. * $this->redirect();
  10. * }
  11. * }
  12. */
  13. namespace traits\controller;
  14. use think\Container;
  15. use think\exception\HttpResponseException;
  16. use think\Response;
  17. use think\response\Redirect;
  18. trait Jump
  19. {
  20. /**
  21. * 应用实例
  22. * @var \think\App
  23. */
  24. protected $app;
  25. /**
  26. * 操作成功跳转的快捷方法
  27. * @access protected
  28. * @param mixed $msg 提示信息
  29. * @param string $url 跳转的URL地址
  30. * @param mixed $data 返回的数据
  31. * @param integer $wait 跳转等待时间
  32. * @param array $header 发送的Header信息
  33. * @return void
  34. */
  35. protected function success($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
  36. {
  37. if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
  38. //$url = $_SERVER["HTTP_REFERER"];
  39. $url = '';
  40. } elseif ('' !== $url) {
  41. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : Container::get('url')->build($url);
  42. }
  43. $result = [
  44. 'code' => 1,
  45. 'msg' => $msg,
  46. 'data' => $data,
  47. 'url' => $url,
  48. 'wait' => $wait,
  49. ];
  50. $type = $this->getResponseType();
  51. // 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
  52. if ('html' == strtolower($type)) {
  53. $type = 'jump';
  54. }
  55. $response = Response::create($result, $type)->header($header)->options(['jump_template' => $this->app['config']->get('dispatch_success_tmpl')]);
  56. throw new HttpResponseException($response);
  57. }
  58. /**
  59. * 操作错误跳转的快捷方法
  60. * @access protected
  61. * @param mixed $msg 提示信息
  62. * @param string $url 跳转的URL地址
  63. * @param mixed $data 返回的数据
  64. * @param integer $wait 跳转等待时间
  65. * @param array $header 发送的Header信息
  66. * @return void
  67. */
  68. protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
  69. {
  70. $type = $this->getResponseType();
  71. if (is_null($url)) {
  72. $url = $this->app['request']->isAjax() ? '' : 'javascript:history.back(-1);';
  73. } elseif ('' !== $url) {
  74. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app['url']->build($url);
  75. }
  76. $result = [
  77. 'code' => 0,
  78. 'msg' => $msg,
  79. 'data' => $data,
  80. 'url' => $url,
  81. 'wait' => $wait,
  82. ];
  83. if ('html' == strtolower($type)) {
  84. $type = 'jump';
  85. }
  86. $response = Response::create($result, $type)->header($header)->options(['jump_template' => $this->app['config']->get('dispatch_error_tmpl')]);
  87. throw new HttpResponseException($response);
  88. }
  89. /**
  90. * 返回封装后的API数据到客户端
  91. * @access protected
  92. * @param mixed $data 要返回的数据
  93. * @param integer $code 返回的code
  94. * @param mixed $msg 提示信息
  95. * @param string $type 返回数据格式
  96. * @param array $header 发送的Header信息
  97. * @return void
  98. */
  99. protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
  100. {
  101. $result = [
  102. 'code' => $code,
  103. 'msg' => $msg,
  104. 'time' => time(),
  105. 'data' => $data,
  106. ];
  107. $type = $type ?: $this->getResponseType();
  108. $response = Response::create($result, $type)->header($header);
  109. throw new HttpResponseException($response);
  110. }
  111. /**
  112. * URL重定向
  113. * @access protected
  114. * @param string $url 跳转的URL表达式
  115. * @param array|integer $params 其它URL参数
  116. * @param integer $code http code
  117. * @param array $with 隐式传参
  118. * @return void
  119. */
  120. protected function redirect($url, $params = [], $code = 302, $with = [])
  121. {
  122. $response = new Redirect($url);
  123. if (is_integer($params)) {
  124. $code = $params;
  125. $params = [];
  126. }
  127. $response->code($code)->params($params)->with($with);
  128. throw new HttpResponseException($response);
  129. }
  130. /**
  131. * 获取当前的response 输出类型
  132. * @access protected
  133. * @return string
  134. */
  135. protected function getResponseType()
  136. {
  137. if (!$this->app) {
  138. $this->app = Container::get('app');
  139. }
  140. $isAjax = $this->app['request']->isAjax();
  141. $config = $this->app['config'];
  142. return $isAjax
  143. ? $config->get('default_ajax_return')
  144. : $config->get('default_return_type');
  145. }
  146. }