| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace app\extra\tools;
- use support\think\Cache;
- use yzh52521\EasyHttp\Http;
- class SfExpress
- {
- /**
- * 接口地址
- * @var string
- */
- protected string $gateway = "https://sfapi-sbox.sf-express.com/std/service";
- protected array $config = [];
- /**
- * 设置配置权限
- * @param array $data
- * @return $this
- */
- public function config(array $data = [])
- {
- $this->config = $data;
- if ($data['version'] == "test")
- {
- $this->gateway = "https://sfapi-sbox.sf-express.com/std/service";
- } else {
- $this->gateway = "https://bspgw.sf-express.com/std/service";
- }
- return $this;
- }
- /**
- * 下单
- * @param array $sendData 发货信息
- * @param array $endData 收件人信息
- * @return array
- */
- public function createOrder(array $sendData = [],array $endData = [],string $orderId = "",int $expressType = 1): array
- {
- $param = [
- "partnerID" => $this->config['appid'],
- "requestID" => strtoupper(CodeExtend::random(12,3)),
- "serviceCode" => "EXP_RECE_CREATE_ORDER",
- "timestamp" => time(),
- "accessToken" => $this->getAccessToken(),
- "msgData" => json_encode([
- "language" => "zh-CN",
- "orderId" => $orderId,
- "monthlyCard" => intval($this->config['account']), // 月结账号
- "payMethod" => 1,
- "expressTypeId" => intval($expressType),
- "isReturnRoutelabel"=> 1,
- "cargoDetails" => [
- [
- "name" => $sendData['express_name'],
- "unit" => $sendData['express_unit'],
- "currency" => "CNY",
- "weight" => 1
- ]
- ],
- "contactInfoList" => [
- [
- "country" => "CN",
- "mobile" => $sendData['express_mobile'],
- "contact" => $sendData['express_contact'],
- "contactType" => 1,
- "address" => $sendData['express_address'],
- ],
- [
- "country" => "CN",
- "mobile" => $endData['mobile'],
- "contact" => $endData['username'],
- "contactType" => 2,
- "address" => $endData['address'],
- ]
- ]
- ])
- ];
- print_r($this->config);
- print_r($param);
- echo getDateFull()."===uri==={$this->gateway}\n";
- $resp = Http::post($this->gateway,$param)->array();
- print_r($resp);
- if ($resp['apiResultCode'] == "A1000") {
- return json_decode($resp['apiResultData'],true);
- }
- return [];
- }
- /**
- * 取消订单
- */
- public function cancelOrder(array $data = [])
- {
- $param = [
- "partnerID" => $this->config['appid'],
- "requestID" => strtoupper(CodeExtend::random(12,3)),
- "serviceCode" => "EXP_RECE_UPDATE_ORDER",
- "timestamp" => time(),
- "accessToken" => $this->getAccessToken(),
- "msgData" => json_encode([
- "dealType" => 2,
- "orderId" => $data['orderId'],
- "waybillNoInfoList" => [
- [
- "waybillNo" => $data['number'],
- "waybillType" => 1
- ]
- ]
- ])
- ];
- $resp = Http::post($this->gateway,$param)->array();
- if ($resp['apiResultCode'] == "A1000") {
- return json_decode($resp['apiResultData'],true);
- }
- return [];
- }
- /**
- * 云打印面单打印2.0接口-面单类API
- */
- public function orderExpress2Pdf(array $data = [])
- {
- $param = [
- "partnerID" => $this->config['appid'],
- "requestID" => strtoupper(CodeExtend::random(12,3)),
- "serviceCode" => "COM_RECE_CLOUD_PRINT_WAYBILLS",
- "timestamp" => time(),
- "accessToken" => $this->getAccessToken(),
- "msgData" => json_encode([
- "templateCode" => "fm_76130_standard_ZSWHCLLWAGK7",
- "fileType" => "pdf",
- "sync" => true,
- "documents" => [
- [
- "masterWaybillNo" => $data['number']
- ]
- ],
- "version" => "1.0",
- ])
- ];
- $resp = Http::post($this->gateway,$param)->array();
- if ($resp['apiResultCode'] == "A1000") {
- return $resp['apiResultData'];
- }
- return [];
- }
- /**
- * 获取运费
- * @param string $orderId
- * @return array|mixed
- */
- public function getOrderFee(string $orderId = "")
- {
- $param = [
- "partnerID" => $this->config['appid'],
- "requestID" => strtoupper(CodeExtend::random(12,3)),
- "serviceCode" => "EXP_RECE_QUERY_SFWAYBILL",
- "timestamp" => time(),
- "accessToken" => $this->getAccessToken(),
- "msgData" => json_encode([
- "trackingType" => 1,
- "trackingNum" => "SF7444497015623"
- ])
- ];
- $resp = Http::post($this->gateway,$param)->array();
- if ($resp['apiResultCode'] == "A1000") {
- return json_decode($resp['apiResultData'],true);
- }
- return [];
- }
- /**
- * 获取AccessToken
- * @return string
- */
- public function getAccessToken(): string
- {
- try {
- $sfToken = Cache::get($this->config['appid']."-sf-token");
- if (!empty($sfToken)) return $sfToken;
- $param = [
- "partnerID" => $this->config['appid'],
- "secret" => $this->config['secret'],
- "grantType" => "password"
- ];
- if ($this->config['version'] == "test") {
- $gateway = "https://sfapi-sbox.sf-express.com/";
- } else {
- $gateway = "https://sfapi.sf-express.com/";
- }
- print_r($this->config);
- print_r($param);
- echo getDateFull()."===uri==={$gateway}\n";
- $resp = Http::post($gateway."oauth2/accessToken",$param)->array();
- print_r($resp);
- if ($resp['apiResultCode'] == "A1000") {
- Cache::set($this->config['appid']."-sf-token",$resp['accessToken'],$resp['expiresIn']);
- return $resp['accessToken'];
- }
- return "";
- } catch (\Throwable $throwable) {
- return "";
- }
- }
- }
|