Upload.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 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\store\controller;
  13. use app\store\model\member\MemberWelfare as MemberWelfareModel;
  14. use app\store\model\Setting as SettingModel;
  15. use app\store\model\UploadFile as UploadFileModel;
  16. use app\common\enum\Setting as SettingEnum;
  17. use app\common\enum\file\FileType as FileTypeEnum;
  18. use app\common\library\storage\Driver as StorageDriver;
  19. /**
  20. * 文件库管理
  21. * Class Upload
  22. * @package app\store\controller
  23. */
  24. class Upload extends Controller
  25. {
  26. // 当前商城的上传设置
  27. private $config;
  28. /**
  29. * 构造方法
  30. * @throws \app\common\exception\BaseException
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public function initialize()
  36. {
  37. parent::initialize();
  38. // 存储配置信息
  39. $this->config = SettingModel::getItem(SettingEnum::STORAGE);
  40. }
  41. /**
  42. * 图片上传接口
  43. * @param int $groupId 分组ID
  44. * @return array
  45. * @throws \think\Exception
  46. */
  47. public function image(int $groupId = 0)
  48. {
  49. // 实例化存储驱动
  50. $storage = new StorageDriver($this->config);
  51. // 设置上传文件的信息
  52. $storage->setUploadFile('iFile')
  53. ->setRootDirName((string)$this->getStoreId())
  54. ->setValidationScene('image')
  55. ->upload();
  56. // 执行文件上传
  57. if (!$storage->upload()) {
  58. return $this->renderError('图片上传失败:' . $storage->getError());
  59. }
  60. // 文件信息
  61. $fileInfo = $storage->getSaveFileInfo();
  62. // 添加文件库记录
  63. $model = new UploadFileModel;
  64. $model->add($fileInfo, FileTypeEnum::IMAGE, $groupId);
  65. // 图片上传成功
  66. return $this->renderSuccess(['fileInfo' => $model->toArray()], '图片上传成功');
  67. }
  68. /**
  69. * 视频上传接口
  70. * @return array
  71. * @throws \think\Exception
  72. */
  73. public function video(int $groupId = 0){
  74. set_time_limit(0);
  75. // 实例化存储驱动
  76. $storage = new StorageDriver($this->config);
  77. // 设置上传文件的信息
  78. $storage->setUploadFile('iFile')
  79. ->setRootDirName((string)$this->getStoreId())
  80. ->setValidationScene('video')
  81. ->upload();
  82. // 执行文件上传
  83. if (!$storage->upload()) {
  84. return $this->renderError('视频上传失败:' . $storage->getError());
  85. }
  86. // 文件信息
  87. $fileInfo = $storage->getSaveFileInfo();
  88. // 添加文件库记录
  89. // todo 需要将封面图保存到cover字段
  90. $model = new UploadFileModel;
  91. $model->add($fileInfo, FileTypeEnum::VIDEO, $groupId);
  92. // 视频上传成功
  93. return $this->renderSuccess(['fileInfo' => $model->toArray()], '视频上传成功');
  94. }
  95. /**
  96. * 文件上传接口zq
  97. * @return array
  98. * @throws \think\Exception
  99. */
  100. public function annex(int $groupId = 0){
  101. set_time_limit(0);
  102. // 实例化存储驱动
  103. $storage = new StorageDriver($this->config);
  104. // 设置上传文件的信息
  105. $storage->setUploadFile('iFile')
  106. ->setRootDirName((string)$this->getStoreId())
  107. ->setValidationScene('annex')
  108. ->upload();
  109. // 执行文件上传
  110. if (!$storage->upload()) {
  111. return $this->renderError('文件上传失败:' . $storage->getError());
  112. }
  113. // 文件信息
  114. $fileInfo = $storage->getSaveFileInfo();
  115. // 添加文件库记录
  116. $model = new UploadFileModel;
  117. $model->add($fileInfo, FileTypeEnum::ANNEX, $groupId);
  118. // 视频上传成功
  119. return $this->renderSuccess(['fileInfo' => $model->toArray()], '文件上传成功');
  120. }
  121. }