作者 lyh

gx

... ... @@ -5,6 +5,7 @@ namespace App\Http\Controllers\File;
use App\Enums\Common\Code;
use App\Models\File\File;
use App\Models\File\Image as ImageModel;
use App\Models\Project\Project;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;
... ... @@ -34,7 +35,7 @@ class FileController
public $token = '';//token
public $cache = '';//缓存数据
public $upload_location = 1;
public $image_type = [
1 => 'file_other',
0 => 'file'
... ... @@ -322,6 +323,11 @@ class FileController
if(isset($this->param['refer_type']) && $this->param['refer_type'] == 1){
$this->path = $this->uploads['path_a'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
}else{
$projectModel = new Project();
$project_info = $projectModel->read(['id'=>$this->cache['project_id']],['upload_location']);
if($project_info['upload_location'] != 1){
$this->upload_location = 0;
}
$this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
}
}
... ...
... ... @@ -6,6 +6,7 @@ use App\Enums\Common\Code;
use App\Http\Controllers\Controller;
use App\Http\Controllers\type;
use App\Models\File\Image as ImageModel;
use App\Models\Project\Project;
use App\Services\CosService;
use App\Services\TencentCosService;
use Illuminate\Http\Exceptions\HttpResponseException;
... ... @@ -27,8 +28,6 @@ class ImageController extends Controller
'Content-Description' => 'File Transfer',
],
];
const TYPE = 1;
public $path = '';//路径
public $config = '';//存储默认配置
... ... @@ -41,6 +40,7 @@ class ImageController extends Controller
public $cache = '';//缓存数据
public $upload_location = 1;
public $image_type = [
1 => 'image_product',
2 => 'image_news',
... ... @@ -127,7 +127,7 @@ class ImageController extends Controller
$this->setUrl();
if ($type == 'multi') {
return $this->multi($files);
} else {
}else{
return $this->single($files);
}
}
... ... @@ -166,8 +166,11 @@ class ImageController extends Controller
if ($rs === false) {
return $this->response('添加失败', Code::USER_ERROR);
}
$cosService = new CosService();
$cosService->uploadFile($this->path,$fileName);
//同步数据到cos
if($this->upload_location == 1){
$cosService = new CosService();
$cosService->uploadFile($this->path,$fileName);
}
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
}
... ... @@ -226,6 +229,11 @@ class ImageController extends Controller
'refer'=>$this->param['refer'] ?? '',
];
$data[] = ['image'=>$hash];
//同步数据到cos
if($this->upload_location == 1){
$cosService = new CosService();
$cosService->uploadFile($this->path,$fileName);
}
}
$imageModel->insert($save_data);
return $this->response('图片资源',Code::SUCCESS,$data);
... ... @@ -318,6 +326,11 @@ class ImageController extends Controller
if(isset($this->param['refer_type']) && $this->param['refer_type'] == 1){
$this->path = $this->uploads['path_a'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
}else{
$projectModel = new Project();
$project_info = $projectModel->read(['id'=>$this->cache['project_id']],['upload_location']);
if($project_info['upload_location'] != 1){
$this->upload_location = 0;
}
$this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
}
}
... ...
... ... @@ -31,13 +31,35 @@ class CosService
],
]);
$key = $path.'/'.$filename;
$response = $cosClient->putObject([
$cosClient->putObject([
'Bucket' => $cos['bucket'],
'Key' => $key,
'Body' => fopen(config('filesystems.disks.upload')['root'].$path.'/'.$filename, 'r'),
]);
var_dump($response);
die();
return $key;
}
/**
* @param $image_name
* @remark :获取图片访问链接
* @name :getImageUrl
* @author :lyh
* @method :post
* @time :2023/7/19 16:08
*/
public function getImageUrl($image_name)
{
$cos = config('filesystems.disks.cos');
$cosClient = new Client([
'region' => $cos['region'],
'credentials' => [
'secretId' => $cos['credentials']['secretId'],
'secretKey' => $cos['credentials']['secretKey'],
],
]);
$imageUrl = $cosClient->getObjectUrl(config('app.cos_bucket'), basename($image_name));
return response()->json([
'image_url' => $imageUrl,
]);
}
}
... ...