228 行
7.4 KiB
PHP
可执行文件
228 行
7.4 KiB
PHP
可执行文件
<?php
|
||
|
||
namespace Addons\Api;
|
||
|
||
//[1.3.89]
|
||
/**
|
||
* 菜鸟物流云
|
||
* Class CNLogistics
|
||
* @package Addons\Api
|
||
*/
|
||
class CNLogistics
|
||
{
|
||
private $appKey;
|
||
private $appSecret;//菜鸟AppSecret
|
||
private $logisticProviderId;//token(申请成功后菜鸟给的token)
|
||
private $cpCode;// 物流编号 - YTO
|
||
|
||
/**
|
||
* 初始化配置
|
||
* CNLogistics constructor.
|
||
* @param $token 授权token
|
||
* @param string $cp_code 物流编号
|
||
*/
|
||
public function __construct($token, $cp_code = '')
|
||
{
|
||
$this->logisticProviderId = $token;
|
||
$this->cpCode = $cp_code;
|
||
$this->appKey = C('CN_APP_KEY');
|
||
$this->appSecret = C('CN_APP_SECRET');
|
||
}
|
||
|
||
/**
|
||
* 获取发货地,CP开通状态,账户的使用情况
|
||
* @return bool|string
|
||
*/
|
||
public function getMerInfo()
|
||
{
|
||
$type = 'TMS_WAYBILL_SUBSCRIPTION_QUERY';
|
||
$requestData = array(
|
||
'cpCode' => $this->cpCode,
|
||
);
|
||
$res = $this->sendRequest($requestData, $type);
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* 电子面单取号接口
|
||
* @param array $sender 发货人信息
|
||
* @param array $recipient 收货人信息
|
||
* @param null $orderCode 订单号(能重复)
|
||
* @return mixed
|
||
*/
|
||
public function getExpressCode($sender = array(), $recipient = array(), $orderCode = null)
|
||
{
|
||
/**
|
||
* - orderCode不能重复
|
||
* - recipient数据必须与为“TMS_WAYBILL_SUBSCRIPTION_QUERY”获取数据
|
||
* - 发货人/收货人,地址/联系人/联系方式不能为空
|
||
*/
|
||
// 请求方式
|
||
$type = "TMS_WAYBILL_GET";
|
||
//请求内容
|
||
$requestData = array(
|
||
'cpCode' => $this->cpCode,
|
||
'sender' => array(
|
||
'address' => array(
|
||
'city' => $sender['city'],
|
||
'detail' => $sender['detail'],//true
|
||
'district' => $sender['district'],
|
||
'province' => $sender['province'],//true
|
||
'town' => $sender['town'],
|
||
),
|
||
'mobile' => $sender['mobile'],
|
||
'name' => $sender['name'],//true
|
||
'phone' => $sender['phone'],
|
||
),
|
||
'tradeOrderInfoDtos' => array(
|
||
'tradeOrderInfoDto' => array(
|
||
'logisticsServices' => '',
|
||
'objectId' => $orderCode,
|
||
'orderInfo' => array(
|
||
'orderChannelsType' => 'OTHERS',//订单渠道平台,请按实际订单所属平台传入(详见https://support-cnkuaidi.taobao.com/doc.htm#?docId=105085&docType=1)
|
||
'tradeOrderList' => array(
|
||
0 => $orderCode,
|
||
),//订单号列表(上限100个)
|
||
),
|
||
'packageInfo' => array(
|
||
'id' => $orderCode,
|
||
'items' => array(
|
||
0 => array(
|
||
'count' => intval($sender['goods_count']),
|
||
'name' => $sender['goods_name'],
|
||
),
|
||
),
|
||
'volume' => $sender['goods_volume'],//体积, 单位 ml
|
||
'weight' => $sender['goods_weight'],//重量,单位 g
|
||
),//包裹信息
|
||
'recipient' => array(
|
||
'address' => array(
|
||
'city' => $recipient['city'],
|
||
'detail' => $recipient['detail'],
|
||
'district' => $recipient['district'],
|
||
'province' => $recipient['province'],
|
||
'town' => $recipient['town'],
|
||
),
|
||
'mobile' => $recipient['mobile'],
|
||
'name' => $recipient['name'],
|
||
'phone' => $recipient['phone'],
|
||
),//收货人信息
|
||
'templateUrl' => $sender['template_url'],
|
||
'userId' => $sender['user_id'],
|
||
),
|
||
),
|
||
'storeCode' => '无',
|
||
'resourceCode' => '无',
|
||
'dmsSorting' => 'false',
|
||
'needEncrypt' => 'false',
|
||
);
|
||
// print_r($requestData);
|
||
// exit;
|
||
$res = $this->sendRequest($requestData, $type);
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* 电子面单取消
|
||
* @param $waybillCode 电子面单号
|
||
* @return mixed
|
||
*/
|
||
public function cancelExpress($waybillCode)
|
||
{
|
||
$type = 'TMS_WAYBILL_DISCARD';
|
||
$requestData = array(
|
||
'cpCode' => $this->cpCode,
|
||
'waybillCode' => $waybillCode,
|
||
);
|
||
$res = $this->sendRequest($requestData, $type);
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* 获取云打印标准面单
|
||
* @return bool|string
|
||
*/
|
||
public function getStandardTemplates()
|
||
{
|
||
$type = 'CLOUDPRINT_STANDARD_TEMPLATES';
|
||
$requestData = array(
|
||
'cpCode' => $this->cpCode,
|
||
);
|
||
$res = $this->sendRequest($requestData, $type);
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* 获取云打印ISV模板
|
||
* @return bool|string
|
||
*/
|
||
public function getISVTemplates()
|
||
{
|
||
$type = 'CLOUDPRINT_ISV_TEMPLATES';
|
||
$requestData = array(
|
||
'cpCode' => $this->cpCode,
|
||
'templateType' => 6,
|
||
);
|
||
$res = $this->sendRequest($requestData, $type);
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* 获取商家自定义区列表
|
||
*/
|
||
public function getMerCustomAreas()
|
||
{
|
||
$type = 'CLOUDPRINT_SELLER_CUSTOM_AREAS';
|
||
$requestData = array();
|
||
$res = $this->sendRequest($requestData, $type);
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* 发送请求
|
||
* @param $requestData 请求报文内容
|
||
* @param $type 消息类型
|
||
* @return bool|string
|
||
*/
|
||
public function sendRequest($requestData, $type)
|
||
{
|
||
$request_url = 'http://link.cainiao.com/gateway/link.do';//正式环境
|
||
$requestData = json_encode($requestData);//正式环境
|
||
$get_data = array(
|
||
"logistic_provider_id={$this->logisticProviderId}",
|
||
"msg_type={$type}",
|
||
"logistics_interface={$requestData}",
|
||
"data_digest={$this->createSign($requestData)}",
|
||
);
|
||
$get_data = implode('&', $get_data);
|
||
// print_r($get_data);
|
||
$res = http_request($request_url, $get_data);
|
||
if ($res) {
|
||
$res = json_decode($res, true);
|
||
if ($res['success'] == 'true') {
|
||
$res['status'] = 1;
|
||
$res['msg'] = '成功';
|
||
} else {
|
||
$res['status'] = 0;
|
||
$res['msg'] = $res['errorMsg'] ? $res['errorMsg'] : '失败';
|
||
}
|
||
} else {
|
||
$res = array(
|
||
'status' => 0,
|
||
'msg' => '请求错误',
|
||
);
|
||
}
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* 电商Sign签名生成
|
||
* @param data 内容
|
||
* @param appkey Appkey
|
||
* @return DataSign签名
|
||
*/
|
||
private function createSign($data)
|
||
{
|
||
return urlencode(base64_encode(md5($data . $this->appSecret, true)));
|
||
}
|
||
} |