FileController.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace api\modules\v1\controllers;
  3. use api\controllers\OnAuthController;
  4. use common\helpers\UploadHelper;
  5. use common\models\common\Attachment;
  6. /**
  7. * 文件上传
  8. *
  9. * Class FileController
  10. * @Author: hua
  11. * @DateTime: 2021/10/14 9:46
  12. * @Copyright: copyright (c) 2021 广东七件事集团
  13. * @package api\modules\v1\controllers
  14. */
  15. class FileController extends OnAuthController
  16. {
  17. public $modelClass = '';
  18. /**
  19. * 不用进行登录验证的方法
  20. * 例如: ['index', 'update', 'create', 'view', 'delete']
  21. * 默认全部需要验证
  22. *
  23. * @var array
  24. */
  25. protected $authOptional = ['index', 'search','upload'];
  26. /**
  27. * 上传
  28. * @Author: hua
  29. * @DateTime: 2021/10/14 9:46
  30. * @Copyright: copyright (c) 2021 广东七件事集团
  31. * @return mixed|void
  32. */
  33. public function actionUpload()
  34. {
  35. try {
  36. ini_set('memory_limit', '512M');
  37. $post = \Yii::$app->request->post();
  38. if (empty($post['upload_type']) || !isset(Attachment::$uploadTypeExplain[$post['upload_type']])) {
  39. return $this->error('上传类型有误');
  40. }
  41. $config = \Yii::$app->services->attachment->getStorageConfig($this->mall_id);
  42. $config['mall_id'] = $this->mall_id;
  43. $upload = new UploadHelper($config, $post['upload_type']);
  44. $upload->verifyFile();
  45. $upload->save();
  46. return $this->success('上传成功',$upload->getBaseInfo($this->mall_id,$this->user->mch_id ?? 0,0));
  47. } catch (\Exception $e) {
  48. return $this->error('上传失败:'.$e->getMessage());
  49. }
  50. }
  51. }