Jump.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * 用法:
  4. * load_trait('controller/Jump');
  5. * class index
  6. * {
  7. * use \traits\controller\Jump;
  8. * public function index(){
  9. * $this->error();
  10. * $this->redirect();
  11. * }
  12. * }
  13. */
  14. namespace traits\controller;
  15. use think\Config;
  16. use think\exception\HttpResponseException;
  17. use think\Request;
  18. use think\Response;
  19. use think\response\Redirect;
  20. use think\Url;
  21. use think\View as ViewTemplate;
  22. trait Jump
  23. {
  24. /* Modified by wuyonghong BEGIN 2018/12/19 ISSUES 返回修改 */
  25. /**
  26. * 操作成功跳转的快捷方法
  27. *
  28. * @access protected
  29. *
  30. * @param mixed $msg 提示信息
  31. * @param mixed $data 返回的数据
  32. * @param int $code
  33. * @param array $header 发送的Header信息
  34. *
  35. * @param string $url 跳转的URL地址
  36. * @param integer $wait 跳转等待时间
  37. *
  38. * @return void
  39. * @throws HttpResponseException
  40. */
  41. protected function success($msg = '', $data = '', $code = 200, array $header = [], $url = null, $wait = 3)
  42. {
  43. if (is_null($url) && !is_null(Request::instance()->server('HTTP_REFERER'))) {
  44. // $url = Request::instance()->server('HTTP_REFERER');
  45. $url = $_SERVER['HTTP_REFERER'];
  46. } elseif ('' !== $url && !strpos($url, '://') && 0 !== strpos($url, '/')) {
  47. $url = Url::build($url);
  48. }
  49. $type = $this->getResponseType();
  50. $result = [
  51. 'code' => $code,
  52. 'msg' => $msg,
  53. 'data' => $data,
  54. 'url' => $url,
  55. 'wait' => $wait,
  56. ];
  57. if ('html' == strtolower($type)) {
  58. $template = Config::get('template');
  59. $view = Config::get('view_replace_str');
  60. $result = ViewTemplate::instance($template, $view)
  61. ->fetch(Config::get('dispatch_success_tmpl'), $result);
  62. }
  63. $response = Response::create($result, $type)->header($header);
  64. throw new HttpResponseException($response);
  65. }
  66. /**
  67. * 操作错误跳转的快捷方法
  68. *
  69. * @access protected
  70. *
  71. * @param mixed $msg 提示信息
  72. * @param mixed $data 返回的数据
  73. * @param int $code
  74. * @param array $header 发送的Header信息
  75. *
  76. * @param string $url 跳转的URL地址
  77. * @param integer $wait 跳转等待时间
  78. *
  79. * @return void
  80. * @throws HttpResponseException
  81. */
  82. protected function error($msg = '', $data = '', $code = 400, array $header = [], $url = null, $wait = 3)
  83. {
  84. if (is_null($url)) {
  85. $url = Request::instance()->isAjax() ? '' : 'javascript:history.back(-1);';
  86. } elseif ('' !== $url && !strpos($url, '://') && 0 !== strpos($url, '/')) {
  87. $url = Url::build($url);
  88. }
  89. $type = $this->getResponseType();
  90. $result = [
  91. 'code' => $code,
  92. 'msg' => $msg,
  93. 'data' => $data,
  94. 'url' => $url,
  95. 'wait' => $wait,
  96. ];
  97. if ('html' == strtolower($type)) {
  98. $template = Config::get('template');
  99. $view = Config::get('view_replace_str');
  100. $result = ViewTemplate::instance($template, $view)
  101. ->fetch(Config::get('dispatch_error_tmpl'), $result);
  102. }
  103. $response = Response::create($result, $type)->header($header);
  104. throw new HttpResponseException($response);
  105. }
  106. /* END 2018/12/19 ISSUES: */
  107. /**
  108. * 返回封装后的 API 数据到客户端
  109. * @access protected
  110. * @param mixed $data 要返回的数据
  111. * @param int $code 返回的 code
  112. * @param mixed $msg 提示信息
  113. * @param string $type 返回数据格式
  114. * @param array $header 发送的 Header 信息
  115. * @return void
  116. * @throws HttpResponseException
  117. */
  118. protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
  119. {
  120. $result = [
  121. 'code' => $code,
  122. 'msg' => $msg,
  123. 'time' => Request::instance()->server('REQUEST_TIME'),
  124. 'data' => $data,
  125. ];
  126. $type = $type ?: $this->getResponseType();
  127. $response = Response::create($result, $type)->header($header);
  128. throw new HttpResponseException($response);
  129. }
  130. /**
  131. * URL 重定向
  132. * @access protected
  133. * @param string $url 跳转的 URL 表达式
  134. * @param array|int $params 其它 URL 参数
  135. * @param int $code http code
  136. * @param array $with 隐式传参
  137. * @return void
  138. * @throws HttpResponseException
  139. */
  140. protected function redirect($url, $params = [], $code = 302, $with = [])
  141. {
  142. if (is_integer($params)) {
  143. $code = $params;
  144. $params = [];
  145. }
  146. $response = new Redirect($url);
  147. $response->code($code)->params($params)->with($with);
  148. throw new HttpResponseException($response);
  149. }
  150. /**
  151. * 获取当前的 response 输出类型
  152. * @access protected
  153. * @return string
  154. */
  155. protected function getResponseType()
  156. {
  157. return Request::instance()->isAjax()
  158. ? Config::get('default_ajax_return')
  159. : Config::get('default_return_type');
  160. }
  161. }