JumpTrait.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 string|null $msg 提示信息
  15. * @param mixed|string $data 返回的数据
  16. * @param string|null $url 跳转的 URL 地址
  17. * @param int $wait 跳转等待时间
  18. * @param array $header 发送的 Header 信息
  19. * @return void
  20. */
  21. protected function success(?string $msg = null, mixed $data = '', ?string $url = null, int $wait = 3, array $header = []): void
  22. {
  23. if (is_null($url)) {
  24. $url = app()->request->server('HTTP_REFERER');
  25. }elseif ($url) {
  26. $url = (strpos($url, '://') || str_starts_with($url, '/')) ? $url : app('route')->buildUrl($url)->__toString();
  27. }
  28. $result = [
  29. 'code' => 1,
  30. 'msg' => $msg,
  31. 'data' => $data,
  32. 'url' => $url,
  33. 'wait' => $wait,
  34. '__token__' => request()->buildToken('__token__'),
  35. ];
  36. $type = $this->getResponseType();
  37. if ($type == 'html') {
  38. $response = view(config('app.dispatch_success_tmpl'), $result);
  39. }else {
  40. $response = json($result);
  41. }
  42. throw new HttpResponseException($response);
  43. }
  44. /**
  45. * 操作错误跳转的快捷方法
  46. * @access protected
  47. * @param string|null $msg 提示信息
  48. * @param mixed $data 返回的数据
  49. * @param string|null $url 跳转的 URL 地址
  50. * @param int $wait 跳转等待时间
  51. * @param array $header 发送的 Header 信息
  52. * @return void
  53. */
  54. protected function error(?string $msg = null, mixed $data = '', ?string $url = null, int $wait = 3, array $header = []): void
  55. {
  56. if (is_null($url)) {
  57. $url = request()->isAjax() ? '' : 'javascript:history.back(-1);';
  58. }elseif ($url) {
  59. $url = (strpos($url, '://') || str_starts_with($url, '/')) ? $url : app('route')->buildUrl($url)->__toString();
  60. }
  61. $type = $this->getResponseType();
  62. $result = [
  63. 'code' => 0,
  64. 'msg' => $msg,
  65. 'data' => $data,
  66. 'url' => $url,
  67. 'wait' => $wait,
  68. '__token__' => request()->buildToken('__token__'),
  69. ];
  70. if ($type == 'html') {
  71. $response = view(config('app.dispatch_error_tmpl'), $result);
  72. }else {
  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 string|null $msg 提示信息
  83. * @param string $type 返回数据格式
  84. * @param array $header 发送的 Header 信息
  85. * @return void
  86. */
  87. protected function result(mixed $data, int $code = 0, ?string $msg = '', string $type = '', array $header = []): void
  88. {
  89. $result = [
  90. 'code' => $code,
  91. 'msg' => $msg,
  92. 'time' => time(),
  93. 'data' => $data,
  94. ];
  95. $type = $type ?: $this->getResponseType();
  96. $response = Response::create($result, $type)->header($header);
  97. throw new HttpResponseException($response);
  98. }
  99. /**
  100. * URL 重定向
  101. * @access protected
  102. * @param string $url 跳转的 URL 表达式
  103. * @param int $code http code
  104. * @return void
  105. * @throws HttpResponseException
  106. */
  107. protected function redirect(string $url = '', int $code = 302): void
  108. {
  109. $response = Response::create($url, 'redirect', $code);
  110. throw new HttpResponseException($response);
  111. }
  112. /**
  113. * 获取当前的 response 输出类型
  114. * @access protected
  115. * @return string
  116. */
  117. protected function getResponseType(): string
  118. {
  119. return (request()->isJson() || request()->isAjax() || request()->isPost()) ? 'json' : 'html';
  120. }
  121. }