UploadController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: weifeng
  5. * Date: 2019-07-19
  6. * Time: 13:57
  7. */
  8. namespace app\frontend\controllers;
  9. use app\common\components\BaseController;
  10. use app\common\facades\Setting;
  11. use app\common\services\ImageZip;
  12. use app\common\services\MiniFileLimitService;
  13. use app\common\services\upload\UploadService;
  14. use app\platform\modules\system\models\SystemSetting;
  15. class UploadController extends BaseController
  16. {
  17. public function uploadPic()
  18. {
  19. $attach = request()->attach;
  20. $ingress = request()->ingress;
  21. $file = request()->file('file');
  22. $local_upload = request()->local_upload == 1 ? true : false;
  23. $upload_type = request()->upload_type;
  24. if (!$file) {
  25. return $this->errorJson('文件上传失败.');
  26. }
  27. if (!$file->isValid()) {
  28. return $this->errorJson('文件上传失败.');
  29. }
  30. if ($ingress && $upload_type == 'image' && $this->isMiniCheckImage()) {
  31. if ($file->getClientSize() > 1024*1024) {
  32. return $this->errorJson('小程序图片安全验证图片不能大于1M');
  33. }
  34. $check_result = (new MiniFileLimitService())->checkImg($file);
  35. if ($check_result['errcode'] == 87014) {
  36. return $this->errorJson('内容含有违法违规信息');
  37. }
  38. }
  39. $file_name = '';
  40. if (request()->need_name) {
  41. $file_name = $file->getClientOriginalName();
  42. }
  43. if ($local_upload) {
  44. $url_arr = (new UploadService())->upload($file, $upload_type, '', $file_name, false);
  45. } else {
  46. $url_arr = (new UploadService())->upload($file, $upload_type, '', $file_name);
  47. }
  48. return $this->successJson('上传成功', [
  49. 'img' => $url_arr['relative_path'],
  50. 'img_url' => $url_arr['absolute_path'],
  51. 'attach' => $attach,
  52. ]);
  53. }
  54. protected function isMiniCheckImage()
  55. {
  56. if (!app('plugins')->isEnabled('min-app')) {
  57. return false;
  58. }
  59. $set = Setting::get('plugin.min_app');
  60. if ($set['switch'] != 1) {
  61. return false;
  62. }
  63. if ($set['image_check'] != 1) {
  64. return false;
  65. }
  66. return true;
  67. }
  68. }