作者 lyh

gx

@@ -5,6 +5,7 @@ namespace App\Http\Controllers\File; @@ -5,6 +5,7 @@ namespace App\Http\Controllers\File;
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 use App\Models\File\File; 6 use App\Models\File\File;
7 use App\Models\File\Image as ImageModel; 7 use App\Models\File\Image as ImageModel;
  8 +use App\Models\Project\Project;
8 use Illuminate\Http\Exceptions\HttpResponseException; 9 use Illuminate\Http\Exceptions\HttpResponseException;
9 use Illuminate\Http\JsonResponse; 10 use Illuminate\Http\JsonResponse;
10 use Illuminate\Support\Facades\Cache; 11 use Illuminate\Support\Facades\Cache;
@@ -34,7 +35,7 @@ class FileController @@ -34,7 +35,7 @@ class FileController
34 public $token = '';//token 35 public $token = '';//token
35 36
36 public $cache = '';//缓存数据 37 public $cache = '';//缓存数据
37 - 38 + public $upload_location = 1;
38 public $image_type = [ 39 public $image_type = [
39 1 => 'file_other', 40 1 => 'file_other',
40 0 => 'file' 41 0 => 'file'
@@ -322,6 +323,11 @@ class FileController @@ -322,6 +323,11 @@ class FileController
322 if(isset($this->param['refer_type']) && $this->param['refer_type'] == 1){ 323 if(isset($this->param['refer_type']) && $this->param['refer_type'] == 1){
323 $this->path = $this->uploads['path_a'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m'); 324 $this->path = $this->uploads['path_a'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
324 }else{ 325 }else{
  326 + $projectModel = new Project();
  327 + $project_info = $projectModel->read(['id'=>$this->cache['project_id']],['upload_location']);
  328 + if($project_info['upload_location'] != 1){
  329 + $this->upload_location = 0;
  330 + }
325 $this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m'); 331 $this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
326 } 332 }
327 } 333 }
@@ -6,6 +6,7 @@ use App\Enums\Common\Code; @@ -6,6 +6,7 @@ use App\Enums\Common\Code;
6 use App\Http\Controllers\Controller; 6 use App\Http\Controllers\Controller;
7 use App\Http\Controllers\type; 7 use App\Http\Controllers\type;
8 use App\Models\File\Image as ImageModel; 8 use App\Models\File\Image as ImageModel;
  9 +use App\Models\Project\Project;
9 use App\Services\CosService; 10 use App\Services\CosService;
10 use App\Services\TencentCosService; 11 use App\Services\TencentCosService;
11 use Illuminate\Http\Exceptions\HttpResponseException; 12 use Illuminate\Http\Exceptions\HttpResponseException;
@@ -27,8 +28,6 @@ class ImageController extends Controller @@ -27,8 +28,6 @@ class ImageController extends Controller
27 'Content-Description' => 'File Transfer', 28 'Content-Description' => 'File Transfer',
28 ], 29 ],
29 ]; 30 ];
30 - const TYPE = 1;  
31 -  
32 public $path = '';//路径 31 public $path = '';//路径
33 32
34 public $config = '';//存储默认配置 33 public $config = '';//存储默认配置
@@ -41,6 +40,7 @@ class ImageController extends Controller @@ -41,6 +40,7 @@ class ImageController extends Controller
41 40
42 public $cache = '';//缓存数据 41 public $cache = '';//缓存数据
43 42
  43 + public $upload_location = 1;
44 public $image_type = [ 44 public $image_type = [
45 1 => 'image_product', 45 1 => 'image_product',
46 2 => 'image_news', 46 2 => 'image_news',
@@ -127,7 +127,7 @@ class ImageController extends Controller @@ -127,7 +127,7 @@ class ImageController extends Controller
127 $this->setUrl(); 127 $this->setUrl();
128 if ($type == 'multi') { 128 if ($type == 'multi') {
129 return $this->multi($files); 129 return $this->multi($files);
130 - } else { 130 + }else{
131 return $this->single($files); 131 return $this->single($files);
132 } 132 }
133 } 133 }
@@ -166,8 +166,11 @@ class ImageController extends Controller @@ -166,8 +166,11 @@ class ImageController extends Controller
166 if ($rs === false) { 166 if ($rs === false) {
167 return $this->response('添加失败', Code::USER_ERROR); 167 return $this->response('添加失败', Code::USER_ERROR);
168 } 168 }
  169 + //同步数据到cos
  170 + if($this->upload_location == 1){
169 $cosService = new CosService(); 171 $cosService = new CosService();
170 $cosService->uploadFile($this->path,$fileName); 172 $cosService->uploadFile($this->path,$fileName);
  173 + }
171 return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]); 174 return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
172 } 175 }
173 176
@@ -226,6 +229,11 @@ class ImageController extends Controller @@ -226,6 +229,11 @@ class ImageController extends Controller
226 'refer'=>$this->param['refer'] ?? '', 229 'refer'=>$this->param['refer'] ?? '',
227 ]; 230 ];
228 $data[] = ['image'=>$hash]; 231 $data[] = ['image'=>$hash];
  232 + //同步数据到cos
  233 + if($this->upload_location == 1){
  234 + $cosService = new CosService();
  235 + $cosService->uploadFile($this->path,$fileName);
  236 + }
229 } 237 }
230 $imageModel->insert($save_data); 238 $imageModel->insert($save_data);
231 return $this->response('图片资源',Code::SUCCESS,$data); 239 return $this->response('图片资源',Code::SUCCESS,$data);
@@ -318,6 +326,11 @@ class ImageController extends Controller @@ -318,6 +326,11 @@ class ImageController extends Controller
318 if(isset($this->param['refer_type']) && $this->param['refer_type'] == 1){ 326 if(isset($this->param['refer_type']) && $this->param['refer_type'] == 1){
319 $this->path = $this->uploads['path_a'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m'); 327 $this->path = $this->uploads['path_a'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
320 }else{ 328 }else{
  329 + $projectModel = new Project();
  330 + $project_info = $projectModel->read(['id'=>$this->cache['project_id']],['upload_location']);
  331 + if($project_info['upload_location'] != 1){
  332 + $this->upload_location = 0;
  333 + }
321 $this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m'); 334 $this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
322 } 335 }
323 } 336 }
@@ -31,13 +31,35 @@ class CosService @@ -31,13 +31,35 @@ class CosService
31 ], 31 ],
32 ]); 32 ]);
33 $key = $path.'/'.$filename; 33 $key = $path.'/'.$filename;
34 - $response = $cosClient->putObject([ 34 + $cosClient->putObject([
35 'Bucket' => $cos['bucket'], 35 'Bucket' => $cos['bucket'],
36 'Key' => $key, 36 'Key' => $key,
37 'Body' => fopen(config('filesystems.disks.upload')['root'].$path.'/'.$filename, 'r'), 37 'Body' => fopen(config('filesystems.disks.upload')['root'].$path.'/'.$filename, 'r'),
38 ]); 38 ]);
39 - var_dump($response);  
40 - die();  
41 return $key; 39 return $key;
42 } 40 }
  41 +
  42 + /**
  43 + * @param $image_name
  44 + * @remark :获取图片访问链接
  45 + * @name :getImageUrl
  46 + * @author :lyh
  47 + * @method :post
  48 + * @time :2023/7/19 16:08
  49 + */
  50 + public function getImageUrl($image_name)
  51 + {
  52 + $cos = config('filesystems.disks.cos');
  53 + $cosClient = new Client([
  54 + 'region' => $cos['region'],
  55 + 'credentials' => [
  56 + 'secretId' => $cos['credentials']['secretId'],
  57 + 'secretKey' => $cos['credentials']['secretKey'],
  58 + ],
  59 + ]);
  60 + $imageUrl = $cosClient->getObjectUrl(config('app.cos_bucket'), basename($image_name));
  61 + return response()->json([
  62 + 'image_url' => $imageUrl,
  63 + ]);
  64 + }
43 } 65 }