Files
2026-06-05 17:15:15 +08:00

99 lines
4.2 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
/**
* 物流轨迹(顺丰 / 京东等)
* 对接快递100「实时查询」: https://api.kuaidi100.com
*
* 规则:同时配置好 CUSTOMER、KEY 即自动走快递100(不必再设 ENABLE=true)。
* 若需临时关闭(保留密钥):LOGISTICS_KUAIDI100_DISABLE = true
*/
$stripQuotes = static function (string $v): string {
$v = trim($v);
if (strlen($v) >= 2 && (($v[0] === '"' && $v[strlen($v) - 1] === '"') || ($v[0] === "'" && $v[strlen($v) - 1] === "'"))) {
return substr($v, 1, -1);
}
return $v;
};
// .env 为 INI 格式:写在某个 [section](如 [trtc])下面的键会变成 trtc.xxx,读不到顶层 LOGISTICS_*
$envKuaidi = static function (string $suffix, $default = '') use ($stripQuotes) {
$top = 'LOGISTICS_KUAIDI100_' . $suffix;
$v = env($top, null);
if ($v !== null && $v !== '') {
return $stripQuotes((string) $v);
}
$nested = env('trtc.' . $top, $default);
return $stripQuotes((string) $nested);
};
$customer = $envKuaidi('CUSTOMER', '');
$key = $envKuaidi('KEY', '');
$hasCreds = $customer !== '' && $key !== '';
$forceDisableRaw = env('LOGISTICS_KUAIDI100_DISABLE', null);
if ($forceDisableRaw === null || $forceDisableRaw === '') {
$forceDisableRaw = env('trtc.LOGISTICS_KUAIDI100_DISABLE', false);
}
$forceDisable = filter_var($forceDisableRaw, FILTER_VALIDATE_BOOLEAN);
// 已填 CUSTOMER+KEY 即启用;需临时关闭时设 LOGISTICS_KUAIDI100_DISABLE=trueLOGISTICS_KUAIDI100_ENABLE 已废弃,可删)
$kuaidiEnabled = ! $forceDisable && $hasCreds;
// 京东物流开放平台 LOPhttps://api.jdl.com
// 仅当填好 APP_KEY / APP_SECRET / ACCESS_TOKENRSA2 模式还需 RSA_PRIVATE_KEY)时启用;
// 作为快递100 对京东自营单(JDVE…)轨迹陈旧时的兜底数据源,并供后台「京东接口更新」按钮使用。
// 未配置时本段 enable=falseExpressTrackService 完全沿用现有快递100 逻辑,互不影响。
// 加签方式由 JD_LOGISTICS_ALGORITHM 控制:rsa2(默认,需私钥) / md5(摘要,无需私钥)。
$envJd = static function (string $suffix, $default = '') use ($stripQuotes) {
$top = 'JD_LOGISTICS_' . $suffix;
$v = env($top, null);
if ($v !== null && $v !== '') {
return $stripQuotes((string) $v);
}
// 兼容误写进 [trtc] 等分区的情况
$nested = env('trtc.' . $top, $default);
return $stripQuotes((string) $nested);
};
$jdAppKey = $envJd('APP_KEY', '');
$jdAppSecret = $envJd('APP_SECRET', '');
$jdAccessToken = $envJd('ACCESS_TOKEN', '');
$jdHasCreds = $jdAppKey !== '' && $jdAppSecret !== '' && $jdAccessToken !== '';
$jdDisableRaw = env('JD_LOGISTICS_DISABLE', null);
if ($jdDisableRaw === null || $jdDisableRaw === '') {
$jdDisableRaw = env('trtc.JD_LOGISTICS_DISABLE', false);
}
$jdDisabled = filter_var($jdDisableRaw, FILTER_VALIDATE_BOOLEAN);
$jdEnabled = ! $jdDisabled && $jdHasCreds;
return [
'kuaidi100' => [
'enable' => $kuaidiEnabled,
'customer' => $customer,
'key' => $key,
'query_url' => $envKuaidi('QUERY_URL', 'https://poll.kuaidi100.com/poll/query.do'),
],
'jd' => [
'enable' => $jdEnabled,
'app_key' => $jdAppKey,
'app_secret' => $jdAppSecret,
'access_token' => $jdAccessToken,
// LOP 网关签名算法(与官方 SDK 一致):md5-salt(默认) / HMacMD5 / HMacSHA1 / HMacSHA256 / HMacSHA512
'algorithm' => $envJd('ALGORITHM', 'md5-salt'),
// 生产网关基址(不含路径)
'gateway' => $envJd('GATEWAY', 'https://api.jdl.com'),
// 接口 path(即签名里的 method);京东物流标准轨迹服务(2025-04-29 改版)
'method' => $envJd('METHOD', '/jd/tracking/query'),
// LOP-DN:对接方案编码(订阅记录可查),标准轨迹服务为 Tracking_JD
'lop_dn' => $envJd('LOP_DN', 'Tracking_JD'),
// 业务体字段:单号字段名 / 单号类型(20000=运单号) / 商家编码(可选)
'reference_field' => $envJd('REFERENCE_FIELD', 'referenceNumber'),
'reference_type' => $envJd('REFERENCE_TYPE', '20000'),
'customer_code' => $envJd('CUSTOMER_CODE', ''),
'api_version' => $envJd('API_VERSION', '2.0'),
],
];