BaseController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. declare(strict_types=1);
  3. namespace app;
  4. use think\App;
  5. use think\View;
  6. use think\exception\HttpException;
  7. use think\exception\HttpResponseException;
  8. use think\exception\ValidateException;
  9. use think\Response;
  10. use think\facade\Validate;
  11. use think\facade\Filesystem;
  12. use think\File;
  13. use app\model\Files;
  14. /**
  15. * 控制器基础类
  16. */
  17. abstract class BaseController
  18. {
  19. /**
  20. * Request实例
  21. * @var \think\Request
  22. */
  23. protected $request;
  24. /**
  25. * 应用实例
  26. * @var \think\App
  27. */
  28. protected $app;
  29. /**
  30. * 是否批量验证
  31. * @var bool
  32. */
  33. protected $batchValidate = false;
  34. /**
  35. * 控制器中间件
  36. * @var array
  37. */
  38. protected $middleware = [];
  39. public function __construct(App $app, View $view)
  40. {
  41. $this->app = $app;
  42. $this->request = $this->app->request;
  43. $this->view = $view;
  44. // 控制器初始化
  45. $this->initialize();
  46. }
  47. // 初始化
  48. protected function initialize()
  49. {
  50. }
  51. protected function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
  52. {
  53. if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
  54. $url = $_SERVER["HTTP_REFERER"];
  55. } elseif ($url) {
  56. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url);
  57. }
  58. $result = [
  59. 'code' => 1,
  60. 'msg' => $msg,
  61. 'data' => $data,
  62. 'url' => $url,
  63. 'wait' => $wait,
  64. ];
  65. $type = $this->getResponseType();
  66. if ($type == 'html') {
  67. $response = view(config('app.dispatch_success_tmpl'), $result);
  68. } else if ($type == 'json') {
  69. $response = json($result);
  70. }
  71. throw new HttpResponseException($response);
  72. }
  73. protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
  74. {
  75. if (is_null($url)) {
  76. $url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);';
  77. } elseif ($url) {
  78. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
  79. }
  80. $result = [
  81. 'code' => 0,
  82. 'msg' => $msg,
  83. 'data' => $data,
  84. 'url' => $url,
  85. 'wait' => $wait,
  86. ];
  87. $type = $this->getResponseType();
  88. if ($type == 'html') {
  89. $response = view(config('app.dispatch_success_tmpl'), $result);
  90. } else if ($type == 'json') {
  91. $response = json($result);
  92. }
  93. throw new HttpResponseException($response);
  94. }
  95. protected function getResponseType()
  96. {
  97. return $this->request->isJson() || $this->request->isAjax() ? 'json' : 'html';
  98. }
  99. //上传前先检测上传模式 如果是oss客户端直传则直接返回 token 、key等信息
  100. public function upload()
  101. {
  102. $file = $this->request->file('file');
  103. $file_type = upload_replace(config('my.filetype')); //上传黑名单过滤
  104. if (!Validate::fileExt($file, $file_type)) {
  105. throw new ValidateException('文件类型验证失败');
  106. }
  107. if (!Validate::fileSize($file, config('my.filesize') * 1024 * 1024)) {
  108. throw new ValidateException('文件大小验证失败');
  109. }
  110. if ($url = $this->up($file)) {
  111. return $this->json(['data' => $url, 'url' => $url, 'weid' => weid()]);
  112. }
  113. }
  114. //开始上传
  115. protected function up($file)
  116. {
  117. $oss_settings = \app\model\OssUpload::getSettings();
  118. $is_local = $this->request->post('is_local');
  119. $cid = $this->request->post('cid');
  120. if (!empty($oss_settings['oss_accessKey']) && !empty($oss_settings['oss_secretKey']) && !empty($oss_settings['oss_bucket']) && !empty($oss_settings['oss_domain']) && $is_local != 1) {
  121. $url = \utils\oss\OssService::OssUpload($oss_settings, ['tmp_name' => $file->getPathname(), 'extension' => $file->extension()]);
  122. } else {
  123. $filename = Filesystem::disk('public')->putFile($this->getFileName(), $file, 'uniqid');
  124. $upload_type = $this->request->post('upload_type');
  125. if ($upload_type == 'file') {
  126. $url = config('filesystem.disks.public.url') . '/' . $filename;
  127. } else {
  128. $url = toimg(config('filesystem.disks.public.url') . '/' . $filename);
  129. }
  130. }
  131. $source_id = $this->userInfo['id'] ?? UID();
  132. if (Validate::fileExt($file, 'jpg,jpeg,png,gif')) {
  133. Files::create([
  134. 'weid' => weid(),
  135. 'cid' => (int) $cid,
  136. 'type' => $upload_type,
  137. 'name' => $filename,
  138. 'uri' => $url,
  139. 'source_id' => (int) $source_id
  140. ]);
  141. }
  142. return $url;
  143. }
  144. //获取上传的文件完整路径
  145. private function getFileName()
  146. {
  147. return app('http')->getName() . '/' . date(config('my.upload_subdir'));
  148. }
  149. //获取阿里云oss客户端上传地址
  150. private function getendpoint($oss_settings)
  151. {
  152. if (strpos($oss_settings['oss_domain'], 'aliyuncs.com') !== false) {
  153. if (strpos($oss_settings['oss_domain'], 'https') !== false) {
  154. $point = 'https://' . $oss_settings['oss_bucket'] . '.' . substr($oss_settings['oss_domain'], 8);
  155. } else {
  156. $point = 'http://' . $oss_settings['oss_bucket'] . '.' . substr($oss_settings['oss_domain'], 7);
  157. }
  158. } else {
  159. $point = $oss_settings['oss_domain'];
  160. }
  161. return $point;
  162. }
  163. //阿里云oss上传异步回调返回上传路径,放到这是因为这个地址必须外部能直接访问到
  164. function aliOssCallBack()
  165. {
  166. $oss_settings = \app\model\OssUpload::getSettings('ali');
  167. $body = file_get_contents('php://input');
  168. header("Content-Type: application/json");
  169. $url = $this->getendpoint($oss_settings['oss_domain']) . '/' . str_replace('%2F', '/', $body);
  170. return $this->json(['code' => 1, 'data' => $url, 'url' => $url]);
  171. }
  172. }