92 lines
3.7 KiB
PHP
Executable File
92 lines
3.7 KiB
PHP
Executable File
<?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=true(LOGISTICS_KUAIDI100_ENABLE 已废弃,可删)
|
||
$kuaidiEnabled = ! $forceDisable && $hasCreds;
|
||
|
||
// 京东官方物流接口(JOS 开放平台 https://api.jd.com/routerjson)
|
||
// 仅当填好 APP_KEY / APP_SECRET / ACCESS_TOKEN 时启用;作为快递100 对京东自营单(JDVE…)轨迹陈旧时的兜底数据源。
|
||
// 未配置时本段 enable=false,ExpressTrackService 完全沿用现有快递100 逻辑,互不影响。
|
||
$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,
|
||
'gateway' => $envJd('GATEWAY', 'https://api.jd.com/routerjson'),
|
||
// 青龙运单轨迹查询;如改用 ECLP 等可在 .env 覆盖 method / param_key / waybill_field
|
||
'method' => $envJd('METHOD', 'jingdong.ldop.receive.trace.get'),
|
||
'param_key' => $envJd('PARAM_KEY', '360buy_param_json'),
|
||
'waybill_field' => $envJd('WAYBILL_FIELD', 'waybillCode'),
|
||
// 部分接口需商家编码 / 事业部编码,可选
|
||
'customer_code' => $envJd('CUSTOMER_CODE', ''),
|
||
'api_version' => $envJd('API_VERSION', '2.0'),
|
||
],
|
||
];
|