Express.php
4.2 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
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\api\controller;
use app\admin\model\Order;
use app\common\controller\Api;
use think\Db;
use think\Validate;
/**
* 物流接口
*/
class Express extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = '*';
protected $uid = '';//token存贮user_id
public function _initialize()
{
parent::_initialize();
$this->uid = $this->auth->getUserId();
}
/**
* @ApiTitle (调用查询物流轨迹)
* @ApiSummary (调用查询物流轨迹)
* @ApiMethod (GET)
* @ApiRoute (/api/express/getExpress)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="order_id", type="inter", required=true, description="订单id")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1575532542",
"data": {
"LogisticCode": "75317246876602",//快递单号
"ShipperCode": "ZTO",//快递编码
"Traces": [
{
"AcceptStation": "【东莞市】 【东莞厚街】(0769-86947628) 的 张小姐(18027104980) 已揽收",//轨迹
"AcceptTime": "2019-12-03 18:43:03"//时间
},
{
"AcceptStation": "【东莞市】 快件离开 【东莞厚街】 已发往 【天津中转部】",
"AcceptTime": "2019-12-04 00:18:01"
},
{
"AcceptStation": "【天津市】 快件离开 【天津中转部】 已发往 【天津南开六部】",
"AcceptTime": "2019-12-05 14:35:03"
}
],
"State": "2",//2:在途中,3:签收,4:问题件
"EBusinessID": "1442433",
"Success": true,
"LogisticCompany": "中通快递"//快递公司
}
})
*
*/
public function getExpress(){
$order_id = $this->request->get('order_id');
$rule = config('verify.order_detail');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['order_id'=>$order_id])) {
$this->error($validate->getError());
}
$where = ['id'=>$order_id,'uid'=>$this->uid];
$order = Common::findSoftWhereData('order',$where,'id,ShipperCode,LogisticCode,LogisticCompany');
if($order){
$logisticResult = $this->getOrderTracesByJson($order['LogisticCode'],$order['ShipperCode']);//快递编码,单号
$data = json_decode($logisticResult,true);
if($data['Success'] == true){//返回信息成功
$data['LogisticCompany'] = $order['LogisticCompany'];
$this->success('成功',$data);
}
$this->error('未找到物流踪迹');
}
$this->error('未找到物流踪迹');
}
/**
* 物流
* @ApiInternal
*/
public function getOrderTracesByJson($kgs,$number){
$express = config('verify.express');
$EBusinessID = $express['EBusinessID'];
$appKey = $express['appKey'];
$url = $express['url'];
$requestData= "{'OrderCode':'','ShipperCode':'$kgs','LogisticCode':'$number'}";
$data = array(
'EBusinessID' => $EBusinessID,
'RequestType' => '1002',
'RequestData' => urlencode($requestData) ,
'DataType' => '2',
);
$data['DataSign'] = $this->encrypt($requestData.$appKey);
$result = $this->auth->http_post($url, $data);
return $result;
}
/**
* 签名
* @ApiInternal
*/
public function encrypt($str) {
return urlencode(base64_encode(md5($str)));
}
}