Commit 3662b769 authored by luhongguang's avatar luhongguang

update:图片兼容

parent 4f8e8782
......@@ -5,10 +5,12 @@ namespace App\Services\goods;
use Api\PhpServices\Idgen\Idgen;
use Api\PhpServices\Ksy\Ks3Api;
use Api\PhpServices\Ksy\Ksyun;
use Api\PhpServices\Sensitive\Sensitive;
use Api\PhpUtils\Common\BaseConvert;
use Api\PhpUtils\Common\GoodsSkuId;
use Api\PhpUtils\Http\HttpUtil;
use App\Exception\custom\GoodsException;
use App\Models\goods\mysql\Category;
use App\Models\goods\mysql\GoodsOperationRecord;
......@@ -966,7 +968,7 @@ class GoodsService
$data[] = $sku;
if (!empty($sku["desc_pic_url"])) {
$ksyun = self::getUrlList($sku["desc_pic_url"]);
$ksyun = self::getUrlList($sku["desc_pic_url"], 200, 200);
$data[$key]["desc_pic_url_list"] = $ksyun;
}
$lastId = $sku["goods_spu_id"];
......@@ -1292,20 +1294,31 @@ class GoodsService
/**
* 获取图片地址,这里因为bucket可能不同,不保证图片顺序
* @param $picUrlStr
* @param $widthSize
* @param $highSize
* @return array
*/
public static function getUrlList($picUrlStr)
public static function getUrlList($picUrlStr, $widthSize = 0, $highSize = 0)
{
$data = $ksyunParams = [];
$strList = explode(",", $picUrlStr);
foreach ($strList as $key => $str) {
$arr = explode("/", $str);
if (count($arr) != 2) {
if (count($arr) == 1) {
if (preg_match("/^http[s]{0,1}:\/\/([\w.]+\/?)\S*/", $str)) {
$data[] = $str;
} else {
if (!empty($widthSize) && !empty($highSize)) {
$urlRes = Ks3Api::picEncryptUrl($str, $widthSize, $highSize);
} else {
$urlRes = Ks3Api::picEncryptUrl($str);
}
if ($urlRes["response"]["data"]["url"]) {
$data[] = $urlRes["response"]["data"]["url"];
} else {
continue;
}
}
} else {
$bucket = $arr[0];
$objectId = $arr[1];
......
......@@ -17,7 +17,7 @@ idgen.partner = "bp"
idgen.key = "5cfdb867e96374c7883b31d6928cc4cb"
[exception]
debug = true
debug = false
exception.user.code = -1
exception.user.msg = "error"
exception.sys.code = -1
......
......@@ -52,21 +52,19 @@ class Ks3Api
/**
* 获取KS签名
* @param $bucket
* @param $objectKey
* @param $resource
* @param $httpMethod
* @param $contentType
* @param $contentMd5
* @param $headers
* @return array
*/
public static function signature($bucket, $objectKey, $httpMethod, $contentType, $contentMd5, $headers)
public static function signature($resource, $httpMethod, $contentType, $contentMd5, $headers)
{
$url = self::BASE_URL . self::PATH_SIGNATURE;
$timestamp = self::msectime();
$date = gmdate(self::KS_DATE_SCHEME);
$resource = "/" . $bucket . "/" . $objectKey;
$md5Str = $httpMethod . "&" . $contentType . "&" . $contentMd5 . "&" . $date . "&" . $resource . "&" . $headers . "&" . $timestamp . "&" . self::KSYUN_SK;
$signature = self::KSYUN_AK . ":" . md5($md5Str);
......@@ -93,13 +91,13 @@ class Ks3Api
* @param int $expirationTime
* @return mixed
*/
public static function picEncryptUrl($imageId, $widthSize, $highSize, $expirationTime = 60 * 60 * 24 * 365)
public static function picEncryptUrl($imageId, $widthSize = 0, $highSize = 0, $expirationTime = 60 * 60 * 24 * 365)
{
$url = self::BASE_GEN_URL . self::PATH_PIC_ENCRYPT_URL;
$timestamp = self::msectime();
$extend = [];
$extend["expiration_time"] = $expirationTime;
if ($widthSize && $highSize) {
if (!empty($widthSize) && !empty($highSize)) {
$extend["query"] = "type=thumbnail_" . $widthSize . "x" . $highSize;
}
$extendJsonStr = json_encode($extend);
......
......@@ -57,14 +57,13 @@ class HttpUtil
$ch = self::CH($url, $timeout, $headers, $post, $proxy, $method, $curl_opts);
$result = false; //返回结果
$result_code = self::CODE_FAIL; //错误代码
$result_http_code = 0; // http code
$error_msg = ''; // ch error
$response = self::defaultResponse();
if (is_resource($ch) === true)
{
//返回结果
$result = false;
while (($result === false) && ($retries-- > 0))
{
$result = curl_exec($ch);
......@@ -106,23 +105,13 @@ class HttpUtil
// 日志
self::addLog($method, $url, $post, $result);
if (curl_errno($ch) !== 0)
{
$error_msg = curl_error($ch);
}
$result_code = curl_errno($ch) === 0 ? self::CODE_SUCCESS : self::CODE_FAIL;
$result_http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 返回结果
$response = self::createResponse($result, $ch);
curl_close($ch);
}
return [
'code' => $result_code,
'response' => empty(json_decode($result, true)) ? $result : json_decode($result, true),
'http_code' => $result_http_code,
'msg' => $error_msg,
];
return $response;
}
/**
......@@ -197,16 +186,17 @@ class HttpUtil
{
$methods = [];
foreach ($urls as $k => $url) {
$methods[$k] = 'GET';
$methods[$k] = 'POST';
}
return self::MULTI_CURL($urls, $methods, $posts, $timeouts , $headers, $proxies, $curl_opts);
}
static function MULTI_GET($urls,$timeouts ,$headers = [], $proxies = [], $curl_opts = [])
static function MULTI_GET($urls =[],$params = [], $timeouts = [],$headers = [], $proxies = [], $curl_opts = [])
{
$methods = $posts = [];
foreach ($urls as $k => $url) {
$methods[$k] = 'POST';
$urls[$k] = $url ."?". http_build_query($params[$k]);
$methods[$k] = 'GET';
$posts[$k] = false;
}
......@@ -244,10 +234,23 @@ class HttpUtil
curl_multi_exec($mh, $active);
} while ($active);
$response = [];
//step four 获取结果,移除并close handle
foreach ($urls as $i => $url)
{
$res[$url]=curl_multi_getcontent($conn[$i]);
$response_content = curl_multi_getcontent($conn[$i]);
$response[$i] = self::createResponse($response_content, $conn[$i]);
// 监控
self::addMon(
$url,
curl_errno($conn[$i]) === 0 ? curl_getinfo($conn[$i], CURLINFO_HTTP_CODE) : -999,
round(curl_getinfo($conn[$i], CURLINFO_TOTAL_TIME),4) * 1000
);
// 日志
self::addLog($methods[$i], $url, $posts[$i], $response_content);
curl_multi_remove_handle($mh,$conn[$i]);
curl_close($conn[$i]);
}
......@@ -255,7 +258,7 @@ class HttpUtil
curl_multi_close($mh);
//step five 结果返回
return $res;
return $response;
}
/*
......@@ -288,6 +291,7 @@ class HttpUtil
}
}
if (!empty($log)) {
//
$params = self::change($params);
FileLog::info($method, json_encode([
'url' => $urls,
......@@ -315,4 +319,33 @@ class HttpUtil
}
return $arr;
}
/**
* @param string $response_content 请求返回结果
* @param resource $ch_handle cURL resource
* @return array
*/
private static function createResponse(string $response_content, $ch_handle): array
{
return [
'code' => curl_errno($ch_handle) === 0 ? self::CODE_SUCCESS : self::CODE_FAIL,
'response' => empty(json_decode($response_content, true)) ? $response_content : json_decode($response_content, true),
'http_code' => curl_getinfo($ch_handle, CURLINFO_HTTP_CODE),
'msg' => curl_error($ch_handle),
];
}
/**
* 默认返回
* @return array
*/
private static function defaultResponse(): array
{
return [
'code' => self::CODE_FAIL,
'response' => '',
'http_code' => 0,
'msg' => '',
];
}
}
......@@ -32,7 +32,7 @@ private static $installed = array (
'aliases' =>
array (
),
'reference' => '862a9fe5cf5c0f2a352166e06c8de2d39fc0288c',
'reference' => '4f8e8782a402f2ef95b6b5b9b21c23da94816828',
'name' => 'yidian/yaf_demo',
),
'versions' =>
......@@ -45,7 +45,7 @@ private static $installed = array (
array (
0 => '9999999-dev',
),
'reference' => 'c77ebfa6785a388d40c8b7cf5b9f46bbf309d748',
'reference' => 'f7b44c008d561d3785797073d0eb875fb2f16756',
),
'api/php_utils' =>
array (
......@@ -55,7 +55,7 @@ private static $installed = array (
array (
0 => '9999999-dev',
),
'reference' => '471681c72e3b7b220511c9944aab6ac6f7545106',
'reference' => 'e4bc1258fafead42eb6ac7b84ae743f08d557a99',
),
'bacon/bacon-qr-code' =>
array (
......@@ -335,7 +335,7 @@ private static $installed = array (
'aliases' =>
array (
),
'reference' => '862a9fe5cf5c0f2a352166e06c8de2d39fc0288c',
'reference' => '4f8e8782a402f2ef95b6b5b9b21c23da94816828',
),
),
);
......
......@@ -7,7 +7,7 @@
"source": {
"type": "git",
"url": "https://git.yidian-inc.com:8021/bp/php_services.git",
"reference": "c77ebfa6785a388d40c8b7cf5b9f46bbf309d748"
"reference": "f7b44c008d561d3785797073d0eb875fb2f16756"
},
"require": {
"api/php_utils": "dev-master",
......@@ -15,7 +15,7 @@
"perftools/php-profiler": "^0.18.0",
"php": "7.2.*"
},
"time": "2021-07-14T06:34:04+00:00",
"time": "2021-07-14T09:06:17+00:00",
"default-branch": true,
"type": "library",
"installation-source": "source",
......@@ -34,7 +34,7 @@
"source": {
"type": "git",
"url": "https://git.yidian-inc.com:8021/bp/php_utils.git",
"reference": "471681c72e3b7b220511c9944aab6ac6f7545106"
"reference": "e4bc1258fafead42eb6ac7b84ae743f08d557a99"
},
"require": {
"elasticsearch/elasticsearch": "~7.0",
......@@ -46,7 +46,7 @@
"mongodb/mongodb": "1.4.3",
"php": "7.2.*"
},
"time": "2021-07-13T14:46:05+00:00",
"time": "2021-07-14T08:30:41+00:00",
"default-branch": true,
"type": "library",
"installation-source": "source",
......
......@@ -6,7 +6,7 @@
'aliases' =>
array (
),
'reference' => '862a9fe5cf5c0f2a352166e06c8de2d39fc0288c',
'reference' => '4f8e8782a402f2ef95b6b5b9b21c23da94816828',
'name' => 'yidian/yaf_demo',
),
'versions' =>
......@@ -19,7 +19,7 @@
array (
0 => '9999999-dev',
),
'reference' => 'c77ebfa6785a388d40c8b7cf5b9f46bbf309d748',
'reference' => 'f7b44c008d561d3785797073d0eb875fb2f16756',
),
'api/php_utils' =>
array (
......@@ -29,7 +29,7 @@
array (
0 => '9999999-dev',
),
'reference' => '471681c72e3b7b220511c9944aab6ac6f7545106',
'reference' => 'e4bc1258fafead42eb6ac7b84ae743f08d557a99',
),
'bacon/bacon-qr-code' =>
array (
......@@ -309,7 +309,7 @@
'aliases' =>
array (
),
'reference' => '862a9fe5cf5c0f2a352166e06c8de2d39fc0288c',
'reference' => '4f8e8782a402f2ef95b6b5b9b21c23da94816828',
),
),
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment