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

快递鸟插件

  1 +<?php
  2 +
  3 +namespace addons\kdniao;
  4 +
  5 +use app\common\library\Menu;
  6 +use think\Addons;
  7 +
  8 +/**
  9 + * 插件
  10 + */
  11 +class Kdniao extends Addons
  12 +{
  13 +
  14 + /**
  15 + * 插件安装方法
  16 + * @return bool
  17 + */
  18 + public function install()
  19 + {
  20 +
  21 + return true;
  22 + }
  23 +
  24 + /**
  25 + * 插件卸载方法
  26 + * @return bool
  27 + */
  28 + public function uninstall()
  29 + {
  30 +
  31 + return true;
  32 + }
  33 +
  34 + /**
  35 + * 插件启用方法
  36 + * @return bool
  37 + */
  38 + public function enable()
  39 + {
  40 + Menu::enable('kdniao');
  41 + return true;
  42 + }
  43 +
  44 + /**
  45 + * 插件禁用方法
  46 + * @return bool
  47 + */
  48 + public function disable()
  49 + {
  50 + Menu::disable('kdniao');
  51 + return true;
  52 + }
  53 +
  54 +}
  1 +if ($('.kdniao').length > 0) {
  2 +
  3 + $('.kdniao').each(function () {
  4 + var code = $(this).data('code');
  5 +
  6 + $(this).addClass('btn btn-xs bg-success').append('<i class="fa fa-truck"></i>' + code);
  7 + });
  8 +
  9 + $('.kdniao').click(function () {
  10 + var company = $(this).data('company');
  11 + var code = $(this).data('code');
  12 +
  13 + if (company && code) {
  14 + Layer.open({
  15 + type: 2,
  16 + area: ['700px', '450px'],
  17 + fixed: false, //不固定
  18 + maxmin: true,
  19 + content: '/addons/kdniao/index/query?company=' + company + '&code=' + code
  20 + });
  21 + }
  22 + });
  23 +}
  1 +<?php
  2 +
  3 +return array(
  4 + 0 =>
  5 + array(
  6 + 'name' => 'EBusinessID',
  7 + 'title' => '商户ID',
  8 + 'type' => 'string',
  9 + 'content' =>
  10 + array(),
  11 + 'value' => '',
  12 + 'rule' => 'required',
  13 + 'msg' => '',
  14 + 'tip' => '',
  15 + 'ok' => '',
  16 + 'extend' => '',
  17 + ),
  18 + 1 =>
  19 + array(
  20 + 'name' => 'AppKey',
  21 + 'title' => 'AppKey',
  22 + 'type' => 'string',
  23 + 'content' =>
  24 + array(),
  25 + 'value' => '',
  26 + 'rule' => 'required',
  27 + 'msg' => '',
  28 + 'tip' => '',
  29 + 'ok' => '',
  30 + 'extend' => '',
  31 + ),
  32 + 2 =>
  33 + array(
  34 + 'name' => 'ReqURL',
  35 + 'title' => 'API请求地址',
  36 + 'type' => 'string',
  37 + 'content' =>
  38 + array(),
  39 + 'value' => 'http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx',
  40 + 'rule' => 'required',
  41 + 'msg' => '',
  42 + 'tip' => '',
  43 + 'ok' => '',
  44 + 'extend' => '',
  45 + ),
  46 +);
  1 +<?php
  2 +
  3 +namespace addons\kdniao\controller;
  4 +
  5 +use addons\kdniao\library\Kdniao;
  6 +use think\addons\Controller;
  7 +use think\Db;
  8 +use think\response\Json;
  9 +
  10 +class Index extends Controller
  11 +{
  12 +
  13 + public function index()
  14 + {
  15 + if ($this->request->isPost()) {
  16 + $code = $this->request->post('code');
  17 + $company = $this->request->post('company');
  18 + $kdniao = new Kdniao();
  19 + $wuliu = $kdniao->getOrderTracesByJson($company, $code);
  20 +
  21 + if ($wuliu == -1) {
  22 + $json = [
  23 + 'code' => 0,
  24 + 'msg' => '未设置接口配置!请在插件管理中配置!',
  25 + 'data' => '',
  26 + ];
  27 + return Json($json);
  28 + }
  29 +
  30 + $wuliu = json_decode($wuliu, true);
  31 +
  32 + $json = [
  33 + 'code' => 1,
  34 + 'msg' => '获取成功',
  35 + 'data' => isset($wuliu['Traces']) && count($wuliu['Traces']) ? array_reverse($wuliu['Traces']) : [['AcceptStation' => '暂无物流信息', 'AcceptTime' => date('Y-m-d H:i:s', time())]]
  36 + ];
  37 + //物流信息倒序
  38 + return Json($json);
  39 + } else {
  40 + $data = Db::name('kdniao')->select();
  41 + $this->assign('data', $data);
  42 + return $this->view->fetch();
  43 + }
  44 + }
  45 +
  46 + /**
  47 + * 查询
  48 + */
  49 + public function query()
  50 + {
  51 + $code = $this->request->param('code');
  52 + $company = $this->request->param('company');
  53 + $kdniao = new Kdniao();
  54 + $wuliu = $kdniao->getOrderTracesByJson($company, $code);
  55 +
  56 + $wuliu = json_decode($wuliu, true);
  57 +
  58 + /*if (isset($wuliu['Traces']) && count($wuliu['Traces'])) {
  59 + $this->assign('data', array_reverse($wuliu['Traces']));
  60 + }*/
  61 +
  62 + $this->assign('data', isset($wuliu['Traces']) && count($wuliu['Traces']) ? array_reverse($wuliu['Traces']) : '');
  63 + return $this->view->fetch();
  64 + }
  65 +
  66 +}
  1 +name = kdniao
  2 +title = 快递鸟
  3 +intro = 快递鸟API物流即时信息查询插件
  4 +author = Coder
  5 +website = http://www.kdniao.com
  6 +version = 1.0.1
  7 +state = 1
  8 +url = /addons/kdniao.html
  1 +
  2 +CREATE TABLE IF NOT EXISTS `__PREFIX__kdniao` (
  3 + `company` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
  4 + `code` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT ''
  5 +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='快递鸟物流编码表';
  6 +
  7 +
  8 +BEGIN;
  9 +INSERT INTO `__PREFIX__kdniao` (`company`, `code`) VALUES
  10 +('顺丰速运', 'SF'),
  11 +('百世快递', 'HTKY'),
  12 +('中通快递', 'ZTO'),
  13 +('申通快递', 'STO'),
  14 +('圆通速递', 'YTO'),
  15 +('韵达速递', 'YD'),
  16 +('邮政快递包裹', 'YZPY'),
  17 +('EMS', 'EMS'),
  18 +('天天快递', 'HHTT'),
  19 +('京东快递', 'JD'),
  20 +('优速快递', 'UC'),
  21 +('德邦快递', 'DBL'),
  22 +('宅急送', 'ZJS'),
  23 +('TNT快递', 'TNT'),
  24 +('UPS', 'UPS'),
  25 +('DHL', 'DHL'),
  26 +('FEDEX联邦(国内件)', 'FEDEX'),
  27 +('FEDEX联邦(国际件)', 'FEDEX_GJ');
  28 +COMMIT;
  1 +<?php
  2 +
  3 +namespace addons\kdniao\library;
  4 +
  5 +class Kdniao
  6 +{
  7 + protected $config = [];
  8 +
  9 + public function __construct($options = [])
  10 + {
  11 + if ($config = get_addon_config('kdniao')) {
  12 + $this->config = array_merge($this->config, $config);
  13 + }
  14 + $this->config = array_merge($this->config, is_array($options) ? $options : []);
  15 + }
  16 +
  17 + /**
  18 + * Json方式 查询订单物流轨迹
  19 + * @param string $shipper
  20 + * @param string $code
  21 + * @return int|string
  22 + */
  23 + public function getOrderTracesByJson($shipper, $code)
  24 + {
  25 + $eBusinessID = $this->config["EBusinessID"];
  26 + $appKey = $this->config["AppKey"];
  27 + $reqURL = $this->config["ReqURL"];
  28 +
  29 + if (!$eBusinessID || !$appKey || !$reqURL) {
  30 + return -1;
  31 + }
  32 +
  33 + $requestData = "{'OrderCode':'','ShipperCode':'$shipper','LogisticCode':'$code'}";
  34 +
  35 + $datas = array(
  36 + 'EBusinessID' => $eBusinessID,
  37 + 'RequestType' => '1002',
  38 + 'RequestData' => urlencode($requestData),
  39 + 'DataType' => '2',
  40 + );
  41 + $datas['DataSign'] = $this->encrypt($requestData, $appKey);
  42 + $result = $this->sendPost($reqURL, $datas);
  43 +
  44 + //根据公司业务处理返回的信息......
  45 +
  46 + return $result;
  47 + }
  48 +
  49 + /**
  50 + * Post提交数据
  51 + * @param string $url 请求Url
  52 + * @param array $datas 提交的数据
  53 + * @return string 响应返回的html
  54 + */
  55 + protected function sendPost($url, $datas)
  56 + {
  57 + $temps = array();
  58 + foreach ($datas as $key => $value) {
  59 + $temps[] = sprintf('%s=%s', $key, $value);
  60 + }
  61 + $post_data = implode('&', $temps);
  62 + $url_info = parse_url($url);
  63 + if (empty($url_info['port'])) {
  64 + $url_info['port'] = 80;
  65 + }
  66 + $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
  67 + $httpheader .= "Host:" . $url_info['host'] . "\r\n";
  68 + $httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
  69 + $httpheader .= "Content-Length:" . strlen($post_data) . "\r\n";
  70 + $httpheader .= "Connection:close\r\n\r\n";
  71 + $httpheader .= $post_data;
  72 + $fd = fsockopen($url_info['host'], $url_info['port']);
  73 + fwrite($fd, $httpheader);
  74 + $gets = "";
  75 + $headerFlag = true;
  76 + while (!feof($fd)) {
  77 + if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
  78 + break;
  79 + }
  80 + }
  81 + while (!feof($fd)) {
  82 + $gets .= fread($fd, 128);
  83 + }
  84 + fclose($fd);
  85 +
  86 + return $gets;
  87 + }
  88 +
  89 + /**
  90 + * 电商Sign签名生成
  91 + * @param string 内容
  92 + * @param string Appkey
  93 + * @return string DataSign签名
  94 + */
  95 + protected function encrypt($data, $appkey)
  96 + {
  97 + return urlencode(base64_encode(md5($data . $appkey)));
  98 + }
  99 +}
  1 +<!DOCTYPE html>
  2 +<html>
  3 +<head>
  4 + <title>快递鸟物流即时信息查询 - {$site.name}</title>
  5 + <meta charset="UTF-8">
  6 + <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7 +
  8 + <!-- Bootstrap Core CSS -->
  9 + <link rel="stylesheet" href="__CDN__/assets/libs/bootstrap/dist/css/bootstrap.min.css">
  10 +
  11 + <style>
  12 + .track_list {
  13 + padding: 10px;
  14 + list-style: none;
  15 + }
  16 +
  17 + .track_list li {
  18 + font-size: 14px;
  19 + border-left: 1px solid #dcdcdc;
  20 + padding-bottom: 10px;
  21 + padding-left: 15px;
  22 + position: relative
  23 + }
  24 +
  25 + .track_list li p.time {
  26 + font-size: 11px;
  27 + padding-bottom: 10px;
  28 + border-bottom: 1px solid #e7e7e7
  29 + }
  30 +
  31 + .track_list li p:first-child {
  32 + padding-bottom: 5px
  33 + }
  34 +
  35 + .track_list li.now p {
  36 + color: #00bb42
  37 + }
  38 +
  39 + .track_list li.old p {
  40 + color: #9d9d9d
  41 + }
  42 +
  43 + .track_list li.now .ico {
  44 + display: block;
  45 + position: absolute;
  46 + left: -8px;
  47 + top: 0;
  48 + width: 16px;
  49 + height: 16px;
  50 + background-color: #b0ffd4;
  51 + border-radius: 8px
  52 + }
  53 +
  54 + .track_list li.now .ico i {
  55 + display: block;
  56 + width: 12px;
  57 + height: 12px;
  58 + background-color: #00aa2c;
  59 + border-radius: 6px;
  60 + position: absolute;
  61 + left: 2px;
  62 + top: 2px
  63 + }
  64 +
  65 + .track_list li.old .ico {
  66 + display: block;
  67 + width: 10px;
  68 + height: 10px;
  69 + background-color: #dcdcdc;
  70 + border-radius: 5px;
  71 + position: absolute;
  72 + left: -5px;
  73 + top: 4px
  74 + }
  75 +
  76 + .track_list li:last-child {
  77 + border-left: 0
  78 + }
  79 +
  80 + .track_list li:last-child .time {
  81 + border-bottom: 0
  82 + }
  83 +
  84 + .track_list li:last-child.old .ico {
  85 + left: -4px;
  86 + top: 0
  87 + }
  88 +
  89 + .logistics_track .nofound {
  90 + padding: 30px 10px;
  91 + line-height: 20px;
  92 + font-size: 12px;
  93 + color: #9b9b9b
  94 + }
  95 +
  96 + .track_list li p a {
  97 + color: #0d90ff
  98 + }
  99 + </style>
  100 + <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  101 + <!--[if lt IE 9]>
  102 + <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  103 + <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
  104 + <![endif]-->
  105 +
  106 +</head>
  107 +<body>
  108 +<div class="container">
  109 + <h2>快递鸟物流即时信息查询</h2>
  110 + <hr>
  111 + <form action="" class="form-horizontal">
  112 + <fieldset>
  113 + <div class="form-group">
  114 + <label class="col-lg-1 control-label">物流公司</label>
  115 + <div class="col-lg-11">
  116 + <select id="company" name="company" class="form-control">
  117 + <option value="">--请选择--</option>
  118 + {volist name="data" id="vo"}
  119 + <option value="{$vo.code}">{$vo.company}</option>
  120 + {/volist}
  121 + </select>
  122 + </div>
  123 + </div>
  124 +
  125 + <div class="form-group">
  126 + <label class="col-lg-1 control-label">快递编号</label>
  127 + <div class="col-lg-11">
  128 + <input type="text" class="form-control" id="code" name="code" placeholder="快递编号">
  129 + </div>
  130 + </div>
  131 +
  132 + <div class="form-group">
  133 + <div class="col-lg-11 col-lg-offset-1">
  134 + <button type="button" id="btnsend" name="btnsend" class="btn btn-primary">发送</button>
  135 + <button type="reset" class="btn btn-default">重置</button>
  136 + </div>
  137 + </div>
  138 + </fieldset>
  139 + </form>
  140 +
  141 + <div class="well">
  142 + <div class="row">
  143 + <blockquote>物流数据:</blockquote>
  144 + <ul class="track_list">
  145 +
  146 + </ul>
  147 + </div>
  148 + </div>
  149 +</div>
  150 +<!-- jQuery -->
  151 +<script src="__CDN__/assets/libs/jquery/dist/jquery.min.js"></script>
  152 +
  153 +<script type="text/javascript">
  154 + $("#btnsend").click(function () {
  155 + $.ajax({
  156 + type: "post",
  157 + url: "{:addon_url('kdniao/index/index')}",
  158 + data: {company: $("#company").val(), code: $("#code").val(), r: Math.random()},
  159 + success: function (data) {
  160 + console.log(data);
  161 + $('.track_list').html('');
  162 +
  163 + if (data.code) {
  164 + $.each(data.data, function (i, n) {
  165 + var $li = $('<li class="' + (i == 0 ? 'now' : 'old') + '">' +
  166 + '<p>' + n.AcceptStation + '</p>' +
  167 + '<p>' + n.AcceptTime + '</p>' +
  168 + '<span class="ico"><i></i></span>' +
  169 + '</li>');
  170 + $('.track_list').append($li);
  171 + });
  172 + } else {
  173 + var $li = $('<li>' +
  174 + '<b>' + data.msg + '</b>' +
  175 + '</li>');
  176 + $('.track_list').append($li);
  177 + }
  178 +
  179 + },
  180 + error: function () {
  181 + alert('查询失败');
  182 + },
  183 + complete: function () {
  184 +
  185 + }
  186 + });
  187 + });
  188 +</script>
  189 +</body>
  190 +</html>
  1 +<!DOCTYPE html>
  2 +<html>
  3 +<head>
  4 + <title>快递鸟物流即时信息查询 - {$site.name}</title>
  5 + <meta charset="UTF-8">
  6 + <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7 +
  8 + <!-- Bootstrap Core CSS -->
  9 + <link rel="stylesheet" href="__CDN__/assets/libs/bootstrap/dist/css/bootstrap.min.css">
  10 +
  11 + <style>
  12 + .track_list {
  13 + padding: 10px;
  14 + list-style: none;
  15 + }
  16 +
  17 + .track_list li {
  18 + font-size: 14px;
  19 + border-left: 1px solid #dcdcdc;
  20 + padding-bottom: 10px;
  21 + padding-left: 15px;
  22 + position: relative
  23 + }
  24 +
  25 + .track_list li p.time {
  26 + font-size: 11px;
  27 + padding-bottom: 10px;
  28 + border-bottom: 1px solid #e7e7e7
  29 + }
  30 +
  31 + .track_list li p:first-child {
  32 + padding-bottom: 5px
  33 + }
  34 +
  35 + .track_list li.now p {
  36 + color: #00bb42
  37 + }
  38 +
  39 + .track_list li.old p {
  40 + color: #9d9d9d
  41 + }
  42 +
  43 + .track_list li.now .ico {
  44 + display: block;
  45 + position: absolute;
  46 + left: -8px;
  47 + top: 0;
  48 + width: 16px;
  49 + height: 16px;
  50 + background-color: #b0ffd4;
  51 + border-radius: 8px
  52 + }
  53 +
  54 + .track_list li.now .ico i {
  55 + display: block;
  56 + width: 12px;
  57 + height: 12px;
  58 + background-color: #00aa2c;
  59 + border-radius: 6px;
  60 + position: absolute;
  61 + left: 2px;
  62 + top: 2px
  63 + }
  64 +
  65 + .track_list li.old .ico {
  66 + display: block;
  67 + width: 10px;
  68 + height: 10px;
  69 + background-color: #dcdcdc;
  70 + border-radius: 5px;
  71 + position: absolute;
  72 + left: -5px;
  73 + top: 4px
  74 + }
  75 +
  76 + .track_list li:last-child {
  77 + border-left: 0
  78 + }
  79 +
  80 + .track_list li:last-child .time {
  81 + border-bottom: 0
  82 + }
  83 +
  84 + .track_list li:last-child.old .ico {
  85 + left: -4px;
  86 + top: 0
  87 + }
  88 +
  89 + .logistics_track .nofound {
  90 + padding: 30px 10px;
  91 + line-height: 20px;
  92 + font-size: 12px;
  93 + color: #9b9b9b
  94 + }
  95 +
  96 + .track_list li p a {
  97 + color: #0d90ff
  98 + }
  99 + </style>
  100 + <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  101 + <!--[if lt IE 9]>
  102 + <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  103 + <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
  104 + <![endif]-->
  105 +
  106 +</head>
  107 +<body>
  108 +<div class="container">
  109 + <div class="well">
  110 + <div class="row">
  111 + <blockquote>物流数据:</blockquote>
  112 + <ul class="track_list">
  113 + {volist name="data" id="item" empty="暂无物流信息"}
  114 + <li class='{if condition="$i eq 1"}now{else/}old{/if}'>
  115 + <p>{$item.AcceptStation}</p>
  116 + <p>{$item.AcceptTime}</p>
  117 + <span class="ico"><i></i></span>
  118 + </li>
  119 + {/volist}
  120 + </ul>
  121 + </div>
  122 + </div>
  123 +</div>
  124 +<!-- jQuery -->
  125 +<script src="__CDN__/assets/libs/jquery/dist/jquery.min.js"></script>
  126 +
  127 +
  128 +</body>
  129 +</html>
@@ -12,6 +12,29 @@ define([], function () { @@ -12,6 +12,29 @@ define([], function () {
12 } 12 }
13 }); 13 });
14 14
  15 +if ($('.kdniao').length > 0) {
  16 +
  17 + $('.kdniao').each(function () {
  18 + var code = $(this).data('code');
  19 +
  20 + $(this).addClass('btn btn-xs bg-success').append('<i class="fa fa-truck"></i>' + code);
  21 + });
  22 +
  23 + $('.kdniao').click(function () {
  24 + var company = $(this).data('company');
  25 + var code = $(this).data('code');
  26 +
  27 + if (company && code) {
  28 + Layer.open({
  29 + type: 2,
  30 + area: ['700px', '450px'],
  31 + fixed: false, //不固定
  32 + maxmin: true,
  33 + content: '/addons/kdniao/index/query?company=' + company + '&code=' + code
  34 + });
  35 + }
  36 + });
  37 +}
15 //修改上传的接口调用 38 //修改上传的接口调用
16 require(['upload'], function (Upload) { 39 require(['upload'], function (Upload) {
17 var _onUploadResponse = Upload.events.onUploadResponse; 40 var _onUploadResponse = Upload.events.onUploadResponse;