作者 lyh

gx

@@ -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\Services\CosService;
9 use App\Services\TencentCosService; 10 use App\Services\TencentCosService;
10 use Illuminate\Http\Exceptions\HttpResponseException; 11 use Illuminate\Http\Exceptions\HttpResponseException;
11 use Illuminate\Http\JsonResponse; 12 use Illuminate\Http\JsonResponse;
@@ -139,7 +140,7 @@ class ImageController extends Controller @@ -139,7 +140,7 @@ class ImageController extends Controller
139 * @method :post 140 * @method :post
140 * @time :2023/6/17 16:30 141 * @time :2023/6/17 16:30
141 */ 142 */
142 - public function single($files){ 143 + public function single($files,$url){
143 $hash = hash_file('md5', $files->getPathname()); 144 $hash = hash_file('md5', $files->getPathname());
144 //查看文件是否存在 145 //查看文件是否存在
145 $imageModel = new ImageModel(); 146 $imageModel = new ImageModel();
@@ -165,6 +166,8 @@ class ImageController extends Controller @@ -165,6 +166,8 @@ class ImageController extends Controller
165 if ($rs === false) { 166 if ($rs === false) {
166 return $this->response('添加失败', Code::USER_ERROR); 167 return $this->response('添加失败', Code::USER_ERROR);
167 } 168 }
  169 + $cosService = new CosService();
  170 + $cosService->uploadFile($files,$this->path,$fileName);
168 return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]); 171 return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
169 } 172 }
170 173
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 2
3 namespace App\Services; 3 namespace App\Services;
4 4
  5 +use Qcloud\Cos\Client;
5 /** 6 /**
6 * @remark : 7 * @remark :
7 * @class :CosService.php 8 * @class :CosService.php
@@ -11,4 +12,30 @@ namespace App\Services; @@ -11,4 +12,30 @@ namespace App\Services;
11 class CosService 12 class CosService
12 { 13 {
13 14
  15 + /**
  16 + * @param $file
  17 + * @remark :上传图片
  18 + * @name :uploadFile
  19 + * @author :lyh
  20 + * @method :post
  21 + * @time :2023/7/19 15:28
  22 + */
  23 + public function uploadFile($file,$path,$filename)
  24 + {
  25 + $cos = config('filesystems.disks.cos');
  26 + $cosClient = new Client([
  27 + 'region' => $cos['region'],
  28 + 'credentials' => [
  29 + 'secretId' => $cos['credentials']['secretId'],
  30 + 'secretKey' => $cos['credentials']['secretKey'],
  31 + ],
  32 + ]);
  33 + $key = $path.'/'.$filename;
  34 + $cosClient->putObject([
  35 + 'Bucket' => $cos['bucket'],
  36 + 'Key' => $key,
  37 + 'Body' => fopen($file->getRealPath(), 'r'),
  38 + ]);
  39 + return $key;
  40 + }
14 } 41 }