BaseController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\admin;
  9. use think\App;
  10. use think\exception\HttpResponseException;
  11. use think\facade\Request;
  12. /**
  13. * 控制器基础类
  14. */
  15. abstract class BaseController
  16. {
  17. /**
  18. * Request实例
  19. * @var \think\Request
  20. */
  21. protected $request;
  22. /**
  23. * 应用实例
  24. * @var \think\App
  25. */
  26. protected $app;
  27. /**
  28. * 是否批量验证
  29. * @var bool
  30. */
  31. protected $batchValidate = false;
  32. /**
  33. * 控制器中间件
  34. * @var array
  35. */
  36. protected $middleware = [];
  37. /**
  38. * 构造方法
  39. * @access public
  40. * @param App $app 应用对象
  41. */
  42. public function __construct(App $app)
  43. {
  44. $this->app = $app;
  45. $this->request = $this->app->request;
  46. // 控制器初始化
  47. $this->initialize();
  48. }
  49. // 初始化
  50. protected function initialize()
  51. {
  52. $this->param = $this->request->param();
  53. }
  54. //
  55. // 以下为新增,为了使用旧版的 success error redirect 跳转 start
  56. //
  57. /**
  58. * 操作成功跳转的快捷方法
  59. * @access protected
  60. * @param mixed $msg 提示信息
  61. * @param string $url 跳转的URL地址
  62. * @param mixed $data 返回的数据
  63. * @param integer $wait 跳转等待时间
  64. * @param array $header 发送的Header信息
  65. * @return void
  66. */
  67. protected function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
  68. {
  69. if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
  70. $url = $_SERVER["HTTP_REFERER"];
  71. } elseif ($url) {
  72. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url);
  73. }
  74. $result = [
  75. 'code' => 0,
  76. 'msg' => $msg,
  77. 'data' => $data,
  78. 'url' => $url,
  79. 'wait' => $wait,
  80. ];
  81. $type = $this->getResponseType();
  82. if ($type == 'html') {
  83. $response = view($this->app->config->get('app.dispatch_success_tmpl'), $result);
  84. } else if ($type == 'json') {
  85. $response = json($result);
  86. }
  87. throw new HttpResponseException($response);
  88. }
  89. /**
  90. * 操作错误跳转的快捷方法
  91. * @access protected
  92. * @param mixed $msg 提示信息
  93. * @param string $url 跳转的URL地址
  94. * @param mixed $data 返回的数据
  95. * @param integer $wait 跳转等待时间
  96. * @param array $header 发送的Header信息
  97. * @return void
  98. */
  99. protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
  100. {
  101. if (is_null($url)) {
  102. $url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);';
  103. } elseif ($url) {
  104. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
  105. }
  106. $result = [
  107. 'code' => 1,
  108. 'msg' => $msg,
  109. 'data' => $data,
  110. 'url' => $url,
  111. 'wait' => $wait,
  112. ];
  113. $type = $this->getResponseType();
  114. if ($type == 'html') {
  115. $response = view($this->app->config->get('app.dispatch_error_tmpl'), $result);
  116. } else if ($type == 'json') {
  117. $response = json($result);
  118. }
  119. throw new HttpResponseException($response);
  120. }
  121. /**
  122. * URL重定向 自带重定向无效
  123. * @access protected
  124. * @param string $url 跳转的URL表达式
  125. * @param array|integer $params 其它URL参数
  126. * @param integer $code http code
  127. * @param array $with 隐式传参
  128. * @return void
  129. */
  130. protected function redirect($url, $params = [], $code = 302, $with = [])
  131. {
  132. $response = Response::create($url, 'redirect');
  133. if (is_integer($params)) {
  134. $code = $params;
  135. $params = [];
  136. }
  137. $response->code($code)->params($params)->with($with);
  138. throw new HttpResponseException($response);
  139. }
  140. /**
  141. * 获取当前的response 输出类型
  142. * @access protected
  143. * @return string
  144. */
  145. protected function getResponseType()
  146. {
  147. return $this->request->isJson() || $this->request->isAjax() ? 'json' : 'html';
  148. }
  149. //
  150. // 以上为新增,为了使用旧版的 success error redirect 跳转 end
  151. //
  152. }