|
|
@@ -1,6 +1,7 @@
|
|
|
<?php
|
|
|
namespace common\components\delivery\services\adapter;
|
|
|
|
|
|
+use Yii;
|
|
|
use common\components\delivery\helpers\HttpClient;
|
|
|
use common\components\delivery\platform\huolala\Huolala;
|
|
|
|
|
|
@@ -12,6 +13,8 @@ use common\components\delivery\platform\huolala\Huolala;
|
|
|
*/
|
|
|
class HuolalaAdapter extends Huolala implements Adapter
|
|
|
{
|
|
|
+ private const CITY_VEHICLE_CACHE_TTL = 259200; // 3 days
|
|
|
+ private const CITY_VEHICLE_CACHE_KEY_PATTERN = 'huolala-city-vehicle:%d';
|
|
|
/**
|
|
|
* 获取已开通城市列表
|
|
|
*
|
|
|
@@ -34,19 +37,36 @@ class HuolalaAdapter extends Huolala implements Adapter
|
|
|
* 通过此接口可获取城市可选车型信息,包括车型详情、计价规则、车型附加要求和额外需求等
|
|
|
*
|
|
|
* @param int $cityId 城市ID
|
|
|
+ * @param bool $forceRefresh 是否强制刷新缓存
|
|
|
* @return array|null 包含城市车型信息的响应数据
|
|
|
*/
|
|
|
- public function getCityVehicleList($cityId)
|
|
|
+ public function getCityVehicleList($cityId, $forceRefresh = false)
|
|
|
{
|
|
|
// 参数验证
|
|
|
if (empty($cityId) || !is_numeric($cityId)) {
|
|
|
return null;
|
|
|
}
|
|
|
+ $cityId = (int)$cityId;
|
|
|
|
|
|
- // 构建请求参数
|
|
|
- $payload = $this->buildRequestPayload('u-city-info', ['city_id' => (int)$cityId]);
|
|
|
-
|
|
|
- // 发送请求
|
|
|
+ $cacheKey = sprintf(self::CITY_VEHICLE_CACHE_KEY_PATTERN, $cityId);
|
|
|
+ $redis = Yii::$app->has('redis') ? Yii::$app->redis : null;
|
|
|
+
|
|
|
+ if ($redis !== null) {
|
|
|
+ if ($forceRefresh) {
|
|
|
+ $redis->executeCommand('DEL', [$cacheKey]);
|
|
|
+ } else {
|
|
|
+ $cached = $redis->executeCommand('GET', [$cacheKey]);
|
|
|
+ if ($cached !== false && $cached !== null) {
|
|
|
+ $decoded = json_decode($cached, true);
|
|
|
+ if (is_array($decoded)) {
|
|
|
+ return $decoded;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建请求参数,发送请求
|
|
|
+ $payload = $this->buildRequestPayload('u-city-info', ['city_id' => $cityId]);
|
|
|
$resp = HttpClient::post($this->baseUrl, $payload);
|
|
|
|
|
|
// 处理响应
|
|
|
@@ -63,9 +83,9 @@ class HuolalaAdapter extends Huolala implements Adapter
|
|
|
&& is_array($vehicle['price_text_item']['exceed_segment_price'])) {
|
|
|
foreach ($vehicle['price_text_item']['exceed_segment_price'] as $segment) {
|
|
|
$exceedSegmentPrice[] = [
|
|
|
- 'start_exdistancekm' => (int)($segment['start_exdistancekm'] ?? 0),
|
|
|
- 'end_exdistancekm' => (int)($segment['end_exdistancekm'] ?? 0),
|
|
|
- 'price_extra_fen' => (int)($segment['price_extra_fen'] ?? 0),
|
|
|
+ 'start_exdistancekm' => $segment['start_exdistancekm'] ?? 0,
|
|
|
+ 'end_exdistancekm' => $segment['end_exdistancekm'] ?? 0,
|
|
|
+ 'price_extra_fen' => $segment['price_extra_fen'] ?? 0,
|
|
|
];
|
|
|
}
|
|
|
}
|
|
|
@@ -77,24 +97,24 @@ class HuolalaAdapter extends Huolala implements Adapter
|
|
|
$vehicleStdItem[] = [
|
|
|
'name' => $std['name'] ?? '',
|
|
|
'desc' => $std['desc'] ?? '',
|
|
|
- 'price_fen' => (int)($std['price_fen'] ?? 0),
|
|
|
+ 'price_fen' => $std['price_fen'] ?? 0,
|
|
|
];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
$vehicleList[] = [
|
|
|
- 'order_vehicle_id' => (int)($vehicle['order_vehicle_id'] ?? 0),
|
|
|
+ 'order_vehicle_id' => $vehicle['order_vehicle_id'] ?? 0,
|
|
|
'vehicle_name' => $vehicle['vehicle_name'] ?? '',
|
|
|
'img_url_high_light' => $vehicle['img_url_high_light'] ?? '',
|
|
|
'vehicle_volume' => $vehicle['vehicle_volume'] ?? '',
|
|
|
'vehicle_weight' => $vehicle['vehicle_weight'] ?? '',
|
|
|
'vehicle_size' => $vehicle['vehicle_size'] ?? '',
|
|
|
- 'standard_order_vehicle_id' => (int)($vehicle['standard_order_vehicle_id'] ?? 0),
|
|
|
+ 'standard_order_vehicle_id' => $vehicle['standard_order_vehicle_id'] ?? 0,
|
|
|
'text_desc' => $vehicle['text_desc'] ?? '',
|
|
|
- 'vehicle_attr' => (int)($vehicle['vehicle_attr'] ?? 0),
|
|
|
+ 'vehicle_attr' => $vehicle['vehicle_attr'] ?? 0,
|
|
|
'price_text_item' => [
|
|
|
- 'base_distancekm' => (int)($vehicle['price_text_item']['base_distancekm'] ?? 0),
|
|
|
- 'base_price_fen' => (int)($vehicle['price_text_item']['base_price_fen'] ?? 0),
|
|
|
+ 'base_distancekm' => $vehicle['price_text_item']['base_distancekm'] ?? 0,
|
|
|
+ 'base_price_fen' => $vehicle['price_text_item']['base_price_fen'] ?? 0,
|
|
|
'exceed_segment_price' => $exceedSegmentPrice,
|
|
|
],
|
|
|
'vehicle_std_item' => $vehicleStdItem,
|
|
|
@@ -116,14 +136,24 @@ class HuolalaAdapter extends Huolala implements Adapter
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return [
|
|
|
- 'city_id' => (int)($data['city_id'] ?? 0),
|
|
|
- 'name' => $data['name'] ?? '',
|
|
|
- 'name_en' => $data['name_en'] ?? '',
|
|
|
- 'city_info_revision' => (int)($data['city_info_revision'] ?? 0),
|
|
|
+ $result = [
|
|
|
+ 'city_id' => $data['city_id'],
|
|
|
+ 'name' => $data['name'],
|
|
|
+ 'name_en' => $data['name_en'],
|
|
|
+ 'city_info_revision' => $data['city_info_revision'],
|
|
|
'vehicle_list' => $vehicleList,
|
|
|
'spec_req_item' => $specReqItem,
|
|
|
];
|
|
|
+
|
|
|
+ if ($redis !== null) {
|
|
|
+ $redis->executeCommand('SETEX', [
|
|
|
+ $cacheKey,
|
|
|
+ self::CITY_VEHICLE_CACHE_TTL,
|
|
|
+ json_encode($result, JSON_UNESCAPED_UNICODE)
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
}
|
|
|
|
|
|
return null;
|