DispatchService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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); // 产生外部请求 -- TODO 优化成不耗时等待
  195. $cityInfoRevision = $cityVehicleList['city_info_revision'];
  196. // 获取所有车型列表
  197. $vehicleList = $cityVehicleList['vehicle_list'];
  198. if (empty($vehicleList)) {
  199. throw new \Exception('没有找到可选车型');
  200. }
  201. // 解析额外需求
  202. $specReqItem = $cityVehicleList['spec_req_item'] ?? [];
  203. $specReq = [];
  204. foreach ($specReqItem as $item) {
  205. $specReq[] = $item['type'] ?? null;
  206. }
  207. $specReq = array_filter($specReq);
  208. // 构建所有车型数据
  209. $vehicleTypeList = [];
  210. foreach ($vehicleList as $vehicle) {
  211. if(!in_array($vehicle['vehicle_name'], ['跑腿', '小面', '微面'])){
  212. continue;
  213. }
  214. $vehicleStd = [];
  215. $vehicleStd[] = count($vehicle['vehicle_std_item']) > 0 ? $vehicle['vehicle_std_item'][0]['name'] : '';
  216. $vehicleTypeList[$vehicle['order_vehicle_id']] = [
  217. 'order_vehicle_id' => $vehicle['order_vehicle_id'],
  218. 'city_info_revision' => $cityInfoRevision,
  219. 'order_time' => time() + 600,
  220. 'addr_info' => [
  221. [
  222. 'name' => $shop['merchantName'],
  223. 'addr' => $shop['province'] . $shop['city'] . $shop['dist'] . $shop['address'],
  224. 'city_id' => $cityId,
  225. 'city_name' => $shop['city'],
  226. 'district_name' => $shop['dist'],
  227. 'house_number' => $shop['floor'],
  228. 'contacts_name' => $shop['mobile'],
  229. 'contacts_phone_no' => $shop['mobile'],
  230. 'lat_lon' => ['lat' => (float)$shop['lat'], 'lon' => (float)$shop['long']],
  231. ],
  232. [
  233. 'name' => $order['customName'],
  234. 'addr' => $order['fullAddress'],
  235. 'city_id' => $cityId,
  236. 'city_name' => $order['city'],
  237. 'district_name' => $order['dist'],
  238. 'house_number' => $order['floor'],
  239. 'contacts_name' => $order['customName'],
  240. 'contacts_phone_no' => $order['customMobile'],
  241. 'lat_lon' => ['lat' => (float)$order['lat'], 'lon' => (float)$order['long']],
  242. ]
  243. ],
  244. 'vehicle_std' => $vehicleStd,
  245. 'spec_req' => array_values($specReq),
  246. 'coupon_id' => 123456,
  247. 'invoice_type' => 1,
  248. 'order_service_type' => 1,
  249. '_meta' => [
  250. 'vehicle_type' => $vehicle['vehicle_name'],
  251. 'city_info_revision' => $cityInfoRevision,
  252. ]
  253. ];
  254. }
  255. return [
  256. 'platform' => 'huolala',
  257. 'city_id' => $cityId,
  258. 'vehicle_type_list' => $vehicleTypeList,
  259. ];
  260. }
  261. /**
  262. * 准备蜂鸟订单数据
  263. */
  264. private function prepareFengniaoData($order, $shop)
  265. {
  266. $this->adapters['fengniao']->setMerchantId(14594092); // TODO: 确认是否需要使用动态的商户ID
  267. $itemInfos = OrderItemClass::getAllByCondition(['orderSn' => $order['orderSn']], null, 'id, name, unitPrice, num');
  268. $goodsItemList = [];
  269. foreach ($itemInfos as $item) {
  270. $goodsItemList[] = [
  271. 'item_actual_amount_cent' => (int)($item['unitPrice'] * 100 * $item['num']),
  272. 'item_amount_cent' => (int)($item['unitPrice'] * 100),
  273. 'item_id' => $item['id'],
  274. 'item_name' => $item['name'],
  275. 'item_quantity' => $item['num'],
  276. ];
  277. }
  278. return [
  279. 'platform' => 'fengniao',
  280. 'partner_order_code' => $order['orderSn'],
  281. 'receiver_primary_phone' => $order['customMobile'],
  282. 'receiver_name' => $order['customName'],
  283. 'receiver_latitude' => (float)$order['lat'],
  284. 'receiver_longitude' => (float)$order['long'],
  285. 'receiver_address' => $order['fullAddress'],
  286. 'position_source' => 3,
  287. 'goods_count' => count($goodsItemList),
  288. 'goods_weight' => (float)$order['weight'],
  289. 'goods_total_amount_cent' => (int)($order['prePrice'] * 100),
  290. 'goods_actual_amount_cent' => (int)($order['actPrice'] * 100),
  291. 'goods_item_list' => $goodsItemList,
  292. 'order_type' => 1,
  293. 'chain_store_id' => 467788524,
  294. 'order_remark' => $order['remark'] ?? '',
  295. ];
  296. }
  297. /**
  298. * 准备闪送订单数据
  299. */
  300. private function prepareShansongData($order, $shop)
  301. {
  302. return [
  303. 'platform' => 'shansong',
  304. 'city_name' => $shop['city'],
  305. 'sender' => [
  306. 'from_address' => $shop['address'],
  307. 'from_address_detail' => $shop['floor'],
  308. 'from_sender_name' => $shop['shopName'],
  309. 'from_mobile' => $shop['mobile'],
  310. 'from_latitude' => (float)$shop['lat'],
  311. 'from_longitude' => (float)$shop['long'],
  312. ],
  313. 'receiver_list' => [
  314. [
  315. 'order_no' => $order['orderSn'],
  316. 'to_address' => $order['address'],
  317. 'to_address_detail' => $order['floor'],
  318. 'to_receiver_name' => $order['customName'],
  319. 'to_mobile' => $order['customMobile'],
  320. 'to_latitude' => (float)$order['lat'],
  321. 'to_longitude' => (float)$order['long'],
  322. 'good_type' => 7,
  323. 'weight' => (int)$order['weight'],
  324. 'remarks' => $order['remark'] ?? '',
  325. ]
  326. ],
  327. 'appoint_type' => 0,
  328. 'appointment_date' => '',
  329. 'travel_way' => 0,
  330. 'delivery_type' => 1,
  331. 'expect_start_time' => null,
  332. 'expect_end_time' => null,
  333. ];
  334. }
  335. /**
  336. * 准备顺丰订单数据
  337. */
  338. private function prepareShunfengData($order, $shop)
  339. {
  340. // 获取订单商品信息
  341. $itemInfos = OrderItemClass::getAllByCondition(['orderSn' => $order['orderSn']], null, 'id, name, unitPrice, num, unitWeight');
  342. // 计算商品总重量(单位:克)
  343. $totalWeight = 0;
  344. foreach ($itemInfos as $item) {
  345. $totalWeight += (int)($item['unitWeight'] * $item['num']) * 1000;
  346. }
  347. // 如果没有商品重量,使用订单重量
  348. if ($totalWeight == 0 && isset($order['weight'])) {
  349. $totalWeight = (int)$order['weight'] * 1000;
  350. }
  351. // 默认重量为1000克(1公斤)
  352. if ($totalWeight == 0) {
  353. $totalWeight = 1000;
  354. }
  355. return [
  356. 'platform' => 'shunfeng',
  357. 'user_lng' => (string)$order['long'], // 用户地址经度
  358. 'user_lat' => (string)$order['lat'], // 用户地址纬度
  359. 'user_address' => $order['fullAddress'], // 用户详细地址
  360. 'weight' => $totalWeight, // 物品重量(单位:克)
  361. 'product_type' => 4, // 物品类型(4:鲜花绿植)
  362. 'push_time' => time(), // 推单时间(秒级时间戳)
  363. 'shop' => [ // 发货店铺信息
  364. 'shop_name' => $shop['merchantName'], // 店铺名称
  365. 'shop_address' => $shop['address'], // 店铺地址
  366. 'shop_lng' => (string)$shop['long'], // 店铺经度
  367. 'shop_lat' => (string)$shop['lat'], // 店铺纬度
  368. 'shop_phone' => $shop['mobile'], // 店铺联系电话
  369. ],
  370. //-------------------------- 非必填 -------------------------------
  371. 'city_name' => $order['city'], // 发单城市
  372. //'order_source' => '6', // 订单接入来源(6:京东秒送,可自定义)
  373. 'source_origin_order_id' => $order['orderSn'], // 商流订单号
  374. 'total_price' => (int)($order['actPrice'] * 100), // 用户订单总金额(单位:分)
  375. 'is_appoint' => 0, // 是否预约单(0:非预约单)
  376. 'appoint_type' => 0, // TODO 预约单类型(0:立即单)
  377. 'expect_time' => 0, // TODO 用户期望送达时间
  378. //'expect_pickup_time' => 0, // TODO 用户期望上门时间 -- appoint_type=2时需必传,秒级时间戳
  379. 'shop_expect_time' => 0, // TODO 商家期望送达时间 -- 格式为:{timestamp},
  380. 'lbs_type' => 2, // 坐标类型(2:高德坐标)
  381. 'is_insured' => 0, // 是否保价(0:非保价)
  382. 'is_person_direct' => 0, // 是否专人直送(0:否)
  383. 'vehicle' => 0, // 配送交通工具(0:否)
  384. //'four_wheeler_type' => 0, // 车型 -- 只有vehicle = 2,⻋型字段才有效。
  385. 'declared_value' => 0, // 保价金额(单位:分)
  386. 'gratuity_fee' => 0, // 订单小费(单位:分)
  387. 'rider_pick_method' => 1, // 物流流向(1:从门店取件送至用户)
  388. 'return_flag' => 511, // 返回字段控制标志位(511:全部返回)
  389. 'multi_pickup_info' => [], // 多点取货信息(暂不使用)
  390. ];
  391. }
  392. /**
  393. * 并发获取各平台报价(核心实现)
  394. *
  395. * 使用 HttpClient::postConcurrent 实现真正的并发调用,每个平台请求 5 秒超时。
  396. * 某个平台的超时或失败不会影响其他平台的执行。
  397. *
  398. * @param array $preparedData 准备好的各平台数据
  399. * @param string $orderTime 配送时间
  400. * @return array 包含成功和失败结果
  401. */
  402. private function concurrentGetPrices($preparedData, $orderTime)
  403. {
  404. $results = [
  405. 'success' => [],
  406. 'failed' => [],
  407. ];
  408. // 第一步:收集所有平台的请求信息
  409. $allRequests = [];
  410. $platformMapping = []; // 用于将请求标识映射回平台名称
  411. foreach ($preparedData as $platform => $data) {
  412. try {
  413. if (!isset($this->adapters[$platform])) {
  414. $results['failed'][$platform] = '平台适配器未初始化';
  415. continue;
  416. }
  417. $adapter = $this->adapters[$platform];
  418. // 根据平台类型调用相应的 buildPriceRequest(s) 方法
  419. if ($platform === 'huolala') {
  420. // 货拉拉返回多个请求(一个请求对应一个车型)
  421. $priceRequests = $adapter->buildPriceRequest($data, $orderTime);
  422. foreach ($priceRequests as $requestKey => $requestInfo) {
  423. $allRequests[$requestKey] = $requestInfo;
  424. $platformMapping[$requestKey] = ['platform' => 'huolala', 'data' => $data];
  425. }
  426. } else {
  427. // 其他平台(蜂鸟、闪送、顺丰)各返回一个请求
  428. $requestInfo = $adapter->buildPriceRequest($data, $orderTime);
  429. $requestKey = "{$platform}_quote";
  430. $allRequests[$requestKey] = $requestInfo;
  431. $platformMapping[$requestKey] = ['platform' => $platform, 'data' => $data];
  432. }
  433. } catch (\Throwable $e) {
  434. $results['failed'][$platform] = "构建请求失败: {$e->getMessage()}";
  435. Yii::error("[DispatchService] {$platform} 构建请求异常: {$e->getMessage()}");
  436. }
  437. }
  438. if (empty($allRequests)) {
  439. return $results;
  440. }
  441. // 第二步:使用 postConcurrent 并发发送所有请求
  442. $startTime = microtime(true);
  443. $concurrentResults = HttpClient::postConcurrent($allRequests, 3);
  444. $totalDuration = microtime(true) - $startTime;
  445. Yii::info("[DispatchService] 所有报价请求完成 (" . round($totalDuration * 1000) . "ms)");
  446. // 第三步:处理并发请求的结果
  447. foreach ($concurrentResults['success'] as $requestKey => $response) {
  448. if (!isset($platformMapping[$requestKey])) {
  449. continue;
  450. }
  451. $mapping = $platformMapping[$requestKey];
  452. $platform = $mapping['platform'];
  453. $data = $mapping['data'];
  454. try {
  455. $adapter = $this->adapters[$platform];
  456. $quote = null;
  457. if ($platform === 'huolala') {
  458. // 货拉拉的响应处理
  459. $quote = $adapter->processPriceResponse($response);
  460. if ($quote && !isset($results['success']['huolala'])) {
  461. // 第一次处理货拉拉的结果,初始化
  462. $results['success']['huolala'] = [
  463. 'platform' => 'huolala',
  464. 'city_id' => $data['city_id'],
  465. 'vehicle_type_list' => $data['vehicle_type_list'],
  466. 'price_info_list' => [],
  467. ];
  468. }
  469. $keyArr = explode('_', $requestKey);
  470. $vehicle_type_list_key = $keyArr[2];
  471. // 将该车型的报价加入到列表中
  472. if ($quote && isset($results['success']['huolala'])) {
  473. $results['success']['huolala']['price_info_list'][] = [
  474. 'calculate_price_info' => isset($quote['calculate_price_info_list']) ? $quote['calculate_price_info_list'][0] : [],
  475. 'vehicle_type' => $data['vehicle_type_list'][$vehicle_type_list_key]
  476. ];
  477. }
  478. } else {
  479. // 蜂鸟和闪送的响应处理
  480. $quote = $adapter->processPriceResponse($response);
  481. if ($quote && !isset($quote['error'])) {
  482. $quote['_duration'] = $totalDuration;
  483. $results['success'][$platform] = $quote;
  484. Yii::info("[DispatchService] {$platform} 报价成功 (" . round($totalDuration * 1000) . "ms)");
  485. } else {
  486. $errorMsg = $quote['error'] ?? '报价返回数据为空';
  487. $results['failed'][$platform] = $errorMsg;
  488. Yii::warning("[DispatchService] {$platform} 报价返回错误: {$errorMsg}");
  489. }
  490. }
  491. } catch (\Throwable $e) {
  492. $platform = $mapping['platform'];
  493. $results['failed'][$platform] = "处理响应失败: {$e->getMessage()}";
  494. Yii::error("[DispatchService] {$platform} 处理响应异常: {$e->getMessage()}");
  495. }
  496. }
  497. // 第四步:处理失败的请求
  498. foreach ($concurrentResults['failed'] as $requestKey => $errorMsg) {
  499. if (!isset($platformMapping[$requestKey])) {
  500. continue;
  501. }
  502. $platform = $platformMapping[$requestKey]['platform'];
  503. // 对于货拉拉,只记录某个车型失败,不影响整体平台状态
  504. if ($platform === 'huolala') {
  505. Yii::warning("[DispatchService] huolala 车型报价失败 ({$requestKey}): {$errorMsg}");
  506. } else {
  507. if (!isset($results['failed'][$platform])) {
  508. $results['failed'][$platform] = $errorMsg;
  509. Yii::error("[DispatchService] {$platform} 报价请求失败: {$errorMsg}");
  510. }
  511. }
  512. }
  513. // 为货拉拉添加元数据
  514. if (isset($results['success']['huolala'])) {
  515. $results['success']['huolala']['_duration'] = $totalDuration;
  516. }
  517. return $results;
  518. }
  519. public function openCitiesLists($platform)
  520. {
  521. $ap = $this->adapters[$platform];
  522. return $ap->cityList();
  523. }
  524. /**
  525. * 刷新 token
  526. */
  527. public function refreshToken($platform, $refreshToken, $data=[])
  528. {
  529. switch($platform) {
  530. case 'shansong':
  531. $auth = new \common\components\delivery\platform\shansong\Auth();
  532. $auth->refreshAccessToken($refreshToken);
  533. break;
  534. case 'huolala':
  535. $auth = new \common\components\delivery\platform\huolala\Auth();
  536. $auth->refreshAccessToken($refreshToken);
  537. break;
  538. case 'fengniao':
  539. $auth = new \common\components\delivery\platform\fengniao\Auth();
  540. $auth->refreshAccessToken($refreshToken, $data['merchantId']);
  541. break;
  542. case 'shunfeng':
  543. $auth = new \common\components\delivery\platform\shunfeng\Auth();
  544. $auth->refreshAccessToken($refreshToken);
  545. break;
  546. }
  547. }
  548. }