JumpTrait.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\common\traits;
  3. use think\exception\HttpResponseException;
  4. use think\Response;
  5. /**
  6. * Trait JumpTrait
  7. * @package app\common\traits
  8. */
  9. trait JumpTrait
  10. {
  11. /**
  12. * 操作成功跳转的快捷方法
  13. * @access protected
  14. * @param mixed $msg 提示信息
  15. * @param mixed $data 返回的数据
  16. * @param string $url 跳转的 URL 地址
  17. * @param int $wait 跳转等待时间
  18. * @param array $header 发送的 Header 信息
  19. * @return void
  20. * @throws HttpResponseException
  21. */
  22. protected function success($msg = '', $data = '', $url = null, $wait = 3, array $header = [])
  23. {
  24. if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
  25. $url = $_SERVER["HTTP_REFERER"];
  26. } elseif ($url) {
  27. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url)->__toString();
  28. }
  29. $result = [
  30. 'code' => 1,
  31. 'msg' => $msg,
  32. 'data' => $data,
  33. 'url' => $url,
  34. 'wait' => $wait,
  35. ];
  36. $type = $this->getResponseType();
  37. if ($type == 'html') {
  38. $response = view(app('config')->get('app.dispatch_success_tmpl'), $result);
  39. } elseif ($type == 'json') {
  40. $response = json($result);
  41. }
  42. throw new HttpResponseException($response);
  43. }
  44. /**
  45. * 操作错误跳转的快捷方法
  46. * @access protected
  47. * @param mixed $msg 提示信息
  48. * @param mixed $data 返回的数据
  49. * @param string $url 跳转的 URL 地址
  50. * @param int $wait 跳转等待时间
  51. * @param array $header 发送的 Header 信息
  52. * @return void
  53. * @throws HttpResponseException
  54. */
  55. protected function error($msg = '', $data = '', $url = null, $wait = 3, array $header = [])
  56. {
  57. if (is_null($url)) {
  58. $url = request()->isAjax() ? '' : 'javascript:history.back(-1);';
  59. } elseif ($url) {
  60. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url)->__toString();
  61. }
  62. $type = $this->getResponseType();
  63. $result = [
  64. 'code' => 0,
  65. 'msg' => $msg,
  66. 'data' => $data,
  67. 'url' => $url,
  68. 'wait' => $wait,
  69. ];
  70. if ($type == 'html') {
  71. $response = view(app('config')->get('app.dispatch_error_tmpl'), $result);
  72. } elseif ($type == 'json') {
  73. $response = json($result);
  74. }
  75. throw new HttpResponseException($response);
  76. }
  77. /**
  78. * 返回封装后的 API 数据到客户端
  79. * @access protected
  80. * @param mixed $data 要返回的数据
  81. * @param int $code 返回的 code
  82. * @param mixed $msg 提示信息
  83. * @param string $type 返回数据格式
  84. * @param array $header 发送的 Header 信息
  85. * @return void
  86. * @throws HttpResponseException
  87. */
  88. protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
  89. {
  90. $result = [
  91. 'code' => $code,
  92. 'msg' => $msg,
  93. 'time' => time(),
  94. 'data' => $data,
  95. ];
  96. $type = $type ?: $this->getResponseType();
  97. $response = Response::create($result, $type)->header($header);
  98. throw new HttpResponseException($response);
  99. }
  100. /**
  101. * URL 重定向
  102. * @access protected
  103. * @param string $url 跳转的 URL 表达式
  104. * @param array|int $params 其它 URL 参数
  105. * @param int $code http code
  106. * @param array $with 隐式传参
  107. * @return void
  108. * @throws HttpResponseException
  109. */
  110. protected function redirect($url = [], $params = [], $code = 302)
  111. {
  112. if (is_integer($params)) {
  113. $code = $params;
  114. $params = [];
  115. }
  116. $response = Response::create($url, 'redirect', $code);
  117. throw new HttpResponseException($response);
  118. }
  119. /**
  120. * 获取当前的 response 输出类型
  121. * @access protected
  122. * @return string
  123. */
  124. protected function getResponseType()
  125. {
  126. return (request()->isJson() || request()->isAjax() || request()->isPost()) ? 'json' : 'html';
  127. }
  128. }