Upload.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\controller;
  13. use think\response\Json;
  14. use app\api\service\Upload as UploadService;
  15. use app\common\enum\file\FileType as FileTypeEnum;
  16. /**
  17. * 文件上传管理
  18. * Class Upload
  19. * @package app\api\controller
  20. */
  21. class Upload extends Controller
  22. {
  23. /**
  24. * 图片上传接口
  25. * @param bool $checkLogin 是否验证登录
  26. * @return Json
  27. * @throws \cores\exception\BaseException
  28. * @throws \think\Exception
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\DbException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. */
  33. public function image(bool $checkLogin = true): Json
  34. {
  35. // 执行文件上传
  36. $service = new UploadService();
  37. if (!$service->upload(FileTypeEnum::IMAGE, $checkLogin)) {
  38. return $this->renderError('文件上传失败:' . $service->getError());
  39. }
  40. // 图片上传成功
  41. return $this->renderSuccess(['fileInfo' => $service->getFileInfo()], '文件上传成功');
  42. }
  43. }