作者 lyh

gx

@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 namespace App\Services; 3 namespace App\Services;
4 4
5 use Illuminate\Http\Request; 5 use Illuminate\Http\Request;
  6 +use Illuminate\Support\Str;
6 7
7 /** 8 /**
8 * @remark :腾讯cos上传 9 * @remark :腾讯cos上传
@@ -47,22 +48,63 @@ class TencentCosService extends BaseService @@ -47,22 +48,63 @@ class TencentCosService extends BaseService
47 $httpVerb = 'PUT'; 48 $httpVerb = 'PUT';
48 $date = gmdate('D, d M Y H:i:s \G\M\T'); 49 $date = gmdate('D, d M Y H:i:s \G\M\T');
49 //生成 KeyTime 50 //生成 KeyTime
50 - $signTime = ( string )( time() - 60 ) . ';' . ( string )( strtotime( '+30 minutes' ) ); 51 + $keyTime = ( string )( time() - 60 ) . ';' . ( string )( strtotime( '+30 minutes' ) );
51 //生成 SignKey 52 //生成 SignKey
52 - $signKey = hash_hmac( 'sha1', $signTime, trim($this->config['secretKey']) ); 53 + $signKey = hash_hmac( 'sha1', $keyTime, trim($this->config['secretKey']) );
53 //生成 UrlParamList 和 HttpParameters 54 //生成 UrlParamList 和 HttpParameters
54 - $filePath = $path.'/'.$fileName;  
55 - $stringToSign = "{$httpVerb}\n\n\n{$date}\n/{$this->config['bucket']}/{$filePath}"; 55 + $queryParameters = \request()->query();
  56 + $sortedKeys = collect($queryParameters)->keys()->sort()->all();
  57 + $urlParamList = "";
  58 + $httpParameters = "";
  59 + foreach ($sortedKeys as $key) {
  60 + $encodedKey = Str::lower(rawurlencode($key));
  61 + $value = isset($queryParameters[$key]) ? rawurlencode($queryParameters[$key]) : "";
  62 + $urlParamList .= $encodedKey . ";";
  63 + $httpParameters .= $encodedKey . "=" . $value . "&";
  64 + }
  65 + $urlParamList = rtrim($urlParamList, ";");
  66 + $httpParameters = rtrim($httpParameters, "&");
  67 + // 生成 HeaderList 和 HttpHeaders
  68 + $headers = \request()->headers->all();
  69 + $sortedKeys = collect($headers)->keys()->sort()->all();
  70 + $headerList = "";
  71 + $httpHeaders = "";
  72 + foreach ($sortedKeys as $key) {
  73 + $encodedKey = Str::lower(rawurlencode($key));
  74 + $value = rawurlencode($headers[$key][0]);
  75 + $headerList .= $encodedKey . ";";
  76 + $httpHeaders .= $encodedKey . "=" . $value . "&";
  77 + }
  78 + $headerList = rtrim($headerList, ";");
  79 + $httpHeaders = rtrim($httpHeaders, "&");
  80 + //生成HttpString
  81 + $httpMethod = Str::lower($httpVerb);
  82 + $uriPathname = $path.'/'.$fileName;
  83 + $httpString = $httpMethod . "\n" . $uriPathname . "\n" . $httpParameters . "\n" . $httpHeaders . "\n";
  84 + //生成 StringToSign
  85 + $sha1String = sha1($httpString);
  86 + $stringToSign = "sha1\n" . $keyTime . "\n" . $sha1String . "\n";
56 $sign = hash_hmac( 'sha1', $stringToSign, $signKey ); 87 $sign = hash_hmac( 'sha1', $stringToSign, $signKey );
57 $authorization = 88 $authorization =
58 - "q-sign-algorithm=sha1&q-ak={$this->config['secretId']}&q-sign-time={$signTime}&q-key-time={$signTime}&q-header-list=&q-url-param-list=&q-signature={$sign}";  
59 - $url = "https://{$this->config['bucket']}.cos.{$this->config['cosRegion']}.myqcloud.com{$filePath}"; 89 + "q-sign-algorithm=sha1&q-ak={$this->config['secretId']}&q-sign-time={$keyTime}&q-key-time={$keyTime}&q-header-list=&q-url-param-list=&q-signature={$sign}";
  90 + $url = "https://{$this->config['bucket']}.cos.{$this->config['cosRegion']}.myqcloud.com{$uriPathname}";
60 // 打开文件流 91 // 打开文件流
61 $filePathUrl = config('filesystems.disks.upload')['root'].$path.'/'.$fileName; 92 $filePathUrl = config('filesystems.disks.upload')['root'].$path.'/'.$fileName;
62 return $this->http_put($url,$contentType,$authorization,$date,$filePathUrl); 93 return $this->http_put($url,$contentType,$authorization,$date,$filePathUrl);
63 } 94 }
64 95
65 /** 96 /**
  97 + * @param Request $request
  98 + * @remark :生成$urlParamList + $httpParameters
  99 + * @name :ceshi
  100 + * @author :lyh
  101 + * @method :post
  102 + * @time :2023/7/19 14:05
  103 + */
  104 + public function queryParameters(Request $request){
  105 +
  106 + }
  107 + /**
66 * @remark :上传文件到第三方 108 * @remark :上传文件到第三方
67 * @name : (http_put 109 * @name : (http_put
68 * @author :lyh 110 * @author :lyh
@@ -86,8 +128,6 @@ class TencentCosService extends BaseService @@ -86,8 +128,6 @@ class TencentCosService extends BaseService
86 curl_close($ch); 128 curl_close($ch);
87 // 检查响应结果 129 // 检查响应结果
88 if ($response) { 130 if ($response) {
89 - var_dump($response);  
90 - die();  
91 return 1; 131 return 1;
92 } else { 132 } else {
93 return 0; 133 return 0;