parseCodes($body); if (empty($items)) { return new RawResponse([]); } $received = $this->resolve($items); $rows = []; foreach ($items as $item) { $code = $item['waybill_code']; $rows[] = [ 'waybill_code' => $code, 'is_galaxy_receive' => !empty($received[$code]) ? 1 : 0, ]; } \think\Log::info('[WmsNotify] waybill.getreceive 处理完成: 共 ' . count($items) . ' 单,已揽收 ' . count(array_filter($received)) . ' 单'); return new RawResponse($rows); } /** * 解析运单号(去空、去重、保序、限量,并拆分『单号|手机号后四位』) * * @param array $body * @return array [{waybill_code, phone}, ...] */ protected function parseCodes(array $body) { return WaybillCodes::parse($body, self::MAX_CODES); } /** * 逐单判定是否已揽收:缓存优先,其余并发查询轨迹接口 * * @param array $items [{waybill_code, phone}, ...] * @return array [运单号 => 1|0] */ protected function resolve(array $items) { $received = []; $redis = $this->connectRedis(); $pending = []; foreach ($items as $item) { $code = $item['waybill_code']; $received[$code] = 0; if ($redis !== null && $this->cacheHit($redis, $code)) { $received[$code] = 1; continue; } $pending[] = $item; } if (empty($pending)) { \think\Log::info('[WmsNotify] waybill.getreceive 命中缓存,跳过轨迹接口: ' . count($items) . ' 单'); return $received; } try { $traceOptions = [ 'timeout' => self::TRACE_TIMEOUT, 'concurrency' => self::TRACE_CONCURRENCY, 'batch_size' => 1, 'retries' => 0, ]; // 轨迹接口地址可通过 application/extra/wms.php 的 trace_base_url 覆盖(便于测试/灰度) $traceBaseUrl = config('wms.trace_base_url'); if (!empty($traceBaseUrl)) { $traceOptions['base_url'] = $traceBaseUrl; } $inputs = []; foreach ($pending as $item) { $inputs[] = [ 'postnum' => $item['waybill_code'], 'phone' => $item['phone'], ]; } $trace = new Trace($traceOptions); $list = $trace->query($inputs); } catch (\Throwable $e) { \think\Log::error('[WmsNotify] waybill.getreceive 轨迹查询异常: ' . $e->getMessage()); return $received; } foreach ($list as $index => $result) { $code = isset($pending[$index]['waybill_code']) ? $pending[$index]['waybill_code'] : ''; if ($code === '' || empty($result['ok']) || empty($result['traces'])) { continue; } $received[$code] = 1; if ($redis !== null) { $this->cacheSet($redis, $code); } } return $received; } /** * @return \Redis|null */ protected function connectRedis() { if (!class_exists('\Redis')) { return null; } $config = (array)config('cache.redis'); $host = !empty($config['host']) ? $config['host'] : '127.0.0.1'; $port = !empty($config['port']) ? (int)$config['port'] : 6379; try { $redis = new \Redis(); $redis->connect($host, $port, 1.5); if (!empty($config['password'])) { $redis->auth($config['password']); } if (!empty($config['select'])) { $redis->select((int)$config['select']); } return $redis; } catch (\Exception $e) { \think\Log::error('[WmsNotify] waybill.getreceive Redis 连接失败: ' . $e->getMessage()); return null; } } protected function cacheHit($redis, $code) { try { return (string)$redis->get(self::CACHE_PREFIX . $code) === '1'; } catch (\Exception $e) { return false; } } protected function cacheSet($redis, $code) { try { $redis->setex(self::CACHE_PREFIX . $code, self::CACHE_TTL, '1'); } catch (\Exception $e) { // 缓存失败不影响本次结果 } } }