DispatchService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. <?php
  2. namespace common\components\delivery\services;
  3. use biz\shop\classes\ShopClass;
  4. use bizGhs\express\classes\DeliveryAuthTokenClass;
  5. use bizGhs\order\classes\OrderItemClass;
  6. use common\components\delivery\helpers\HttpClient;
  7. use common\components\delivery\services\adapter\{ ShansongAdapter, HuolalaAdapter, FengniaoAdapter, ShunfengAdapter}; // MeituanAdapter, DadaAdapter, UUAdapter,
  8. use Yii;
  9. /**
  10. * 聚合调度逻辑(平台选择/优先级)
  11. * Class DispatchService
  12. * @package App\Services
  13. */
  14. class DispatchService
  15. {
  16. protected $adapters;
  17. protected $platformName = '';
  18. public function __construct($mainId, $platform='')
  19. {
  20. if ($platform == '') {
  21. $authPlatforms = DeliveryAuthTokenClass::getAllByCondition(['mainId'=>$mainId]);
  22. foreach($authPlatforms as $pt) {
  23. switch ($pt['platform']) {
  24. case 'shansong':
  25. $this->adapters['shansong'] = new ShansongAdapter($pt['accessToken']);
  26. break;
  27. case 'huolala':
  28. $this->adapters['huolala'] = new HuolalaAdapter($pt['accessToken']);
  29. break;
  30. case 'fengniao':
  31. $adapter = new FengniaoAdapter($pt['accessToken']);
  32. $adapter->setMerchantId(14594092); // TODO: 确认是否需要使用动态的商户ID
  33. $this->adapters['fengniao'] = $adapter;
  34. break;
  35. case 'shunfeng':
  36. $this->adapters['shunfeng'] = new ShunfengAdapter($pt['accessToken'], $pt['shopId']);
  37. break;
  38. }
  39. //检测 token 是否即将过期
  40. $tokenExpireTime = $pt['expiresAt'];
  41. if ($tokenExpireTime < time() + 86400 * 5) {
  42. Yii::info('刷新 token: ' . $pt['platform'] . ',refreshToken: ' . $pt['refreshToken'] . ',merchantId: ' . $pt['merchantId']);
  43. $this->refreshToken($pt['platform'], $pt['refreshToken'], ['merchantId' => $pt['merchantId']]);
  44. }
  45. }
  46. } else {
  47. $authPlatform = DeliveryAuthTokenClass::getByCondition(['mainId'=>$mainId, 'platform'=>$platform]);
  48. switch ($platform) {
  49. case 'shansong':
  50. $this->adapters['shansong'] = new ShansongAdapter($authPlatform['accessToken']);
  51. break;
  52. case 'huolala':
  53. $this->adapters['huolala'] = new HuolalaAdapter($authPlatform['accessToken']);
  54. break;
  55. case 'fengniao':
  56. //$this->adapters['fengniao'] = new FengniaoAdapter($authPlatform['accessToken']);
  57. $adapter = new FengniaoAdapter($authPlatform['accessToken']);
  58. $adapter->setMerchantId(14594092); // TODO: 确认是否需要使用动态的商户ID
  59. $this->adapters['fengniao'] = $adapter;
  60. break;
  61. case 'shunfeng':
  62. $this->adapters['shunfeng'] = new ShunfengAdapter($authPlatform['accessToken'], $authPlatform['shopId']);
  63. break;
  64. }
  65. $this->platformName = $platform;
  66. if($authPlatform['expiresAt'] < time() + 86400 * 5) {
  67. Yii::info('刷新 token: ' . $platform . ',refreshToken: ' . $authPlatform['refreshToken'] . ',merchantId: ' . $authPlatform['merchantId']);
  68. $this->refreshToken($platform, $authPlatform['refreshToken'], ['merchantId' => $authPlatform['merchantId']]);
  69. }
  70. }
  71. }
  72. /**
  73. * 发单调度
  74. */
  75. public function createOrder($order, $shopId, $params)
  76. {
  77. $order = $order->toArray();
  78. $shop = ShopClass::getById($shopId, false, 'id,merchantName,fullAddress,lat,long,city,dist,floor,mobile,merchantName');
  79. $adapter = $this->getAdapter();
  80. $orderData = $adapter->formatOrderData($order, $shop, $params);
  81. return $adapter->createOrder($orderData);
  82. }
  83. /**
  84. * @param $orderId 平台订单号
  85. * @param string $thirdOrderNo xhGhsOrder.orderSn
  86. */
  87. public function selectOrder($orderId, $thirdOrderNo='')
  88. {
  89. $adapter = $this->getAdapter();
  90. if ($adapter instanceof ShansongAdapter) {
  91. return $adapter->selectOrder($orderId, $thirdOrderNo);
  92. }
  93. return $adapter->selectOrder($orderId);
  94. }
  95. public function cancelOrder($orderId, $reason)
  96. {
  97. $adapter = $this->getAdapter();
  98. return $adapter->cancelOrder($orderId, $reason);
  99. }
  100. public function getCancelReasonList($orderId)
  101. {
  102. $adapter = $this->getAdapter();
  103. return $adapter->getCancelReasonList($orderId);
  104. }
  105. public function getAdapter()
  106. {
  107. return $this->adapters[$this->platformName];
  108. }
  109. /**
  110. * 获取所有平台的最佳报价(使用 Guzzle 并发请求)
  111. *
  112. * 流程说明:
  113. * 1. 同步准备各平台的前置数据(如城市信息、订单商品等)
  114. * 2. 使用 Guzzle Pool 并发发送各平台的报价请求(5秒超时)
  115. * 3. 某个平台失败/超时不影响其他平台,继续等待结果
  116. *
  117. * @param array $order 订单信息
  118. * @param array $shop 店铺信息
  119. * @return array 返回所有平台的报价结果
  120. */
  121. public function getAllPlatformPrice($order, $shop, $orderTime)
  122. {
  123. // 第一步:准备前置数据(同步进行,因为某些平台需要这些数据)
  124. $preparedData = $this->preparePlatformData($order, $shop, $orderTime);
  125. if (empty($preparedData)) {
  126. return ['error' => '所有平台数据准备失败'];
  127. }
  128. // 第二步:并发调用各平台的报价接口
  129. $results = $this->concurrentGetPrices($preparedData, $orderTime);
  130. if (empty($results['success']) && empty($results['failed'])) {
  131. return ['error' => '没有可用的物流平台'];
  132. }
  133. // 格式化返回结果
  134. return [
  135. 'quotes' => array_values($results['success'] ?? []),
  136. 'failed' => $results['failed'] ?? [],
  137. ];
  138. }
  139. /**
  140. * 为各平台准备订单数据(前置数据同步获取)
  141. *
  142. * @param array $order 订单信息
  143. * @param array $shop 店铺信息
  144. * @return array 各平台的订单数据
  145. */
  146. private function preparePlatformData($order, $shop)
  147. {
  148. $preparedData = [];
  149. // 准备 Huolala 数据
  150. if (isset($this->adapters['huolala'])) {
  151. try {
  152. $preparedData['huolala'] = $this->prepareHuolalaData($order, $shop);
  153. } catch (\Exception $e) {
  154. Yii::warning("Huolala 数据准备失败: {$e->getMessage()}");
  155. }
  156. }
  157. // 准备 Fengniao 数据
  158. if (isset($this->adapters['fengniao'])) {
  159. try {
  160. $preparedData['fengniao'] = $this->prepareFengniaoData($order, $shop);
  161. } catch (\Exception $e) {
  162. Yii::warning("Fengniao 数据准备失败: {$e->getMessage()}");
  163. }
  164. }
  165. // 准备 Shansong 数据
  166. if (isset($this->adapters['shansong'])) {
  167. try {
  168. $preparedData['shansong'] = $this->prepareShansongData($order, $shop);
  169. } catch (\Exception $e) {
  170. Yii::warning("Shansong 数据准备失败: {$e->getMessage()}");
  171. }
  172. }
  173. // 准备顺丰订单数据
  174. if (isset($this->adapters['shunfeng'])) {
  175. try {
  176. $preparedData['shunfeng'] = $this->prepareShunfengData($order, $shop);
  177. } catch (\Exception $e) {
  178. Yii::warning("Shunfeng 数据准备失败: {$e->getMessage()}");
  179. }
  180. }
  181. return $preparedData;
  182. }
  183. /**
  184. * 准备货拉拉订单数据
  185. */
  186. private function prepareHuolalaData($order, $shop)
  187. {
  188. $formatCity = rtrim($order['city'], '市');
  189. $cities = include Yii::getAlias('@common/components/delivery/platform/huolala/cities.php');
  190. if (!isset($cities[$formatCity])) {
  191. throw new \Exception('城市编码表中没有找到城市: ' . $formatCity);
  192. }
  193. $cityId = $cities[$formatCity]['city_id'];
  194. $cityVehicleList = $this->adapters['huolala']->getCityVehicleList($cityId); // 产生外部请求
  195. $cityInfoRevision = $cityVehicleList['city_info_revision']; // 城市版本号
  196. $vehicleList = $cityVehicleList['vehicle_list']; // 所有车型列表
  197. if (empty($vehicleList)) {
  198. throw new \Exception('没有找到可选车型');
  199. }
  200. // 解析额外需求
  201. $specReqItem = $cityVehicleList['spec_req_item'] ?? [];
  202. $specReq = [];
  203. foreach ($specReqItem as $item) {
  204. $specReq[] = $item['type'] ?? null;
  205. }
  206. $specReq = array_filter($specReq);
  207. // 构建所有车型数据
  208. $vehicleTypeList = [];
  209. foreach ($vehicleList as $vehicle) {
  210. if(!in_array($vehicle['vehicle_name'], ['跑腿', '小面', '微面'])){
  211. continue;
  212. }
  213. $vehicleStd = [];
  214. $vehicleStd[] = count($vehicle['vehicle_std_item']) > 0 ? $vehicle['vehicle_std_item'][0]['name'] : '';
  215. $vehicleTypeList[$vehicle['order_vehicle_id']] = [
  216. 'order_vehicle_id' => $vehicle['order_vehicle_id'],
  217. 'city_info_revision' => $cityInfoRevision,
  218. 'order_time' => time() + 600,
  219. 'addr_info' => [
  220. [
  221. 'name' => $shop['merchantName'],
  222. 'addr' => $shop['province'] . $shop['city'] . $shop['dist'] . $shop['address'],
  223. 'city_id' => $cityId,
  224. 'city_name' => $shop['city'],
  225. 'district_name' => $shop['dist'],
  226. 'house_number' => $shop['floor'],
  227. 'contacts_name' => $shop['mobile'],
  228. 'contacts_phone_no' => $shop['mobile'],
  229. 'lat_lon' => ['lat' => (float)$shop['lat'], 'lon' => (float)$shop['long']],
  230. ],
  231. [
  232. 'name' => $order['customName'],
  233. 'addr' => $order['fullAddress'],
  234. 'city_id' => $cityId,
  235. 'city_name' => $order['city'],
  236. 'district_name' => $order['dist'],
  237. 'house_number' => $order['floor'],
  238. 'contacts_name' => $order['customName'],
  239. 'contacts_phone_no' => $order['customMobile'],
  240. 'lat_lon' => ['lat' => (float)$order['lat'], 'lon' => (float)$order['long']],
  241. ]
  242. ],
  243. 'vehicle_std' => $vehicleStd,
  244. 'spec_req' => array_values($specReq),
  245. 'coupon_id' => 123456, // TODO ???
  246. 'invoice_type' => 1,
  247. 'order_service_type' => 1,
  248. '_meta' => [
  249. 'vehicle_type' => $vehicle['vehicle_name'],
  250. 'city_info_revision' => $cityInfoRevision,
  251. ]
  252. ];
  253. }
  254. return [
  255. 'platform' => 'huolala',
  256. 'city_id' => $cityId,
  257. 'vehicle_type_list' => $vehicleTypeList,
  258. ];
  259. }
  260. /**
  261. * 准备蜂鸟订单数据
  262. */
  263. private function prepareFengniaoData($order, $shop)
  264. {
  265. $this->adapters['fengniao']->setMerchantId(14594092); // TODO: 确认是否需要使用动态的商户ID
  266. $itemInfos = OrderItemClass::getAllByCondition(['orderSn' => $order['orderSn']], null, 'id, name, unitPrice, num');
  267. $goodsItemList = [];
  268. foreach ($itemInfos as $item) {
  269. $goodsItemList[] = [
  270. 'item_actual_amount_cent' => (int)($item['unitPrice'] * 100 * $item['num']),
  271. 'item_amount_cent' => (int)($item['unitPrice'] * 100),
  272. 'item_id' => $item['id'],
  273. 'item_name' => $item['name'],
  274. 'item_quantity' => $item['num'],
  275. ];
  276. }
  277. return [
  278. 'platform' => 'fengniao',
  279. 'partner_order_code' => $order['orderSn'],
  280. 'receiver_primary_phone' => $order['customMobile'],
  281. 'receiver_name' => $order['customName'],
  282. 'receiver_latitude' => (float)$order['lat'],
  283. 'receiver_longitude' => (float)$order['long'],
  284. 'receiver_address' => $order['fullAddress'],
  285. 'position_source' => 3,
  286. 'goods_count' => count($goodsItemList),
  287. 'goods_weight' => (float)$order['weight'],
  288. 'goods_total_amount_cent' => (int)($order['prePrice'] * 100),
  289. 'goods_actual_amount_cent' => (int)($order['actPrice'] * 100),
  290. 'goods_item_list' => $goodsItemList,
  291. 'order_type' => 1,
  292. 'chain_store_id' => 467788524,
  293. 'order_remark' => $order['remark'] ?? '',
  294. ];
  295. }
  296. /**
  297. * 准备闪送订单数据
  298. */
  299. private function prepareShansongData($order, $shop)
  300. {
  301. return [
  302. 'platform' => 'shansong',
  303. 'city_name' => $shop['city'],
  304. 'sender' => [
  305. 'from_address' => $shop['address'],
  306. 'from_address_detail' => $shop['floor'],
  307. 'from_sender_name' => $shop['shopName'],
  308. 'from_mobile' => $shop['mobile'],
  309. 'from_latitude' => (float)$shop['lat'],
  310. 'from_longitude' => (float)$shop['long'],
  311. ],
  312. 'receiver_list' => [
  313. [
  314. 'order_no' => $order['orderSn'],
  315. 'to_address' => $order['address'],
  316. 'to_address_detail' => $order['floor'],
  317. 'to_receiver_name' => $order['customName'],
  318. 'to_mobile' => $order['customMobile'],
  319. 'to_latitude' => (float)$order['lat'],
  320. 'to_longitude' => (float)$order['long'],
  321. 'good_type' => 7,
  322. 'weight' => (int)$order['weight'],
  323. 'remarks' => $order['remark'] ?? '',
  324. ]
  325. ],
  326. 'appoint_type' => 0,
  327. 'appointment_date' => '',
  328. 'travel_way' => 0,
  329. 'delivery_type' => 1,
  330. 'expect_start_time' => null,
  331. 'expect_end_time' => null,
  332. ];
  333. }
  334. /**
  335. * 准备顺丰订单数据
  336. */
  337. private function prepareShunfengData($order, $shop)
  338. {
  339. // 获取订单商品信息
  340. $itemInfos = OrderItemClass::getAllByCondition(['orderSn' => $order['orderSn']], null, 'id, name, unitPrice, num, unitWeight');
  341. // 计算商品总重量(单位:克)
  342. $totalWeight = 0;
  343. foreach ($itemInfos as $item) {
  344. $totalWeight += (int)($item['unitWeight'] * $item['num']) * 1000;
  345. }
  346. // 如果没有商品重量,使用订单重量
  347. if ($totalWeight == 0 && isset($order['weight'])) {
  348. $totalWeight = (int)$order['weight'] * 1000;
  349. }
  350. // 默认重量为1000克(1公斤)
  351. if ($totalWeight == 0) {
  352. $totalWeight = 1000;
  353. }
  354. return [
  355. 'platform' => 'shunfeng',
  356. 'user_lng' => (string)$order['long'], // 用户地址经度
  357. 'user_lat' => (string)$order['lat'], // 用户地址纬度
  358. 'user_address' => $order['fullAddress'], // 用户详细地址
  359. 'weight' => $totalWeight, // 物品重量(单位:克)
  360. 'product_type' => 4, // 物品类型(4:鲜花绿植)
  361. 'push_time' => time(), // 推单时间(秒级时间戳)
  362. 'shop' => [ // 发货店铺信息
  363. 'shop_name' => $shop['merchantName'], // 店铺名称
  364. 'shop_address' => $shop['address'], // 店铺地址
  365. 'shop_lng' => (string)$shop['long'], // 店铺经度
  366. 'shop_lat' => (string)$shop['lat'], // 店铺纬度
  367. 'shop_phone' => $shop['mobile'], // 店铺联系电话
  368. ],
  369. //-------------------------- 非必填 -------------------------------
  370. 'city_name' => $order['city'], // 发单城市
  371. //'order_source' => '6', // 订单接入来源(6:京东秒送,可自定义)
  372. 'source_origin_order_id' => $order['orderSn'], // 商流订单号
  373. 'total_price' => (int)($order['actPrice'] * 100), // 用户订单总金额(单位:分)
  374. 'is_appoint' => 0, // 是否预约单(0:非预约单)
  375. 'appoint_type' => 0, // TODO 预约单类型(0:立即单)
  376. 'expect_time' => 0, // TODO 用户期望送达时间
  377. //'expect_pickup_time' => 0, // TODO 用户期望上门时间 -- appoint_type=2时需必传,秒级时间戳
  378. 'shop_expect_time' => 0, // TODO 商家期望送达时间 -- 格式为:{timestamp},
  379. 'lbs_type' => 2, // 坐标类型(2:高德坐标)
  380. 'is_insured' => 0, // 是否保价(0:非保价)
  381. 'is_person_direct' => 0, // 是否专人直送(0:否)
  382. 'vehicle' => 0, // 配送交通工具(0:否)
  383. //'four_wheeler_type' => 0, // 车型 -- 只有vehicle = 2,⻋型字段才有效。
  384. 'declared_value' => 0, // 保价金额(单位:分)
  385. 'gratuity_fee' => 0, // 订单小费(单位:分)
  386. 'rider_pick_method' => 1, // 物流流向(1:从门店取件送至用户)
  387. 'return_flag' => 511, // 返回字段控制标志位(511:全部返回)
  388. 'multi_pickup_info' => [], // 多点取货信息(暂不使用)
  389. ];
  390. }
  391. /**
  392. * 并发获取各平台报价(核心实现)
  393. *
  394. * 使用 HttpClient::postConcurrent 实现真正的并发调用,每个平台请求 5 秒超时。
  395. * 某个平台的超时或失败不会影响其他平台的执行。
  396. *
  397. * @param array $preparedData 准备好的各平台数据
  398. * @param string $orderTime 配送时间
  399. * @return array 包含成功和失败结果
  400. */
  401. private function concurrentGetPrices($preparedData, $orderTime)
  402. {
  403. $results = [
  404. 'success' => [],
  405. 'failed' => [],
  406. ];
  407. // 第一步:收集所有平台的请求信息
  408. $allRequests = [];
  409. $platformMapping = []; // 用于将请求标识映射回平台名称
  410. foreach ($preparedData as $platform => $data) {
  411. try {
  412. if (!isset($this->adapters[$platform])) {
  413. $results['failed'][$platform] = '平台适配器未初始化';
  414. continue;
  415. }
  416. $adapter = $this->adapters[$platform];
  417. // 根据平台类型调用相应的 buildPriceRequest(s) 方法
  418. if ($platform === 'huolala') {
  419. // 货拉拉返回多个请求(一个请求对应一个车型)
  420. $priceRequests = $adapter->buildPriceRequest($data, $orderTime);
  421. foreach ($priceRequests as $requestKey => $requestInfo) {
  422. $allRequests[$requestKey] = $requestInfo;
  423. $platformMapping[$requestKey] = ['platform' => 'huolala', 'data' => $data];
  424. }
  425. } else {
  426. // 其他平台(蜂鸟、闪送、顺丰)各返回一个请求
  427. $requestInfo = $adapter->buildPriceRequest($data, $orderTime);
  428. $requestKey = "{$platform}_quote";
  429. $allRequests[$requestKey] = $requestInfo;
  430. $platformMapping[$requestKey] = ['platform' => $platform, 'data' => $data];
  431. }
  432. } catch (\Throwable $e) {
  433. $results['failed'][$platform] = "构建请求失败: {$e->getMessage()}";
  434. Yii::error("[DispatchService] {$platform} 构建请求异常: {$e->getMessage()}");
  435. }
  436. }
  437. if (empty($allRequests)) {
  438. return $results;
  439. }
  440. // 第二步:使用 postConcurrent 并发发送所有请求
  441. $startTime = microtime(true);
  442. $concurrentResults = HttpClient::postConcurrent($allRequests, 3);
  443. $totalDuration = microtime(true) - $startTime;
  444. Yii::info("[DispatchService] 所有报价请求完成 (" . round($totalDuration * 1000) . "ms)");
  445. // 第三步:处理并发请求的结果
  446. foreach ($concurrentResults['success'] as $requestKey => $response) {
  447. if (!isset($platformMapping[$requestKey])) {
  448. continue;
  449. }
  450. $mapping = $platformMapping[$requestKey];
  451. $platform = $mapping['platform'];
  452. $data = $mapping['data'];
  453. try {
  454. $adapter = $this->adapters[$platform];
  455. $quote = null;
  456. if ($platform === 'huolala') {
  457. // 货拉拉的响应处理
  458. $quote = $adapter->processPriceResponse($response);
  459. if ($quote && !isset($results['success']['huolala'])) {
  460. // 第一次处理货拉拉的结果,初始化
  461. $results['success']['huolala'] = [
  462. 'platform' => 'huolala',
  463. 'city_id' => $data['city_id'],
  464. 'vehicle_type_list' => $data['vehicle_type_list'],
  465. 'price_info_list' => [],
  466. ];
  467. }
  468. $keyArr = explode('_', $requestKey);
  469. $vehicle_type_list_key = $keyArr[2];
  470. // 将该车型的报价加入到列表中
  471. if ($quote && isset($results['success']['huolala'])) {
  472. $results['success']['huolala']['price_info_list'][] = [
  473. 'calculate_price_info' => isset($quote['calculate_price_info_list']) ? $quote['calculate_price_info_list'][0] : [],
  474. 'vehicle_type' => $data['vehicle_type_list'][$vehicle_type_list_key]
  475. ];
  476. }
  477. } else {
  478. // 蜂鸟和闪送的响应处理
  479. $quote = $adapter->processPriceResponse($response);
  480. if ($quote && !isset($quote['error'])) {
  481. $quote['_duration'] = $totalDuration;
  482. $results['success'][$platform] = $quote;
  483. Yii::info("[DispatchService] {$platform} 报价成功 (" . round($totalDuration * 1000) . "ms)");
  484. } else {
  485. $errorMsg = $quote['error'] ?? '报价返回数据为空';
  486. $results['failed'][$platform] = $errorMsg;
  487. Yii::warning("[DispatchService] {$platform} 报价返回错误: {$errorMsg}");
  488. }
  489. }
  490. } catch (\Throwable $e) {
  491. $platform = $mapping['platform'];
  492. $results['failed'][$platform] = "处理响应失败: {$e->getMessage()}";
  493. Yii::error("[DispatchService] {$platform} 处理响应异常: {$e->getMessage()}");
  494. }
  495. }
  496. // 第四步:处理失败的请求
  497. foreach ($concurrentResults['failed'] as $requestKey => $errorMsg) {
  498. if (!isset($platformMapping[$requestKey])) {
  499. continue;
  500. }
  501. $platform = $platformMapping[$requestKey]['platform'];
  502. // 对于货拉拉,只记录某个车型失败,不影响整体平台状态
  503. if ($platform === 'huolala') {
  504. Yii::warning("[DispatchService] huolala 车型报价失败 ({$requestKey}): {$errorMsg}");
  505. } else {
  506. if (!isset($results['failed'][$platform])) {
  507. $results['failed'][$platform] = $errorMsg;
  508. Yii::error("[DispatchService] {$platform} 报价请求失败: {$errorMsg}");
  509. }
  510. }
  511. }
  512. // 为货拉拉添加元数据
  513. if (isset($results['success']['huolala'])) {
  514. $results['success']['huolala']['_duration'] = $totalDuration;
  515. }
  516. return $results;
  517. }
  518. public function openCitiesLists($platform)
  519. {
  520. $ap = $this->adapters[$platform];
  521. return $ap->cityList();
  522. }
  523. /**
  524. * 刷新 token
  525. */
  526. public function refreshToken($platform, $refreshToken, $data=[])
  527. {
  528. switch($platform) {
  529. case 'shansong':
  530. $auth = new \common\components\delivery\platform\shansong\Auth();
  531. $auth->refreshAccessToken($refreshToken);
  532. break;
  533. case 'huolala':
  534. $auth = new \common\components\delivery\platform\huolala\Auth();
  535. $auth->refreshAccessToken($refreshToken);
  536. break;
  537. case 'fengniao':
  538. $auth = new \common\components\delivery\platform\fengniao\Auth();
  539. $auth->refreshAccessToken($refreshToken, $data['merchantId']);
  540. break;
  541. case 'shunfeng':
  542. $auth = new \common\components\delivery\platform\shunfeng\Auth();
  543. $auth->refreshAccessToken($refreshToken);
  544. break;
  545. }
  546. }
  547. }