Express.php
5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace addons\shopro\library;
use fast\Http;
class Express
{
// 查询接口
const REQURL = "https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
// 订阅接口
const SUBURL = "https://api.kdniao.com/api/dist";
protected $config = [];
/**
* 构造函数
*/
public function __construct()
{
$config = \addons\shopro\model\Config::get(['name' => 'services']);
$config = ($config && $config->value) ? json_decode($config->value, true) : [];
$expressConfig = $config['express'] ?? [];
if (!$expressConfig || !$expressConfig['ebusiness_id'] || !$expressConfig['appkey']) {
throw new \Exception('请配置快递接口');
}
$this->config = $expressConfig;
}
/**
* Json方式 物流信息订阅
*/
public function subscribe($data = [], $orderExpress = null, $order = null)
{
$requestData = $this->getRequestData($data, $orderExpress = null, $order = null);
$datas = [
'EBusinessID' => $this->config['ebusiness_id'],
'RequestType' => $this->config['type'] == 'free' ? '1008' : '8008',
'RequestData' => urlencode($requestData),
'DataType' => '2',
];
$datas['DataSign'] = $this->encrypt($requestData, $this->config['appkey']);
$result = Http::sendRequest(self::SUBURL, $datas, 'POST', []);
if ($result['ret'] == 1) {
$exResult = json_decode($result['msg'], true);
if (!$exResult['Success']) {
throw new \Exception($exResult['Reason']);
}
return $exResult;
} else {
throw new \Exception($result['msg']);
}
}
// 查询快递信息
public function search($data = [], $orderExpress = null, $order = null)
{
$requestData = $this->getRequestData($data, $orderExpress = null, $order = null);
$datas = [
'EBusinessID' => $this->config['ebusiness_id'],
'RequestType' => $this->config['type'] == 'free' ? '1002' : '8001',
'RequestData' => urlencode($requestData),
'DataType' => '2',
];
$datas['DataSign'] = $this->encrypt($requestData, $this->config['appkey']);
$result = Http::sendRequest(self::REQURL, $datas, 'POST', []);
if ($result['ret'] == 1) {
$exResult = json_decode($result['msg'], true);
if (!$exResult['Success']) {
throw new \Exception($exResult['Reason']);
}
return $exResult;
} else {
throw new \Exception($result['msg']);
}
}
// 组装请求数据
private function getRequestData($data = [], $orderExpress = null, $order = null) {
$requestData = [
'OrderCode' => $order ? $order->order_sn : '',
'ShipperCode' => $data['express_code'],
'LogisticCode' => $data['express_no'],
];
if ($data['express_code'] == 'JD') {
// 京东青龙配送单号
$requestData['CustomerName'] = $this->config['jd_code'];
} else if ($data['express_code'] == 'SF') {
// 收件人手机号后四位
$requestData['CustomerName'] = substr($order->phone, 7);
}
$requestData = json_encode($requestData);
return $requestData;
}
// 差异更新物流信息
public function checkAndAddTraces ($orderExpress, $express) {
$traces = $express['Traces'];
// 查询现有轨迹记录
$orderExpressLog = \addons\shopro\model\OrderExpressLog::where('order_express_id', $orderExpress->id)->select();
$log_count = count($orderExpressLog);
if ($log_count > 0) {
// 移除已经存在的记录
array_splice($traces, 0, $log_count);
}
// 增加包裹记录
foreach ($traces as $k => $trace) {
$orderExpressLog = new \addons\shopro\model\OrderExpressLog();
$orderExpressLog->user_id = $orderExpress['user_id'];
$orderExpressLog->order_id = $orderExpress['order_id'];
$orderExpressLog->order_express_id = $orderExpress['id'];
$orderExpressLog->status = $trace['Action'] ?? $express['State'];
$orderExpressLog->content = $trace['AcceptStation'];
$orderExpressLog->changedate = substr($trace['AcceptTime'], 0, 19); // 快递鸟测试数据 返回的是个 2020-08-03 16:58:272 格式
$orderExpressLog->location = $trace['Location'] ?? ($express['Location'] ?? null);
$orderExpressLog->save();
}
}
// 组装返回结果
public function setPushResult($success = false, $reason = '') {
$result = [
"EBusinessID" => $this->config['ebusiness_id'],
"UpdateTime" => date('Y-m-d H:i:s'),
"Success" => $success,
"Reason" => $reason
];
return json_encode($result);
}
// 加签
function encrypt($data, $appkey)
{
return urlencode(base64_encode(md5($data . $appkey)));
}
}