|
|
@@ -0,0 +1,175 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace common\components;
|
|
|
+
|
|
|
+use Yii;
|
|
|
+use yii\helpers\Json;
|
|
|
+use linslin\yii2\curl;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 微信小程序物流服务工具类
|
|
|
+ * 参考微信官方文档:https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/interface_and_event.html
|
|
|
+ * 风格参考 miniUtil.php
|
|
|
+ */
|
|
|
+class expressUtil
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 获取 access_token
|
|
|
+ * @param $merchant
|
|
|
+ * @param int $ptStyle
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function getAccessToken($merchant, $ptStyle = 0)
|
|
|
+ {
|
|
|
+ // 直接复用 miniUtil 的获取小程序 access_token 方法
|
|
|
+ return miniUtil::getMiniProgramAccessToken($merchant, $ptStyle);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 绑定账号
|
|
|
+ * @param $merchant
|
|
|
+ * @param $data
|
|
|
+ * @param int $ptStyle
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public static function bindAccount($merchant, $data, $ptStyle = 0)
|
|
|
+ {
|
|
|
+ $accessToken = self::getAccessToken($merchant, $ptStyle);
|
|
|
+ $url = "https://api.weixin.qq.com/cgi-bin/express/business/account/bind?access_token={$accessToken}";
|
|
|
+ $curl = new curl\Curl();
|
|
|
+ $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
|
|
|
+ return Json::decode($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拉取已绑定账号
|
|
|
+ */
|
|
|
+ public static function getBindAccount($merchant, $ptStyle = 0)
|
|
|
+ {
|
|
|
+ $accessToken = self::getAccessToken($merchant, $ptStyle);
|
|
|
+ $url = "https://api.weixin.qq.com/cgi-bin/express/business/account/get?access_token={$accessToken}";
|
|
|
+ $curl = new curl\Curl();
|
|
|
+ $result = $curl->setOption(CURLOPT_POSTFIELDS, '{}')->post($url);
|
|
|
+ return Json::decode($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取支持的快递公司列表
|
|
|
+ */
|
|
|
+ public static function getAllDelivery($merchant, $ptStyle = 0)
|
|
|
+ {
|
|
|
+ $accessToken = self::getAccessToken($merchant, $ptStyle);
|
|
|
+ $url = "https://api.weixin.qq.com/cgi-bin/express/business/delivery/getall?access_token={$accessToken}";
|
|
|
+ $curl = new curl\Curl();
|
|
|
+ $result = $curl->setOption(CURLOPT_POSTFIELDS, '{}')->post($url);
|
|
|
+ return Json::decode($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成运单
|
|
|
+ */
|
|
|
+ public static function addOrder($merchant, $data, $ptStyle = 0)
|
|
|
+ {
|
|
|
+ $accessToken = self::getAccessToken($merchant, $ptStyle);
|
|
|
+ $url = "https://api.weixin.qq.com/cgi-bin/express/business/order/add?access_token={$accessToken}";
|
|
|
+ $curl = new curl\Curl();
|
|
|
+ $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
|
|
|
+ return Json::decode($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取消运单
|
|
|
+ */
|
|
|
+ public static function cancelOrder($merchant, $data, $ptStyle = 0)
|
|
|
+ {
|
|
|
+ $accessToken = self::getAccessToken($merchant, $ptStyle);
|
|
|
+ $url = "https://api.weixin.qq.com/cgi-bin/express/business/order/cancel?access_token={$accessToken}";
|
|
|
+ $curl = new curl\Curl();
|
|
|
+ $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
|
|
|
+ return Json::decode($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取运单数据
|
|
|
+ */
|
|
|
+ public static function getOrder($merchant, $data, $ptStyle = 0)
|
|
|
+ {
|
|
|
+ $accessToken = self::getAccessToken($merchant, $ptStyle);
|
|
|
+ $url = "https://api.weixin.qq.com/cgi-bin/express/business/order/get?access_token={$accessToken}";
|
|
|
+ $curl = new curl\Curl();
|
|
|
+ $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
|
|
|
+ return Json::decode($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量获取运单数据
|
|
|
+ */
|
|
|
+ public static function batchGetOrder($merchant, $data, $ptStyle = 0)
|
|
|
+ {
|
|
|
+ $accessToken = self::getAccessToken($merchant, $ptStyle);
|
|
|
+ $url = "https://api.weixin.qq.com/cgi-bin/express/business/order/batchget?access_token={$accessToken}";
|
|
|
+ $curl = new curl\Curl();
|
|
|
+ $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
|
|
|
+ return Json::decode($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询运单轨迹
|
|
|
+ */
|
|
|
+ public static function getPath($merchant, $data, $ptStyle = 0)
|
|
|
+ {
|
|
|
+ $accessToken = self::getAccessToken($merchant, $ptStyle);
|
|
|
+ $url = "https://api.weixin.qq.com/cgi-bin/express/business/path/get?access_token={$accessToken}";
|
|
|
+ $curl = new curl\Curl();
|
|
|
+ $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
|
|
|
+ return Json::decode($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 配置打印员
|
|
|
+ */
|
|
|
+ public static function addPrinter($merchant, $data, $ptStyle = 0)
|
|
|
+ {
|
|
|
+ $accessToken = self::getAccessToken($merchant, $ptStyle);
|
|
|
+ $url = "https://api.weixin.qq.com/cgi-bin/express/business/printer/update?access_token={$accessToken}";
|
|
|
+ $curl = new curl\Curl();
|
|
|
+ $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
|
|
|
+ return Json::decode($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询打印员
|
|
|
+ */
|
|
|
+ public static function getPrinter($merchant, $data, $ptStyle = 0)
|
|
|
+ {
|
|
|
+ $accessToken = self::getAccessToken($merchant, $ptStyle);
|
|
|
+ $url = "https://api.weixin.qq.com/cgi-bin/express/business/printer/get?access_token={$accessToken}";
|
|
|
+ $curl = new curl\Curl();
|
|
|
+ $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
|
|
|
+ return Json::decode($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询电子面单余额
|
|
|
+ */
|
|
|
+ public static function getQuota($merchant, $data, $ptStyle = 0)
|
|
|
+ {
|
|
|
+ $accessToken = self::getAccessToken($merchant, $ptStyle);
|
|
|
+ $url = "https://api.weixin.qq.com/cgi-bin/express/business/quota/get?access_token={$accessToken}";
|
|
|
+ $curl = new curl\Curl();
|
|
|
+ $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
|
|
|
+ return Json::decode($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 沙盒测试:模拟更新订单接口
|
|
|
+ */
|
|
|
+ public static function testUpdateOrder($merchant, $data, $ptStyle = 0)
|
|
|
+ {
|
|
|
+ $accessToken = self::getAccessToken($merchant, $ptStyle);
|
|
|
+ $url = "https://api.weixin.qq.com/cgi-bin/express/business/test_update_order?access_token={$accessToken}";
|
|
|
+ $curl = new curl\Curl();
|
|
|
+ $result = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode($data))->post($url);
|
|
|
+ return Json::decode($result);
|
|
|
+ }
|
|
|
+}
|