Jump.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /**
  25. * 操作成功跳转的快捷方法
  26. * @access protected
  27. * @param mixed $msg 提示信息
  28. * @param string $url 跳转的URL地址
  29. * @param mixed $data 返回的数据
  30. * @param integer $wait 跳转等待时间
  31. * @param array $header 发送的Header信息
  32. * @return void
  33. */
  34. protected function success($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
  35. {
  36. $code = 1;
  37. if (is_numeric($msg)) {
  38. $code = $msg;
  39. $msg = '';
  40. }
  41. if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
  42. $url = $_SERVER["HTTP_REFERER"];
  43. } elseif ('' !== $url) {
  44. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : Url::build($url);
  45. }
  46. $result = [
  47. 'code' => $code,
  48. 'msg' => $msg,
  49. 'data' => $data,
  50. 'url' => $url,
  51. 'wait' => $wait,
  52. ];
  53. $type = $this->getResponseType();
  54. if ('html' == strtolower($type)) {
  55. $result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
  56. ->fetch(Config::get('dispatch_success_tmpl'), $result);
  57. }
  58. $response = Response::create($result, $type)->header($header);
  59. throw new HttpResponseException($response);
  60. }
  61. /**
  62. * 操作错误跳转的快捷方法
  63. * @access protected
  64. * @param mixed $msg 提示信息
  65. * @param string $url 跳转的URL地址
  66. * @param mixed $data 返回的数据
  67. * @param integer $wait 跳转等待时间
  68. * @param array $header 发送的Header信息
  69. * @return void
  70. */
  71. protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
  72. {
  73. // die("<script>alert('{$msg}');window.history.go(-2);</script>");
  74. $code = 0;
  75. if (is_numeric($msg)) {
  76. $code = $msg;
  77. $msg = '';
  78. }
  79. if (is_null($url)) {
  80. $url = Request::instance()->isAjax() ? '' : 'javascript:history.back(-1);';
  81. } elseif ('' !== $url) {
  82. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : Url::build($url);
  83. }
  84. $result = [
  85. 'code' => $code,
  86. 'msg' => $msg,
  87. 'data' => $data,
  88. 'url' => $url,
  89. 'wait' => $wait,
  90. ];
  91. $type = $this->getResponseType();
  92. if ('html' == strtolower($type)) {
  93. $result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
  94. ->fetch(Config::get('dispatch_error_tmpl'), $result);
  95. }
  96. $response = Response::create($result, $type)->header($header);
  97. throw new HttpResponseException($response);
  98. }
  99. /**
  100. * 返回封装后的API数据到客户端
  101. * @access protected
  102. * @param mixed $data 要返回的数据
  103. * @param integer $code 返回的code
  104. * @param mixed $msg 提示信息
  105. * @param string $type 返回数据格式
  106. * @param array $header 发送的Header信息
  107. * @return void
  108. */
  109. protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
  110. {
  111. $result = [
  112. 'code' => $code,
  113. 'msg' => $msg,
  114. 'time' => $_SERVER['REQUEST_TIME'],
  115. 'data' => $data,
  116. ];
  117. $type = $type ?: $this->getResponseType();
  118. $response = Response::create($result, $type)->header($header);
  119. throw new HttpResponseException($response);
  120. }
  121. /**
  122. * URL重定向
  123. * @access protected
  124. * @param string $url 跳转的URL表达式
  125. * @param array|integer $params 其它URL参数
  126. * @param integer $code http code
  127. * @param array $with 隐式传参
  128. * @return void
  129. */
  130. protected function redirect($url, $params = [], $code = 302, $with = [])
  131. {
  132. $response = new Redirect($url);
  133. if (is_integer($params)) {
  134. $code = $params;
  135. $params = [];
  136. }
  137. $response->code($code)->params($params)->with($with);
  138. throw new HttpResponseException($response);
  139. }
  140. /**
  141. * 获取当前的response 输出类型
  142. * @access protected
  143. * @return string
  144. */
  145. protected function getResponseType()
  146. {
  147. $isAjax = Request::instance()->isAjax();
  148. return $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type');
  149. }
  150. }