123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2018/8/9
- * Time: 下午7:44
- */
- require_once (BASE_CORE_PATH . "/framework/function/http.php");
- class kdn_helper
- {
- private static $EBusinessID = 1366783;
- private static $AppKey = "e62955d6-4871-4740-9ec9-62068a544ab5";
- private static $ReqURL = "http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
- //const TRACEAPI = "http://api.kdniao.cc/api/dis";
- public static function cur_businessid()
- {
- return self::$EBusinessID;
- }
- public static function query($shipperCode, $logisticCode)
- {
- $requestData = json_encode(array('ShipperCode' => $shipperCode,'LogisticCode' => $logisticCode));
- Log::record("EBusinessID=". self::$EBusinessID,Log::DEBUG);
- $datas = array(
- 'EBusinessID' => self::$EBusinessID,
- 'RequestType' => '8001',
- 'RequestData' => urlencode($requestData),
- 'DataType' => '2',
- 'DataSign' => self::encrypt($requestData, self::$AppKey)
- );
- $result = http_post_data(self::$ReqURL,$datas);
- return $result;
- }
- public static function onCallback($data)
- {
- $order_sn = $data['CallBack'];
- if(empty($order_sn)) return;
- $e_code = $data['ShipperCode'];
- $shipping_code = $data['LogisticCode'];
- $key = "express_{$e_code}_{$shipping_code}";
- wkcache($key, $data);
- // 数据解析
- switch ($data['State'])
- {
- case '1': // 已揽收
- Log::record(__METHOD__ . " order_sn={$order_sn},code = {$data['LogisticCode']} 已揽收.",Log::DEBUG);
- break;
- case '2': // 在途中
- Log::record(__METHOD__ . " order_sn={$order_sn},code = {$data['LogisticCode']} 在途中.",Log::DEBUG);
- break;
- case '3': // 已签收, 设置为已收货
- Log::record(__METHOD__ . " order_sn={$order_sn},code = {$data['LogisticCode']} 已签收.",Log::DEBUG);
- break;
- case '4': // 问题件
- Log::record(__METHOD__ . " order_sn={$order_sn},code = {$data['LogisticCode']} 问题件.",Log::ERR);
- break;
- default: {
- Log::record(__METHOD__ . ' query_status false');
- }
- }
- }
- public static function subscribe($order_sn)
- {
- $model_order = Model('order');
- $condition['order_sn'] = $order_sn;
- $order_info = $model_order->getOrderInfo($condition, array('order_common', 'order_goods'));
- // 发送快递鸟订阅回调
- $express = rkcache('express', true);
- $e_code = $express[$order_info['extend_order_common']['shipping_express_id']]['e_kdn_code'];
- if(empty($e_code)) {
- Log::record("找不到该单号的快递公司编码—— ordersn={$order_sn}",Log::ERR);
- return;
- }
- // 通知订阅, 快递鸟定时回调
- $ret = self::req_subscribe($e_code, $order_info['shipping_code'], $order_sn);
- if($ret === false) {
- Log::record("kdn_helper::subscribe order_sn={$order_sn} error.",Log::ERR);
- } else {
- Log::record("kdn_helper::subscribe ret={$ret}",Log::DEBUG);
- }
- }
- public static function req_subscribe($e_code, $logisticCode, $order_sn)
- {
- $datas = [
- 'ShipperCode' => $e_code,
- 'LogisticCode' => $logisticCode,
- 'Callback' => $order_sn
- ];
- $requestData = json_encode($datas);
- $datas = array(
- 'EBusinessID' => self::$EBusinessID,
- 'RequestType' => '8008',
- 'RequestData' => urlencode($requestData),
- 'DataType' => '2',
- 'DataSign' => self::encrypt($requestData, self::$AppKey)
- );
- $result = http_post_data(self::$ReqURL, $datas);
- return $result;
- }
- private static function encrypt($data, $appkey)
- {
- return urlencode(base64_encode(md5($data . $appkey)));
- }
- }
|