jh-admin/extend/Kdzhushou.php

278 lines
9.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* SAAS应用系统 --- 十年开发经验汇集巨献!
* ==========================================================
* Copy right 2020-2050 成都众联思索科技有限公司,保留所有权利。
* ----------------------------------------------------------
* 官方网址: https://www.zoomtk.com
* 这不是自由软件!未经允许不得用于商业目或程序代码摘取及修改。
* 任何企业和个人未经允许对程序代码以任何形式任何目的再发布传播。
* 唯一发布渠道www.zoomtk.com;非官方渠道统一视为侵权行为。
* ==========================================================
*/
namespace extend;
class Kdzhushou
{
private $key; // 授权key
private $Secret;
private $api_url = 'https://gw.kuaidizs.cn/open/api'; //电子面单网关
public $config = [];
public function __construct($config)
{
$_SERVER=request()->server();
$this->key = $config["kdzhushou_Key"];
$this->Secret = $config["kdzhushou_Secret"];
$this->config = [
'appKey' => $this->key,
'secret' => $this->Secret,
'timestamp' => date('Y-m-d H:i:s'),
'format' => 'json',
'version' => '1.0',
'sign_method' => 'md5',
];
}
/***
* 货
* @param $shopName
* @param $token
* @return mixed
*/
public function GetLoginUrl($shopName = '小店', $token = '')
{
$this->config['method'] = 'kdzs.print.loginurl.get';
$data = [
'userName' => $this->config['appKey'] . "'_{$shopName}_{$token}'",
];
$datas = array_merge($this->config, $data);
$sign = $this->sign($datas);
$datas['sign'] = $sign;
$res = $this->send_post_request($this->api_url, $datas);
return json_decode($res, true);
}
public function search($express_no, $code, $mobile = '')
{
$this->config['method'] = 'kdzs.logistics.trace.search';
$data['cpCode'] = $code;
$data['mailNo'] = $express_no;
if ($code == 'SF') {
$data['tel'] = substr($mobile, -4);
};
$datas = array_merge($this->config, $data);
$sign = $this->sign($datas);
$datas['sign'] = $sign;
$res = $this->send_post_request($this->api_url, $datas);
$res= json_decode($res, true);
return $res;
}
public function getTemplate(){
$this->config['method'] = 'kdzs.print.template.get';
}
/**
* 快递助手接口
* @param 1.'' 2.'' 3.'' 4.创建订单加密接口
* @param arr|$arr
* @return bool|string
*/
public function callbackData($type, $arr)
{
//cpCode 快递公司编码 例ZTO (中通)
//mailNo 快递单号
$host = "https://gw.kuaidizs.cn/open/api";
$appkey = 'EB5DFFB78B86400E9790A1EC8BDD8603';
$secret = '70561989888E4765A1E80D614A0F20CF';
$array = [];
switch ($type) {
case 1:
$method = '';
break;
case 2:
$method = '';
break;
case 3:
$method = '';
break;
case 4:
$method = 'kdzs.print.trade.create';
break;
default:
$method = '';
$arra = [];
break;
}
//1. 设置参数值
$zarr = [
//调用接口的method 在开放平台上可以找到
'method' => $method,
'appKey' => $appkey,
'timestamp' => date('Y-m-d H:i:s', time()),
// 'timestamp' => "2021-07-28 18:15:31",
'format' => "json",
'version' => "1.0",//版本
'sign_method' => "md5",//加密方式
];
$earr = in_array($type, [1, 2, 3]) ? $arr : ['data' => json_encode($arr, TRUE)];
$array = array_merge($zarr, $earr);
// var_dump(
// $array
// );
// die;
//2. 按ASCII顺序排序,拼接参数名与参数值
$string = $this->asc_sort($array);
var_dump($string);
//3. 生成签名,生成算法md5(secret+bar2foo1foo_bar3foobar4+secret)
$sign = md5($secret . $string . $secret);
$array['sign'] = strtoupper($sign);
var_dump($array);
//4. 发送请求根据API的要求定义相对应的Content-Type
$result = $this->send_post_request($host, $array);
$rarray = json_decode($result, true);
echo "<pre>";
print_r($rarray);
die;
}
/**
* 查询物流轨迹企业版
* @param unknown $express_no 物流公司编码
* @param unknown $send_no 快递单号
*/
public function getExpressTracesEnterpriseEdition($express_no, $send_no, $mobile = '')
{
$data = array();
// 测试地址仅可测试100单实际使用时使用“实际地址”
// $url = $this->http_type."poll.kuaidi100.com/test/poll/query.do"; // 测试地址
$url = $this->http_type . "eolink.o.apispace.com/wlgj1/paidtobuy_api/trace_search"; // 实际地址
$data['cpCode'] = $express_no;
$data['mailNo'] = $send_no;
if ($express_no == 'SF') {
$data['tel'] = $mobile;
}
$headers = [
"Content-Type:application/json",
"Authorization-Type:apikey",
"X-APISpace-Token:{$this->key}",
];
$result = $this->sendRequest($url, $data, $headers);
return $result;
}
/**
* POST提交数据
* @param array $data ,string $url
* @return string
*/
public function send_post_request($url, $data)
{ // 模拟提交数据函数
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, request()->header('HTTP_USER_AGENT')); // 模拟用户使用的浏览器HTTP_USER_AGENT
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
curl_setopt($curl, CURLOPT_COOKIEFILE, null); // 读取上面所储存的Cookie信息
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
echo 'Errno' . curl_error($curl);
}
curl_close($curl); // 关键CURL会话
return $tmpInfo; // 返回数据
}
/**
* 处理返回数据
* @param unknown $data
*/
public function handleReturnResult($data)
{
$result = array();
$list = [
'success' => true,
'reason' => '',
'status' => '',
'status_name' => '',
'shipper_code' => '',
'logistic_code' => '',
'list' => [],
];
if (isset($data['logisticsTrace'])) {
$result["success"] = true;
$Trace = $data['logisticsTrace'];
$result["reason"] = $Trace["theLastMessage"];
$result["status"] = '';
$result["status_name"] = $Trace['logisticsStatusDesc'];
$result["shipper_code"] = $Trace["theLastMessage"];
$result["logistic_code"] = $Trace["logisticsStatus"];
if (isset($Trace["logisticsTraceDetailList"])) {
$lists = [];
foreach ($Trace["logisticsTraceDetailList"] as $k => $v) {
$lists[] = array(
"datetime" => date('Y-m-d H:i:s', $v["time"] / 1000),
"remark" => $v["desc"],
);
}
$result["list"] = $lists;
return $result;
}
}
return $list;
}
/**
* 物流跟踪状态
* @param $state
*/
public function getStatusName($status)
{
$data = array(
0 => "在途",
1 => "揽收",
2 => "疑难",
3 => "签收",
4 => "退签",
5 => "派件",
6 => "退回",
);
$status_name = isset($data[$status]) ? $data[$status] : '';
return $status_name;
}
/***
* 获取签名
* @param $data
* @return string
*/
public function sign($data)
{
ksort($data);
$text = '';
foreach ($data as $key => $item) {
$text .= $key . $item;
}
return strtoupper(md5($this->Secret . $text . $this->Secret));
}
}