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