Jump.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. } elseif ('' !== $url) {
  40. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : Container::get('url')->build($url);
  41. }
  42. $result = [
  43. 'code' => 1,
  44. 'msg' => $msg,
  45. 'data' => $data,
  46. 'url' => $url,
  47. 'wait' => $wait,
  48. ];
  49. $type = $this->getResponseType();
  50. // 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
  51. if ('html' == strtolower($type)) {
  52. $type = 'jump';
  53. }
  54. $response = Response::create($result, $type)->header($header)->options(['jump_template' => $this->app['config']->get('dispatch_success_tmpl')]);
  55. throw new HttpResponseException($response);
  56. }
  57. /**
  58. * 操作错误跳转的快捷方法
  59. * @access protected
  60. * @param mixed $msg 提示信息
  61. * @param string $url 跳转的URL地址
  62. * @param mixed $data 返回的数据
  63. * @param integer $wait 跳转等待时间
  64. * @param array $header 发送的Header信息
  65. * @return void
  66. */
  67. protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
  68. {
  69. $type = $this->getResponseType();
  70. if (is_null($url)) {
  71. $url = $this->app['request']->isAjax() ? '' : 'javascript:history.back(-1);';
  72. } elseif ('' !== $url) {
  73. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app['url']->build($url);
  74. }
  75. $result = [
  76. 'code' => 0,
  77. 'msg' => $msg,
  78. 'data' => $data,
  79. 'url' => $url,
  80. 'wait' => $wait,
  81. ];
  82. if ('html' == strtolower($type)) {
  83. $type = 'jump';
  84. }
  85. $response = Response::create($result, $type)->header($header)->options(['jump_template' => $this->app['config']->get('dispatch_error_tmpl')]);
  86. throw new HttpResponseException($response);
  87. }
  88. /**
  89. * 返回封装后的API数据到客户端
  90. * @access protected
  91. * @param mixed $data 要返回的数据
  92. * @param integer $code 返回的code
  93. * @param mixed $msg 提示信息
  94. * @param string $type 返回数据格式
  95. * @param array $header 发送的Header信息
  96. * @return void
  97. */
  98. protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
  99. {
  100. $result = [
  101. 'code' => $code,
  102. 'msg' => $msg,
  103. 'time' => time(),
  104. 'data' => $data,
  105. ];
  106. $type = $type ?: $this->getResponseType();
  107. $response = Response::create($result, $type)->header($header);
  108. throw new HttpResponseException($response);
  109. }
  110. /**
  111. * URL重定向
  112. * @access protected
  113. * @param string $url 跳转的URL表达式
  114. * @param array|integer $params 其它URL参数
  115. * @param integer $code http code
  116. * @param array $with 隐式传参
  117. * @return void
  118. */
  119. protected function redirect($url, $params = [], $code = 302, $with = [])
  120. {
  121. $response = new Redirect($url);
  122. if (is_integer($params)) {
  123. $code = $params;
  124. $params = [];
  125. }
  126. $response->code($code)->params($params)->with($with);
  127. throw new HttpResponseException($response);
  128. }
  129. /**
  130. * 获取当前的response 输出类型
  131. * @access protected
  132. * @return string
  133. */
  134. protected function getResponseType()
  135. {
  136. if (!$this->app) {
  137. $this->app = Container::get('app');
  138. }
  139. $isAjax = $this->app['request']->isAjax();
  140. $config = $this->app['config'];
  141. return $isAjax
  142. ? $config->get('default_ajax_return')
  143. : $config->get('default_return_type');
  144. }
  145. }