Jump.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. $code = 0;
  74. if (is_numeric($msg)) {
  75. $code = $msg;
  76. $msg = '';
  77. }
  78. if (is_null($url)) {
  79. $url = Request::instance()->isAjax() ? '' : 'javascript:history.back(-1);';
  80. } elseif ('' !== $url) {
  81. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : Url::build($url);
  82. }
  83. $result = [
  84. 'code' => $code,
  85. 'msg' => $msg,
  86. 'data' => $data,
  87. 'url' => $url,
  88. 'wait' => $wait,
  89. ];
  90. $type = $this->getResponseType();
  91. if ('html' == strtolower($type)) {
  92. $result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
  93. ->fetch(Config::get('dispatch_error_tmpl'), $result);
  94. }
  95. $response = Response::create($result, $type)->header($header);
  96. throw new HttpResponseException($response);
  97. }
  98. /**
  99. * 返回封装后的API数据到客户端
  100. * @access protected
  101. * @param mixed $data 要返回的数据
  102. * @param integer $code 返回的code
  103. * @param mixed $msg 提示信息
  104. * @param string $type 返回数据格式
  105. * @param array $header 发送的Header信息
  106. * @return void
  107. */
  108. protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
  109. {
  110. $result = [
  111. 'code' => $code,
  112. 'msg' => $msg,
  113. 'time' => $_SERVER['REQUEST_TIME'],
  114. 'data' => $data,
  115. ];
  116. $type = $type ?: $this->getResponseType();
  117. $response = Response::create($result, $type)->header($header);
  118. throw new HttpResponseException($response);
  119. }
  120. /**
  121. * URL重定向
  122. * @access protected
  123. * @param string $url 跳转的URL表达式
  124. * @param array|integer $params 其它URL参数
  125. * @param integer $code http code
  126. * @return void
  127. */
  128. protected function redirect($url, $params = [], $code = 302)
  129. {
  130. $response = new Redirect($url);
  131. if (is_integer($params)) {
  132. $code = $params;
  133. $params = [];
  134. }
  135. $response->code($code)->params($params);
  136. throw new HttpResponseException($response);
  137. }
  138. /**
  139. * 获取当前的response 输出类型
  140. * @access protected
  141. * @return string
  142. */
  143. protected function getResponseType()
  144. {
  145. $isAjax = Request::instance()->isAjax();
  146. return $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type');
  147. }
  148. }