BirdController.php
4.8 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
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/6/7
* Time: 9:25
*/
namespace app\portal\controller;
use app\portal\model\IndentModel;
use cmf\controller\HomeBaseController;
use think\Db;
class BirdController extends HomeBaseController
{
public function createOrder($indent_id = null)
{
if(empty($indent_id)){
$this->error('缺少必要参数','','','');
}
$where['id'] = ['eq',$indent_id];
$indentModel = new IndentModel();
$indent = $indentModel->findData($where);
if(empty($indent)){
$this->error('未查询到该订单','','','');
}
if($indent['state'] != 2){
$this->error('该订单不是待发货状态','','','');
}
$bird = config('bird');
//电商ID
defined('EBusinessID') or define('EBusinessID', $bird['EBusinessID']);
//电商加密私钥,快递鸟提供,注意保管,不要泄漏
defined('AppKey') or define('AppKey', $bird['AppKey']);
//请求url,接口正式地址:http://api.kdniao.com/api/eorderservice 测试环境地址:http://testapi.kdniao.com:8081/api/oorderservice
defined('ReqURL') or define('ReqURL', 'http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json');
//构造在线下单提交信息
$eorder = [];
$eorder["ShipperCode"] = "SF";
$eorder["OrderCode"] = "PM2016050789471";
$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"] = "李先生";
$receiver["Mobile"] = "18888888888";
$receiver["ProvinceName"] = "李先生";
$receiver["CityName"] = "深圳市";
$receiver["ExpAreaName"] = "福田区";
$receiver["Address"] = "赛格广场5401AB";
$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);
if ($result["ResultCode"] == "100") {
return $result['Order']['LogisticCode'];//返回快递单号
} else {
return $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;
}
/**
* 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);
$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)));
}
}