UploadAction.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace common\components;
  3. use common\helpers\UploadHelper;
  4. use common\models\common\Attachment;
  5. use common\traits\BaseControllerTrait;
  6. use Exception;
  7. use Yii;
  8. use yii\base\Action;
  9. class UploadAction extends Action
  10. {
  11. use BaseControllerTrait;
  12. public $mall_id = 0;
  13. public $store_id = 0;
  14. public $mch_id = 0;
  15. public $upload_type = '';
  16. public $group_id = 0;
  17. public $addons_name = '';
  18. public function run()
  19. {
  20. try {
  21. if (!$this->upload_type || !isset(Attachment::$uploadTypeExplain[$this->upload_type])) {
  22. throw new Exception('上传类型有误');
  23. }
  24. $config = Yii::$app->services->attachment->getStorageConfig($this->mall_id);
  25. $config['mall_id'] = $this->mall_id;
  26. $upload = new UploadHelper($config, $this->upload_type);
  27. $upload->verifyFile();
  28. // 添加禁止上传到系统图标目录
  29. if (in_array($this->group_id, array_column(Yii::$app->services->attachment->systemIconGroup, 'id'))) {
  30. throw new Exception('禁止上传至系统图标目录');
  31. }
  32. $upload->save();
  33. $this->success('上传成功',$upload->getBaseInfo($this->mall_id,$this->mch_id,$this->group_id,$this->store_id,$this->addons_name));
  34. } catch (Exception $e) {
  35. $this->error('上传失败:'.$e->getMessage());
  36. }
  37. }
  38. }