作者 lyh

gx

@@ -165,7 +165,7 @@ class ImageController extends Controller @@ -165,7 +165,7 @@ class ImageController extends Controller
165 if ($rs === false) { 165 if ($rs === false) {
166 return $this->response('添加失败', Code::USER_ERROR); 166 return $this->response('添加失败', Code::USER_ERROR);
167 } 167 }
168 - return $this->uploadCos($this->path,$fileName); 168 + return $this->uploadCos($this->path,$fileName,$data['type']);
169 return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]); 169 return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
170 } 170 }
171 171
@@ -327,8 +327,8 @@ class ImageController extends Controller @@ -327,8 +327,8 @@ class ImageController extends Controller
327 * @method :post 327 * @method :post
328 * @time :2023/7/18 17:38 328 * @time :2023/7/18 17:38
329 */ 329 */
330 - public function uploadCos($path,$fileName){ 330 + public function uploadCos($path,$fileName,$image_type){
331 $txCos = new TencentCosService(); 331 $txCos = new TencentCosService();
332 - return $txCos->upload_image($path,$fileName); 332 + return $txCos->upload_image($path,$fileName,$image_type);
333 } 333 }
334 } 334 }
@@ -40,22 +40,21 @@ class TencentCosService extends BaseService @@ -40,22 +40,21 @@ class TencentCosService extends BaseService
40 * @method :post 40 * @method :post
41 * @time :2023/7/18 16:56 41 * @time :2023/7/18 16:56
42 */ 42 */
43 - public function upload_image($path,$fileName){ 43 + public function upload_image($path,$fileName,$image_type){
44 // 构建请求URL 44 // 构建请求URL
45 - $pathname = '/'.$this->config['bucket'].$path.'/'.$fileName;  
46 - $url = 'https://'.$this->config['bucket'].'.cos.'.$this->config['cosRegion'].'.myqcloud.com'.$pathname;  
47 - $date = gmdate('D, d M Y H:i:s T');  
48 - $signKey = hash_hmac('sha1', $date, $this->config['secretKey']);  
49 - $method = 'PUT';  
50 - $signature = base64_encode(hash_hmac('sha1', "{$method}\n\n\n\n", $signKey, true));  
51 - $headers = array(  
52 - 'Authorization: QCloud ' . $this->config['secretId'] . ':' . $signature,  
53 - 'Date: ' . $date,  
54 - 'Host: ' . $this->config['bucket'] . '.cos.' . $this->config['cosRegion'] . '.myqcloud.com'  
55 - ); 45 + $httpVerb = 'PUT';
  46 + $contentMd5 = '';
  47 + $contentType = 'image/'.$image_type;
  48 + $date = gmdate('D, d M Y H:i:s \G\M\T');
  49 + $filePath = $path.'/'.$fileName;
  50 + $expiredTime = time() + 3600;
  51 + $stringToSign = "{$httpVerb}\n{$contentMd5}\n{$contentType}\n{$date}\n/{$this->config['bucket']}/{$filePath}";
  52 + $sign = base64_encode(hash_hmac('sha1', $stringToSign, $this->config['secretKey'], true));
  53 + $authorization = "q-sign-algorithm=sha1&q-ak={$this->config['secretId']}&q-sign-time={$expiredTime}&q-key-time={$expiredTime}&q-header-list=&q-url-param-list=&q-signature={$sign}";
  54 + $url = "https://{$this->config['bucket']}.cos.{$this->config['cosRegion']}.myqcloud.com{$filePath}";
56 // 打开文件流 55 // 打开文件流
57 - $filePath = config('filesystems.disks.upload')['root'].$path.'/'.$fileName;  
58 - return $this->http_put($url,$filePath,$headers); 56 + $filePathUrl = config('filesystems.disks.upload')['root'].$path.'/'.$fileName;
  57 + return $this->http_put($url,$contentType,$authorization,$date,$filePathUrl);
59 } 58 }
60 59
61 /** 60 /**
@@ -65,24 +64,28 @@ class TencentCosService extends BaseService @@ -65,24 +64,28 @@ class TencentCosService extends BaseService
65 * @method :post 64 * @method :post
66 * @time :2023/7/18 17:06 65 * @time :2023/7/18 17:06
67 */ 66 */
68 - public function http_put($url,$filePath,$headers){ 67 + public function http_put($url,$contentType,$authorization,$date,$filePath){
69 // 执行请求 68 // 执行请求
70 - $ch = curl_init();  
71 - curl_setopt($ch, CURLOPT_URL, $url); 69 + $ch = curl_init($url);
72 curl_setopt($ch, CURLOPT_PUT, true); 70 curl_setopt($ch, CURLOPT_PUT, true);
73 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 71 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
74 - curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);  
75 - curl_setopt($ch, CURLOPT_INFILE, fopen($filePath, 'rb')); 72 + curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  73 + 'Content-Type: ' . $contentType,
  74 + 'Authorization: ' . $authorization,
  75 + 'Date: ' . $date
  76 + ));
  77 + curl_setopt($ch, CURLOPT_INFILE, fopen($filePath, 'r'));
76 curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath)); 78 curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath));
77 - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 79 +
78 $response = curl_exec($ch); 80 $response = curl_exec($ch);
79 curl_close($ch); 81 curl_close($ch);
80 // 检查响应结果 82 // 检查响应结果
81 if ($response) { 83 if ($response) {
82 var_dump($response); 84 var_dump($response);
83 - echo '图片上传成功'; 85 + die();
  86 + return 1;
84 } else { 87 } else {
85 - echo '图片上传失败'; 88 + return 0;
86 } 89 }
87 } 90 }
88 91