Jump.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. if (is_null($url) && !is_null(Request::instance()->server('HTTP_REFERER'))) {
  37. $url = Request::instance()->server('HTTP_REFERER');
  38. } elseif ('' !== $url) {
  39. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : Url::build($url);
  40. }
  41. $result = [
  42. 'code' => 1,
  43. 'msg' => $msg,
  44. 'data' => $data,
  45. 'url' => $url,
  46. 'wait' => $wait,
  47. ];
  48. $type = $this->getResponseType();
  49. if ('html' == strtolower($type)) {
  50. $result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
  51. ->fetch(Config::get('dispatch_success_tmpl'), $result);
  52. }
  53. $response = Response::create($result, $type)->header($header);
  54. throw new HttpResponseException($response);
  55. }
  56. /**
  57. * 操作错误跳转的快捷方法
  58. * @access protected
  59. * @param mixed $msg 提示信息
  60. * @param string $url 跳转的URL地址
  61. * @param mixed $data 返回的数据
  62. * @param integer $wait 跳转等待时间
  63. * @param array $header 发送的Header信息
  64. * @return void
  65. */
  66. protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
  67. {
  68. if (is_null($url)) {
  69. $url = Request::instance()->isAjax() ? '' : 'javascript:history.back(-1);';
  70. } elseif ('' !== $url) {
  71. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : Url::build($url);
  72. }
  73. $result = [
  74. 'code' => 0,
  75. 'msg' => $msg,
  76. 'data' => $data,
  77. 'url' => $url,
  78. 'wait' => $wait,
  79. ];
  80. $type = $this->getResponseType();
  81. if ('html' == strtolower($type)) {
  82. $result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
  83. ->fetch(Config::get('dispatch_error_tmpl'), $result);
  84. }
  85. $response = Response::create($result, $type)->header($header);
  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' => Request::instance()->server('REQUEST_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. $isAjax = Request::instance()->isAjax();
  137. return $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type');
  138. }
  139. }