You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

168 lines
4.9 KiB

  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 int $wait 跳转等待时间
  31. * @param array $header 发送的 Header 信息
  32. * @return void
  33. * @throws HttpResponseException
  34. */
  35. protected function success($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
  36. {
  37. if (is_null($url) && !is_null(Request::instance()->server('HTTP_REFERER'))) {
  38. $url = Request::instance()->server('HTTP_REFERER');
  39. } elseif ('' !== $url && !strpos($url, '://') && 0 !== strpos($url, '/')) {
  40. $url = Url::build($url);
  41. }
  42. $type = $this->getResponseType();
  43. $result = [
  44. 'code' => 1,
  45. 'msg' => $msg,
  46. 'data' => $data,
  47. 'url' => $url,
  48. 'wait' => $wait,
  49. ];
  50. if ('html' == strtolower($type)) {
  51. $template = Config::get('template');
  52. $view = Config::get('view_replace_str');
  53. $result = ViewTemplate::instance($template, $view)
  54. ->fetch(Config::get('dispatch_success_tmpl'), $result);
  55. }
  56. $response = Response::create($result, $type)->header($header);
  57. throw new HttpResponseException($response);
  58. }
  59. /**
  60. * 操作错误跳转的快捷方法
  61. * @access protected
  62. * @param mixed $msg 提示信息
  63. * @param string $url 跳转的 URL 地址
  64. * @param mixed $data 返回的数据
  65. * @param int $wait 跳转等待时间
  66. * @param array $header 发送的 Header 信息
  67. * @return void
  68. * @throws HttpResponseException
  69. */
  70. protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
  71. {
  72. if (is_null($url)) {
  73. $url = Request::instance()->isAjax() ? '' : 'javascript:history.back(-1);';
  74. } elseif ('' !== $url && !strpos($url, '://') && 0 !== strpos($url, '/')) {
  75. $url = Url::build($url);
  76. }
  77. $type = $this->getResponseType();
  78. $result = [
  79. 'code' => 0,
  80. 'msg' => $msg,
  81. 'data' => $data,
  82. 'url' => $url,
  83. 'wait' => $wait,
  84. ];
  85. if ('html' == strtolower($type)) {
  86. $template = Config::get('template');
  87. $view = Config::get('view_replace_str');
  88. $result = ViewTemplate::instance($template, $view)
  89. ->fetch(Config::get('dispatch_error_tmpl'), $result);
  90. }
  91. $response = Response::create($result, $type)->header($header);
  92. throw new HttpResponseException($response);
  93. }
  94. /**
  95. * 返回封装后的 API 数据到客户端
  96. * @access protected
  97. * @param mixed $data 要返回的数据
  98. * @param int $code 返回的 code
  99. * @param mixed $msg 提示信息
  100. * @param string $type 返回数据格式
  101. * @param array $header 发送的 Header 信息
  102. * @return void
  103. * @throws HttpResponseException
  104. */
  105. protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
  106. {
  107. $result = [
  108. 'code' => $code,
  109. 'msg' => $msg,
  110. 'time' => Request::instance()->server('REQUEST_TIME'),
  111. 'data' => $data,
  112. ];
  113. $type = $type ?: $this->getResponseType();
  114. $response = Response::create($result, $type)->header($header);
  115. throw new HttpResponseException($response);
  116. }
  117. /**
  118. * URL 重定向
  119. * @access protected
  120. * @param string $url 跳转的 URL 表达式
  121. * @param array|int $params 其它 URL 参数
  122. * @param int $code http code
  123. * @param array $with 隐式传参
  124. * @return void
  125. * @throws HttpResponseException
  126. */
  127. protected function redirect($url, $params = [], $code = 302, $with = [])
  128. {
  129. if (is_integer($params)) {
  130. $code = $params;
  131. $params = [];
  132. }
  133. $response = new Redirect($url);
  134. $response->code($code)->params($params)->with($with);
  135. throw new HttpResponseException($response);
  136. }
  137. /**
  138. * 获取当前的 response 输出类型
  139. * @access protected
  140. * @return string
  141. */
  142. protected function getResponseType()
  143. {
  144. return Request::instance()->isAjax()
  145. ? Config::get('default_ajax_return')
  146. : Config::get('default_return_type');
  147. }
  148. }