| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\models\common;
- use app\models\UploadConfig;
- use yii\base\Model;
- use yii\helpers\Json;
- /**
- * @property UploadConfig $model
- */
- class CommonUpload extends Model
- {
- /**
- * @var string 阿里云
- */
- const STORAGE_ALIYUN = 'aliyun';
- /**
- * @var string 腾讯云
- */
- const STORAGE_QCLOUD = 'qcloud';
- /**
- * @var string 七牛
- */
- const STORAGE_QINIU = 'qiniu';
- public $model;
- public $store_id;
- public $storage_type;
- public $aliyun;
- public $qcloud;
- public $qiniu;
- public function rules()
- {
- return [
- [['storage_type',], 'string',],
- [['aliyun', 'qcloud', 'qiniu'], 'default', 'value' => (object)[],],
- ];
- }
- public function save()
- {
- $this->model->storage_type = !empty($this->storage_type) ? $this->storage_type : 'local';
- // 七牛云存储
- foreach ($this->qiniu as $k => $v) {
- $this->qiniu[$k] = trim($v);
- }
- $this->qiniu['domain'] = trim($this->qiniu['domain'], '/');
- $this->model->qiniu = Json::encode($this->qiniu);
- // 阿里云oss存储
- foreach ($this->aliyun as $k => $v) {
- $this->aliyun[$k] = trim($v);
- }
- $this->aliyun['domain'] = trim($this->aliyun['domain'], '/');
- $this->model->aliyun = Json::encode($this->aliyun);
- // 腾讯云cos存储
- foreach ($this->qcloud as $k => $v) {
- $this->qcloud[$k] = trim($v);
- }
- $this->qcloud['domain'] = trim($this->qcloud['domain'], '/');
- $this->model->qcloud = Json::encode($this->qcloud);
- if ($this->storage_type == self::STORAGE_QCLOUD) {
- preg_match('/(.*?)-(\d+)\.cos\.?(.*?)\.myqcloud\.com/', $this->qcloud['region'], $region);
- if (!$region || $region == 0) {
- return [
- 'code' => 1,
- 'msg' => '默认域名不正确'
- ];
- }
- }
- if ($this->model->isNewRecord) {
- $this->model->store_id = $this->store_id;
- $this->model->is_delete = 0;
- $this->model->created_at = time();
- }
- if ($this->model->save()) {
- return [
- 'code' => 0,
- 'msg' => '保存成功',
- ];
- } else {
- foreach ($this->model->errors as $error) {
- return [
- 'code' => 1,
- 'msg' => $error
- ];
- }
- }
- }
- }
|