zm
2021-03-25 d84ff6053b22269a6c59dc005e9efb8de6595988
commit | author | age
90c639 1 <?php
Z 2 /**
3  * @copyright ©2018 浙江禾匠信息科技
4  * @author Lu Wei
5  * @link http://www.zjhejiang.com/
6  * Created by IntelliJ IDEA
7  * Date Time: 2018/12/29 17:07
8  */
9
10
11 namespace app\forms;
12
13
14 use app\forms\admin\mall\MallOverrunForm;
15 use app\forms\common\attachment\AttachmentUpload;
16 use app\forms\common\attachment\CommonAttachment;
17 use app\forms\common\CommonOption;
18 use app\models\Attachment;
19 use app\models\AttachmentStorage;
20 use app\models\Model;
21 use app\models\Option;
22 use app\models\User;
23 use app\models\UserIdentity;
24 use Grafika\Grafika;
25 use Grafika\ImageInterface;
26 use OSS\OssClient;
27 use Qcloud\Cos\Client;
28 use Qiniu\Auth;
29 use Qiniu\Storage\UploadManager;
30 use yii\web\UploadedFile;
31 use function GuzzleHttp\Psr7\mimetype_from_filename;
32
33 class AttachmentUploadForm extends Model
34 {
35     /** @var UploadedFile */
36     public $file;
37
38     public $type;
39
40     public $attachment_group_id;
41
42     protected $docExt = ['txt', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'csv', 'pdf', 'md'];
43     protected $imageExt = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp',];
44     protected $videoExt = ['mp4', 'ogg', 'm4a',];
45
46     public function rules()
47     {
48         return [
49             [['file'], 'file'],
50             [['file'], 'validateExt'],
51             [['attachment_group_id'], 'integer'],
52             [['type'], 'string'],
53         ];
54     }
55
56     public function validateExt($a, $p)
57     {
58         $supportExt = array_merge($this->docExt, $this->imageExt, $this->videoExt);
59         if (!in_array($this->file->extension, $supportExt)) {
60             $this->addError($a, '不支持的文件类型: ' . $this->file->extension);
61         }
62
63         $option = CommonOption::get(Option::NAME_OVERRUN, 0, 'admin', (new MallOverrunForm())->getDefault());
64         if (in_array($this->file->extension, $this->imageExt)) {
65             if (($option['is_img_overrun'] == 'false' || $option['is_img_overrun'] == false) && $this->file->size > ($option['img_overrun'] * 1024 * 1024)) {
66                 $this->addError($a, '图片大小超出限制,当前大小为: '
67                     . (round($this->file->size / 1024 / 1024, 4)) . 'MB,最大限制为:'
68                     . $option['img_overrun'] . 'MB');
69             }
70         }
71
72         if (in_array($this->file->extension, $this->videoExt)) {
73             if (($option['is_video_overrun'] == 'false' || $option['is_video_overrun'] == false) && $this->file->size > ($option['video_overrun'] * 1024 * 1024)) {
74                 $this->addError($a, '视频大小超出限制,当前大小为: '
75                     . (round($this->file->size / 1024 / 1024, 4)) . 'MB,最大限制为:'
76                     . $option['video_overrun'] . 'MB');
77             }
78         }
79     }
80
81     public function save()
82     {
83         if (!$this->validate()) {
84             return $this->getErrorResponse($this);
85         }
86
87         try {
88             $mall = \Yii::$app->mall;
89         } catch (\Exception $e) {
90             $mall = null;
91         }
92
93         $user = null;
94         if (!\Yii::$app->user->isGuest) {
95             $user = \Yii::$app->user->identity;
96         }
97         try {
98             $storage = CommonAttachment::getCommon($user, $mall)->getAttachment();
99         } catch (\Exception $exception) {
100             return [
101                 'code' => 1,
102                 'msg' => $exception->getMessage(),
103             ];
104         }
105
106         if ($this->type === 'image') {
107             $type = 1;
108         } elseif ($this->type === 'video') {
109             $type = 2;
110         } else {
111             if (in_array($this->file->extension, $this->imageExt)) {
112                 $type = 1;
113             } elseif (in_array($this->file->extension, $this->videoExt)) {
114                 $type = 2;
115             } elseif (in_array($this->file->extension, $this->docExt)) {
116                 $type = 3;
117             } else {
118                 $type = 0;
119             }
120         }
121
122         $mallId = 0;
123         $mchId = 0;
124         if (!\Yii::$app->user->isGuest) {
125             /** @var User $user */
126             $user = \Yii::$app->user->identity;
127             $userIdentity = $user->identity;
128             if (
129                 $userIdentity
130                 && ($userIdentity->is_super_admin || $userIdentity->is_admin || $userIdentity->is_operator)
131             ) {
132                 $mallId = $mall ? $mall->id : 0;
133             } elseif (\Yii::$app->mchId && $mall) {
134                 $mallId = $mall->id;
135             } else {
136                 $mallId = 0;
137             }
138             $mchId = \Yii::$app->mchId ? \Yii::$app->mchId : 0;
139         }
140         try {
141             $attachmentUpload = new AttachmentUpload([
142                 'storage' => $storage,
143                 'file' => $this->file,
144                 'type' => $type,
145                 'mall_id' => $mallId,
146                 'mch_id' => $mchId,
147                 'attachment_group_id' => $this->attachment_group_id ? $this->attachment_group_id : 0
148             ]);
149             $attachment = $attachmentUpload->upload();
150             $attachment->thumb_url = $attachment->thumb_url ? $attachment->thumb_url : $attachment->url;
151             return [
152                 'code' => 0,
153                 'data' => $attachment,
154             ];
155         } catch (\Exception $exception) {
156             return [
157                 'code' => 1,
158                 'msg' => $exception->getMessage()
159             ];
160         }
161     }
162
163     public static function getInstanceFromFile($localFilePath)
164     {
165         return AttachmentUpload::getInstanceFromFile($localFilePath);
166     }
167 }