Jump.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 跳转traits
  13. // +----------------------------------------------------------------------
  14. namespace app\common\library\traits;
  15. use think\App;
  16. use think\exception\HttpResponseException;
  17. use think\Response;
  18. trait Jump
  19. {
  20. /**
  21. * Request实例
  22. * @var \think\Request
  23. */
  24. protected $request;
  25. /**
  26. * 应用实例
  27. * @var \think\App
  28. */
  29. protected $app;
  30. /**
  31. * 构造方法
  32. * @access public
  33. * @param App $app 应用对象
  34. */
  35. public function __construct(App $app)
  36. {
  37. $this->app = $app;
  38. $this->request = $this->app->request;
  39. }
  40. /**
  41. * 操作成功跳转的快捷方法
  42. * @access protected
  43. * @param mixed $msg 提示信息
  44. * @param string|null $url 跳转的URL地址
  45. * @param mixed $data 返回的数据
  46. * @param integer $wait 跳转等待时间
  47. * @param array $header 发送的Header信息
  48. * @return void
  49. */
  50. protected function success($msg = '', ?string $url = null, $data = '', int $wait = 3, array $header = [])
  51. {
  52. if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
  53. $url = $_SERVER["HTTP_REFERER"];
  54. } elseif ($url) {
  55. $url = (strpos($url, '://') || str_starts_with($url, '/')) ? $url : (string) $this->app->route->buildUrl($url);
  56. }
  57. $result = [
  58. 'code' => 1,
  59. 'msg' => $msg,
  60. 'data' => $data,
  61. 'url' => $url,
  62. 'wait' => $wait,
  63. ];
  64. $type = $this->getResponseType();
  65. // 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
  66. if ('html' == strtolower($type)) {
  67. $type = 'view';
  68. $response = Response::create($this->app->config->get('app.dispatch_success_tmpl'), $type)->assign($result)->header($header);
  69. } else {
  70. $response = Response::create($result, $type)->header($header);
  71. }
  72. throw new HttpResponseException($response);
  73. }
  74. /**
  75. * 操作错误跳转的快捷方法
  76. * @access protected
  77. * @param mixed $msg 提示信息
  78. * @param string|null $url 跳转的URL地址
  79. * @param mixed $data 返回的数据
  80. * @param integer $wait 跳转等待时间
  81. * @param array $header 发送的Header信息
  82. * @return void
  83. */
  84. protected function error($msg = '', ?string $url = null, $data = '', int $wait = 3, array $header = [])
  85. {
  86. if (is_null($url)) {
  87. $url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);';
  88. } elseif ($url) {
  89. $url = (strpos($url, '://') || str_starts_with($url, '/')) ? $url : (string) $this->app->route->buildUrl($url);
  90. }
  91. $result = [
  92. 'code' => 0,
  93. 'msg' => $msg,
  94. 'data' => $data,
  95. 'url' => $url,
  96. 'wait' => $wait,
  97. ];
  98. $type = $this->getResponseType();
  99. if ('html' == strtolower($type)) {
  100. $type = 'view';
  101. $response = Response::create($this->app->config->get('app.dispatch_error_tmpl'), $type)->assign($result)->header($header);
  102. } else {
  103. $response = Response::create($result, $type)->header($header);
  104. }
  105. throw new HttpResponseException($response);
  106. }
  107. /**
  108. * 返回封装后的API数据到客户端
  109. * @access protected
  110. * @param mixed $data 要返回的数据
  111. * @param integer $code 返回的code
  112. * @param mixed $msg 提示信息
  113. * @param string $type 返回数据格式
  114. * @param array $header 发送的Header信息
  115. * @return void
  116. */
  117. protected function result($data, $code = 0, $msg = '', string $type = '', array $header = [])
  118. {
  119. $result = [
  120. 'code' => $code,
  121. 'msg' => $msg,
  122. 'time' => time(),
  123. 'data' => $data,
  124. ];
  125. $type = $type ?: $this->getResponseType();
  126. $response = Response::create($result, $type)->header($header);
  127. throw new HttpResponseException($response);
  128. }
  129. /**
  130. * URL重定向
  131. * @access protected
  132. * @param string $url 跳转的URL表达式
  133. * @param integer $code http code
  134. * @param array $with 隐式传参
  135. * @return void
  136. */
  137. protected function redirect($url, int $code = 302, $with = [])
  138. {
  139. $response = Response::create($url, 'redirect');
  140. $response->code($code)->with($with);
  141. throw new HttpResponseException($response);
  142. }
  143. /**
  144. * 获取当前的response 输出类型
  145. * @access protected
  146. * @return string
  147. */
  148. protected function getResponseType(): string
  149. {
  150. return $this->request->isJson() || $this->request->isAjax() ? 'json' : 'html';
  151. }
  152. }