作者 何书鹏
1 个管道 的构建 通过 耗费 0 秒

快递鸟插件

<?php
namespace addons\kdniao;
use app\common\library\Menu;
use think\Addons;
/**
* 插件
*/
class Kdniao extends Addons
{
/**
* 插件安装方法
* @return bool
*/
public function install()
{
return true;
}
/**
* 插件卸载方法
* @return bool
*/
public function uninstall()
{
return true;
}
/**
* 插件启用方法
* @return bool
*/
public function enable()
{
Menu::enable('kdniao');
return true;
}
/**
* 插件禁用方法
* @return bool
*/
public function disable()
{
Menu::disable('kdniao');
return true;
}
}
... ...
if ($('.kdniao').length > 0) {
$('.kdniao').each(function () {
var code = $(this).data('code');
$(this).addClass('btn btn-xs bg-success').append('<i class="fa fa-truck"></i>' + code);
});
$('.kdniao').click(function () {
var company = $(this).data('company');
var code = $(this).data('code');
if (company && code) {
Layer.open({
type: 2,
area: ['700px', '450px'],
fixed: false, //不固定
maxmin: true,
content: '/addons/kdniao/index/query?company=' + company + '&code=' + code
});
}
});
}
\ No newline at end of file
... ...
<?php
return array(
0 =>
array(
'name' => 'EBusinessID',
'title' => '商户ID',
'type' => 'string',
'content' =>
array(),
'value' => '',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
),
1 =>
array(
'name' => 'AppKey',
'title' => 'AppKey',
'type' => 'string',
'content' =>
array(),
'value' => '',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
),
2 =>
array(
'name' => 'ReqURL',
'title' => 'API请求地址',
'type' => 'string',
'content' =>
array(),
'value' => 'http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
),
);
... ...
<?php
namespace addons\kdniao\controller;
use addons\kdniao\library\Kdniao;
use think\addons\Controller;
use think\Db;
use think\response\Json;
class Index extends Controller
{
public function index()
{
if ($this->request->isPost()) {
$code = $this->request->post('code');
$company = $this->request->post('company');
$kdniao = new Kdniao();
$wuliu = $kdniao->getOrderTracesByJson($company, $code);
if ($wuliu == -1) {
$json = [
'code' => 0,
'msg' => '未设置接口配置!请在插件管理中配置!',
'data' => '',
];
return Json($json);
}
$wuliu = json_decode($wuliu, true);
$json = [
'code' => 1,
'msg' => '获取成功',
'data' => isset($wuliu['Traces']) && count($wuliu['Traces']) ? array_reverse($wuliu['Traces']) : [['AcceptStation' => '暂无物流信息', 'AcceptTime' => date('Y-m-d H:i:s', time())]]
];
//物流信息倒序
return Json($json);
} else {
$data = Db::name('kdniao')->select();
$this->assign('data', $data);
return $this->view->fetch();
}
}
/**
* 查询
*/
public function query()
{
$code = $this->request->param('code');
$company = $this->request->param('company');
$kdniao = new Kdniao();
$wuliu = $kdniao->getOrderTracesByJson($company, $code);
$wuliu = json_decode($wuliu, true);
/*if (isset($wuliu['Traces']) && count($wuliu['Traces'])) {
$this->assign('data', array_reverse($wuliu['Traces']));
}*/
$this->assign('data', isset($wuliu['Traces']) && count($wuliu['Traces']) ? array_reverse($wuliu['Traces']) : '');
return $this->view->fetch();
}
}
... ...
name = kdniao
title = 快递鸟
intro = 快递鸟API物流即时信息查询插件
author = Coder
website = http://www.kdniao.com
version = 1.0.1
state = 1
url = /addons/kdniao.html
... ...
CREATE TABLE IF NOT EXISTS `__PREFIX__kdniao` (
`company` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
`code` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='快递鸟物流编码表';
BEGIN;
INSERT INTO `__PREFIX__kdniao` (`company`, `code`) VALUES
('顺丰速运', 'SF'),
('百世快递', 'HTKY'),
('中通快递', 'ZTO'),
('申通快递', 'STO'),
('圆通速递', 'YTO'),
('韵达速递', 'YD'),
('邮政快递包裹', 'YZPY'),
('EMS', 'EMS'),
('天天快递', 'HHTT'),
('京东快递', 'JD'),
('优速快递', 'UC'),
('德邦快递', 'DBL'),
('宅急送', 'ZJS'),
('TNT快递', 'TNT'),
('UPS', 'UPS'),
('DHL', 'DHL'),
('FEDEX联邦(国内件)', 'FEDEX'),
('FEDEX联邦(国际件)', 'FEDEX_GJ');
COMMIT;
\ No newline at end of file
... ...
<?php
namespace addons\kdniao\library;
class Kdniao
{
protected $config = [];
public function __construct($options = [])
{
if ($config = get_addon_config('kdniao')) {
$this->config = array_merge($this->config, $config);
}
$this->config = array_merge($this->config, is_array($options) ? $options : []);
}
/**
* Json方式 查询订单物流轨迹
* @param string $shipper
* @param string $code
* @return int|string
*/
public function getOrderTracesByJson($shipper, $code)
{
$eBusinessID = $this->config["EBusinessID"];
$appKey = $this->config["AppKey"];
$reqURL = $this->config["ReqURL"];
if (!$eBusinessID || !$appKey || !$reqURL) {
return -1;
}
$requestData = "{'OrderCode':'','ShipperCode':'$shipper','LogisticCode':'$code'}";
$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 string 响应返回的html
*/
protected 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 string 内容
* @param string Appkey
* @return string DataSign签名
*/
protected function encrypt($data, $appkey)
{
return urlencode(base64_encode(md5($data . $appkey)));
}
}
... ...
<!DOCTYPE html>
<html>
<head>
<title>快递鸟物流即时信息查询 - {$site.name}</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap Core CSS -->
<link rel="stylesheet" href="__CDN__/assets/libs/bootstrap/dist/css/bootstrap.min.css">
<style>
.track_list {
padding: 10px;
list-style: none;
}
.track_list li {
font-size: 14px;
border-left: 1px solid #dcdcdc;
padding-bottom: 10px;
padding-left: 15px;
position: relative
}
.track_list li p.time {
font-size: 11px;
padding-bottom: 10px;
border-bottom: 1px solid #e7e7e7
}
.track_list li p:first-child {
padding-bottom: 5px
}
.track_list li.now p {
color: #00bb42
}
.track_list li.old p {
color: #9d9d9d
}
.track_list li.now .ico {
display: block;
position: absolute;
left: -8px;
top: 0;
width: 16px;
height: 16px;
background-color: #b0ffd4;
border-radius: 8px
}
.track_list li.now .ico i {
display: block;
width: 12px;
height: 12px;
background-color: #00aa2c;
border-radius: 6px;
position: absolute;
left: 2px;
top: 2px
}
.track_list li.old .ico {
display: block;
width: 10px;
height: 10px;
background-color: #dcdcdc;
border-radius: 5px;
position: absolute;
left: -5px;
top: 4px
}
.track_list li:last-child {
border-left: 0
}
.track_list li:last-child .time {
border-bottom: 0
}
.track_list li:last-child.old .ico {
left: -4px;
top: 0
}
.logistics_track .nofound {
padding: 30px 10px;
line-height: 20px;
font-size: 12px;
color: #9b9b9b
}
.track_list li p a {
color: #0d90ff
}
</style>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<h2>快递鸟物流即时信息查询</h2>
<hr>
<form action="" class="form-horizontal">
<fieldset>
<div class="form-group">
<label class="col-lg-1 control-label">物流公司</label>
<div class="col-lg-11">
<select id="company" name="company" class="form-control">
<option value="">--请选择--</option>
{volist name="data" id="vo"}
<option value="{$vo.code}">{$vo.company}</option>
{/volist}
</select>
</div>
</div>
<div class="form-group">
<label class="col-lg-1 control-label">快递编号</label>
<div class="col-lg-11">
<input type="text" class="form-control" id="code" name="code" placeholder="快递编号">
</div>
</div>
<div class="form-group">
<div class="col-lg-11 col-lg-offset-1">
<button type="button" id="btnsend" name="btnsend" class="btn btn-primary">发送</button>
<button type="reset" class="btn btn-default">重置</button>
</div>
</div>
</fieldset>
</form>
<div class="well">
<div class="row">
<blockquote>物流数据:</blockquote>
<ul class="track_list">
</ul>
</div>
</div>
</div>
<!-- jQuery -->
<script src="__CDN__/assets/libs/jquery/dist/jquery.min.js"></script>
<script type="text/javascript">
$("#btnsend").click(function () {
$.ajax({
type: "post",
url: "{:addon_url('kdniao/index/index')}",
data: {company: $("#company").val(), code: $("#code").val(), r: Math.random()},
success: function (data) {
console.log(data);
$('.track_list').html('');
if (data.code) {
$.each(data.data, function (i, n) {
var $li = $('<li class="' + (i == 0 ? 'now' : 'old') + '">' +
'<p>' + n.AcceptStation + '</p>' +
'<p>' + n.AcceptTime + '</p>' +
'<span class="ico"><i></i></span>' +
'</li>');
$('.track_list').append($li);
});
} else {
var $li = $('<li>' +
'<b>' + data.msg + '</b>' +
'</li>');
$('.track_list').append($li);
}
},
error: function () {
alert('查询失败');
},
complete: function () {
}
});
});
</script>
</body>
</html>
... ...
<!DOCTYPE html>
<html>
<head>
<title>快递鸟物流即时信息查询 - {$site.name}</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap Core CSS -->
<link rel="stylesheet" href="__CDN__/assets/libs/bootstrap/dist/css/bootstrap.min.css">
<style>
.track_list {
padding: 10px;
list-style: none;
}
.track_list li {
font-size: 14px;
border-left: 1px solid #dcdcdc;
padding-bottom: 10px;
padding-left: 15px;
position: relative
}
.track_list li p.time {
font-size: 11px;
padding-bottom: 10px;
border-bottom: 1px solid #e7e7e7
}
.track_list li p:first-child {
padding-bottom: 5px
}
.track_list li.now p {
color: #00bb42
}
.track_list li.old p {
color: #9d9d9d
}
.track_list li.now .ico {
display: block;
position: absolute;
left: -8px;
top: 0;
width: 16px;
height: 16px;
background-color: #b0ffd4;
border-radius: 8px
}
.track_list li.now .ico i {
display: block;
width: 12px;
height: 12px;
background-color: #00aa2c;
border-radius: 6px;
position: absolute;
left: 2px;
top: 2px
}
.track_list li.old .ico {
display: block;
width: 10px;
height: 10px;
background-color: #dcdcdc;
border-radius: 5px;
position: absolute;
left: -5px;
top: 4px
}
.track_list li:last-child {
border-left: 0
}
.track_list li:last-child .time {
border-bottom: 0
}
.track_list li:last-child.old .ico {
left: -4px;
top: 0
}
.logistics_track .nofound {
padding: 30px 10px;
line-height: 20px;
font-size: 12px;
color: #9b9b9b
}
.track_list li p a {
color: #0d90ff
}
</style>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="well">
<div class="row">
<blockquote>物流数据:</blockquote>
<ul class="track_list">
{volist name="data" id="item" empty="暂无物流信息"}
<li class='{if condition="$i eq 1"}now{else/}old{/if}'>
<p>{$item.AcceptStation}</p>
<p>{$item.AcceptTime}</p>
<span class="ico"><i></i></span>
</li>
{/volist}
</ul>
</div>
</div>
</div>
<!-- jQuery -->
<script src="__CDN__/assets/libs/jquery/dist/jquery.min.js"></script>
</body>
</html>
... ...
... ... @@ -12,6 +12,29 @@ define([], function () {
}
});
if ($('.kdniao').length > 0) {
$('.kdniao').each(function () {
var code = $(this).data('code');
$(this).addClass('btn btn-xs bg-success').append('<i class="fa fa-truck"></i>' + code);
});
$('.kdniao').click(function () {
var company = $(this).data('company');
var code = $(this).data('code');
if (company && code) {
Layer.open({
type: 2,
area: ['700px', '450px'],
fixed: false, //不固定
maxmin: true,
content: '/addons/kdniao/index/query?company=' + company + '&code=' + code
});
}
});
}
//修改上传的接口调用
require(['upload'], function (Upload) {
var _onUploadResponse = Upload.events.onUploadResponse;
... ...