mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 12:33:56 +00:00
85 lines
2.8 KiB
PHP
85 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Open1688\ControllersLogic;
|
|
|
|
use App\Classes\General\Abstracts\AbstractControllerLogic;
|
|
use App\Classes\General\Open1688Helper;
|
|
|
|
use ErrorException;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class GetProductDetailLogic extends AbstractControllerLogic
|
|
{
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function notification():array {
|
|
return [
|
|
'title' => 'Get product detail',
|
|
'message' => 'You have successfully get 1688 product detail'
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
* @throws ErrorException
|
|
*/
|
|
public function logic(Request $request) : JsonResponse
|
|
{
|
|
$result = Open1688Helper::productDetail($request->route('offer_id'));
|
|
$productAttribute = [];
|
|
$variants = [];
|
|
$uniqueVariants = [];
|
|
$variantInfo = [];
|
|
$priceList = [];
|
|
$variantKeyCombinationOrder = [];
|
|
|
|
foreach ($result['productAttribute'] as $attr) {
|
|
$productAttribute[$attr['attributeName']] = array_key_exists($attr['attributeName'], $productAttribute) ? $productAttribute[$attr['attributeName']] . ", " . $attr['value'] : $attr['value'];
|
|
}
|
|
|
|
$result['productAttribute'] = $productAttribute;
|
|
|
|
foreach ($result['productSkuInfos'] as $sku) {
|
|
$uniqueVariantKey = '';
|
|
foreach ($sku['skuAttributes'] as $skuAttr) {
|
|
if (count($variantKeyCombinationOrder) < count($sku['skuAttributes'])) {
|
|
array_push($variantKeyCombinationOrder, $skuAttr['attributeName']);
|
|
}
|
|
if (!in_array($skuAttr['value'], $uniqueVariants)) {
|
|
$variants[$skuAttr['attributeName']][] = [
|
|
'image' => isset($skuAttr['skuImageUrl']) ? $skuAttr['skuImageUrl'] : null,
|
|
'value' => $skuAttr['value']
|
|
];
|
|
}
|
|
|
|
array_push($uniqueVariants, $skuAttr['value']);
|
|
|
|
$uniqueVariantKey .= $skuAttr['value'];
|
|
}
|
|
|
|
$price = isset($sku['price']) ? $sku['price'] : $sku['consignPrice'];
|
|
|
|
$variantInfo[$uniqueVariantKey] = [
|
|
'skuId' => $sku['skuId'],
|
|
'amountOnSale' => $sku['amountOnSale'],
|
|
'price' => $price,
|
|
];
|
|
array_push($priceList, $price);
|
|
}
|
|
|
|
$result['variants'] = $variants;
|
|
$result['variantInfo'] = $variantInfo;
|
|
|
|
$minPrice = min($priceList);
|
|
$maxPrice = max($priceList);
|
|
|
|
$result['priceRange'] = $minPrice === $maxPrice ? $minPrice : "CNY ¥$minPrice - CNY ¥$maxPrice";
|
|
$result['variantKeyCombinationOrder'] = $variantKeyCombinationOrder;
|
|
|
|
return $this->response(['data' => $result]);
|
|
}
|
|
}
|