BirdController.php
13.5 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/6/7
* Time: 9:25
*/
namespace app\admin\controller;
use app\portal\model\AddressModel;
use app\portal\model\IndentModel;
use cmf\controller\AdminBaseController;
use think\Db;
class BirdController extends AdminBaseController
{
protected $EBusinessID;
protected $AppKey;
protected $indent_id;
function _initialize() {
$bird = config('bird');
$this->EBusinessID = $bird['EBusinessID'];
$this->AppKey = $bird['AppKey'];
//电商ID
defined('EBusinessID') or define('EBusinessID', $this->EBusinessID);
//电商加密私钥,快递鸟提供,注意保管,不要泄漏
defined('AppKey') or define('AppKey', $this->AppKey);
}
/**
* 电子面单
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function createOrder()
{
if(empty($_POST['indent_id'])){
$indent_id = null;
}else{
$indent_id = $_POST['indent_id'];
}
if(empty($indent_id)){
return "缺少必要参数";
}
$this->indent_id = $indent_id;
$where1['id'] = ['eq',$this->indent_id];
$indentModel = new IndentModel();
$indent = $indentModel->findData($where1);
if(empty($indent)){
return '未查询到该订单';
}
if($indent['state'] != 2){
return '该订单不是待发货状态';
}
//收货地址
$where2['id'] = ['eq',$indent['indent_address']];
$addressModel = new AddressModel();
$address = $addressModel->findData($where2);
$region = explode(' ',$address['region']);
//请求url,接口正式地址:http://api.kdniao.com/api/OOrderService 测试环境地址:http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json
// defined('ReqURL') or define('ReqURL', 'http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json');
//请求url,接口正式地址:http://api.kdniao.com/api/EOrderService 测试环境地址:http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json
defined('ReqURL') or define('ReqURL', 'http://api.kdniao.com/api/EOrderService');
//构造在线下单提交信息
$eorder = [];
$eorder["ShipperCode"] = "$indent[logistic_name]";
$eorder["OrderCode"] = $indent['order_number'];
$eorder["PayType"] = 1;
$eorder["ExpType"] = 1;
$eorder['IsNotice'] = 0;
$sender = config('sender');
// $sender["Name"] = "李先生";
// $sender["Mobile"] = "18888888888";
// $sender["ProvinceName"] = "李先生";
// $sender["CityName"] = "深圳市";
// $sender["ExpAreaName"] = "福田区";
// $sender["Address"] = "赛格广场5401AB";
$receiver = [];
$receiver["Name"] = $address['name'];
$receiver["Mobile"] = $address['phone'];
$receiver["ProvinceName"] = $region[0];
$receiver["CityName"] = $region[1];
$receiver["ExpAreaName"] = $region[2];
$receiver["Address"] = $address['detailed'];
$commodityOne = [];
$commodityOne["GoodsName"] = "书";
$commodity = [];
$commodity[] = $commodityOne;
$eorder["Sender"] = $sender;
$eorder["Receiver"] = $receiver;
$eorder["Commodity"] = $commodity;
//调用在线下单
$jsonParam = json_encode($eorder, JSON_UNESCAPED_UNICODE);
$jsonResult = $this->submitOOrder($jsonParam);
//解析在线下单返回结果
$result = json_decode($jsonResult, true);
cache('test',$result);
if ($result["ResultCode"] == "100") {
if(!empty($result['Order']['KDNOrderCode'])){
$indentModel->updateData(['id'=>$indent_id],['logistic_code'=>$result['Order']['KDNOrderCode']]);
$demo = $this->takeOrder($indent_id);
}
return json_encode(['code'=>20000,'msg'=>'SUCCESS','data'=>$result]);//返回快递单号
} else {
return json_encode(['code'=>40000,'msg'=>$result['Reason']]);
}
}
/**
* Json方式 提交在线下单
* @param $requestData
* @return url响应返回的html
*/
function submitOOrder($requestData)
{
$datas = array(
'EBusinessID' => EBusinessID,
'RequestType' => '1001',
'RequestData' => urlencode($requestData),
'DataType' => '2',
);
$datas['DataSign'] = $this->encrypt($requestData, AppKey);
$result = $this->sendPost(ReqURL, $datas);
//根据公司业务处理返回的信息......
return $result;
}
/**
* 查询订单
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getOrder($indent_id = null){
if(empty($indent_id)){
$this->error('缺少必要参数','','','');
}
$this->indent_id = $indent_id;
$where['id'] = ['eq',$this->indent_id];
$indentModel = new IndentModel();
$indent = $indentModel->findData($where);
if(empty($indent)){
$this->error('未查询到该订单','','','');
}
if(!empty($indent['salesman_uid'])){
$indent['order_number']= null;
}
// if($indent['state'] != 5){
// $this->error('该订单不是已发货状态','','','');
// }
//请求url,接口正式地址:http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx 测试环境地址:http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json
defined('ReqURL') or define('ReqURL', 'http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx');
$jsonResult = $this->getOrderTracesByJson($indent['order_number'],$indent['logistic_code'],$indent['logistic_name']);
$result = json_decode($jsonResult,true);
if($result['Success'] == true){
return ['code'=>20000,'msg'=>'SUCCESS','data'=>['state'=>$result['State'],'traces'=>$result['Traces']]];//订单轨迹
}else{
return ['code'=>40000,'msg'=>$result['Reason']];
}
}
/**
* Json方式 查询订单物流轨迹
*/
function getOrderTracesByJson($OrderCode,$LogisticCode,$LogisticName){
$requestData= "{'OrderCode':'$OrderCode','ShipperCode':'$LogisticName','LogisticCode':$LogisticCode}";
$datas = array(
'EBusinessID' => EBusinessID,
'RequestType' => '1002',
'RequestData' => urlencode($requestData) ,
'DataType' => '2',
);
$datas['DataSign'] = $this->encrypt($requestData, AppKey);
$result=$this->sendPost(ReqURL, $datas);
//根据公司业务处理返回的信息......
return $result;
}
/**
* 订阅订单轨迹
* @return string|void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function takeOrder($indent_id = null){
if(empty($indent_id)){
$this->error('缺少必要参数','','','');
}
$this->indent_id = $indent_id;
$where['id'] = ['eq',$this->indent_id];
$indentModel = new IndentModel();
$indent = $indentModel->findData($where);
if(empty($indent)){
$this->error('未查询到该订单','','','');
}
// if($indent['state'] != 2){
// $this->error('该订单不是已发货状态','','','');
// }
//收货地址
$where2['id'] = ['eq',$indent['indent_address']];
$addressModel = new AddressModel();
$address = $addressModel->findData($where2);
//请求url,接口正式地址:http://api.kdniao.com/api/dist 测试环境地址:http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInv
defined('ReqURL') or define('ReqURL', 'http://api.kdniao.com/api/dist');
$jsonResult = $this->orderTracesSubByJson($indent['order_number'],$indent['logistic_code'],$address,$indent['logistic_name'],$indent['logistic_name']);
$result = json_decode($jsonResult,true);
cache('b',$result);
if(empty($result['Reason'])){
return ['code'=>20000,'msg'=>'SUCCESS'];//订单轨迹
}else{
return ['code'=>40000,'msg'=>$result['Reason']];
}
}
/**
* Json方式 物流信息订阅
*/
public function orderTracesSubByJson($OrderCode,$LogisticCode,$address,$LogisticName){
$sender = config('sender');
$region = explode(' ',$address['region']);
$requestData="{'OrderCode': '$OrderCode',".
"'ShipperCode':'$LogisticName',".
"'LogisticCode':'$LogisticCode',".
"'PayType':1,".
"'ExpType':1,".
"'IsNotice':0,".
"'Sender':".
"{".
"'Company':'LV','Name':'$sender[Name]','Mobile':'$sender[Mobile]','ProvinceName':'$sender[ProvinceName]','CityName':'$sender[CityName]','ExpAreaName':'$sender[ExpAreaName]','Address':'$sender[Address]'},".
"'Receiver':".
"{".
"'Company':'GCCUI','Name':'$address[name]','Mobile':'$address[phone]','ProvinceName':'$region[0]','CityName':'$region[1]','ExpAreaName':'$region[2]','Address':'$address[detailed]'},".
"'Commodity':".
"[{".
"'GoodsName':'书'}],".
"'Remark':'小心轻放'}";
$datas = array(
'EBusinessID' => EBusinessID,
'RequestType' => '1008',
'RequestData' => urlencode($requestData) ,
'DataType' => '2',
);
$datas['DataSign'] = $this->encrypt($requestData, AppKey);
$result = $this->sendPost(ReqURL, $datas);
//根据公司业务处理返回的信息......
return $result;
}
public function monitoring($indent_id = null){
// if(empty($indent_id)){
// $this->error('缺少必要参数','','','');
// }
// $this->indent_id = $indent_id;
// $where['id'] = ['eq',$this->indent_id];
// $indentModel = new IndentModel();
// $indent = $indentModel->findData($where);
// if(empty($indent)){
// $this->error('未查询到该订单','','','');
// }
//// if($indent['state'] != 2){
//// $this->error('该订单不是已发货状态','','','');
//// }
// //收货地址
// $where2['id'] = ['eq',$indent['indent_address']];
// $addressModel = new AddressModel();
// $address = $addressModel->findData($where2);
//请求url,接口正式地址:http://api.kdniao.com/api/eorderservice 测试环境地址:http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInv
defined('ReqURL') or define('ReqURL', 'http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx');
$jsonResult = $this->getOrder2();
$result = json_decode($jsonResult,true);
dump($result);
if(empty($result['Reason'])){
return ['code'=>20000,'msg'=>'SUCCESS'];//订单轨迹
}else{
return ['code'=>40000,'msg'=>$result['Reason']];
}
}
public function getOrder2(){
$requestData= "{'OrderCode':'','ShipperCode':'YD','LogisticCode':'3718481494155'}";
$datas = array(
'EBusinessID' => EBusinessID,
'RequestType' => '1002',
'RequestData' => urlencode($requestData) ,
'DataType' => '2',
);
$datas['DataSign'] = $this->encrypt($requestData, AppKey);
$result=$this->sendPost(ReqURL, $datas);
//根据公司业务处理返回的信息......
return $result;
}
/**
* post提交数据
* @param string $url 请求Url
* @param array $datas 提交的数据
* @return url响应返回的html
*/
function sendPost($url, $datas)
{
$temps = array();
foreach ($datas as $key => $value) {
$temps[] = sprintf('%s=%s', $key, $value);
}
$post_data = implode('&', $temps);
$url_info = parse_url($url);
if(empty($url_info['port']))
{
$url_info['port']=80;
}
$httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
$httpheader .= "Host:" . $url_info['host'] . "\r\n";
$httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
$httpheader .= "Content-Length:" . strlen($post_data) . "\r\n";
$httpheader .= "Connection:close\r\n\r\n";
$httpheader .= $post_data;
$fd = fsockopen($url_info['host'], $url_info['port']);
fwrite($fd, $httpheader);
$gets = "";
$headerFlag = true;
while (!feof($fd)) {
if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
break;
}
}
while (!feof($fd)) {
$gets .= fread($fd, 128);
}
fclose($fd);
return $gets;
}
/**
* 电商Sign签名生成
* @param data 内容
* @param appkey Appkey
* @return DataSign签名
*/
function encrypt($data, $appkey)
{
return urlencode(base64_encode(md5($data . $appkey)));
}
public function test(){
$arr = [];
echo $arr;
}
}