SfExpress.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\extra\tools;
  3. use support\think\Cache;
  4. use yzh52521\EasyHttp\Http;
  5. class SfExpress
  6. {
  7. /**
  8. * 接口地址
  9. * @var string
  10. */
  11. protected string $gateway = "https://sfapi-sbox.sf-express.com/std/service";
  12. protected array $config = [];
  13. /**
  14. * 设置配置权限
  15. * @param array $data
  16. * @return $this
  17. */
  18. public function config(array $data = [])
  19. {
  20. $this->config = $data;
  21. if ($data['version'] == "test")
  22. {
  23. $this->gateway = "https://sfapi-sbox.sf-express.com/std/service";
  24. } else {
  25. $this->gateway = "https://bspgw.sf-express.com/std/service";
  26. }
  27. return $this;
  28. }
  29. /**
  30. * 下单
  31. * @param array $sendData 发货信息
  32. * @param array $endData 收件人信息
  33. * @return array
  34. */
  35. public function createOrder(array $sendData = [],array $endData = [],string $orderId = "",int $expressType = 1): array
  36. {
  37. $param = [
  38. "partnerID" => $this->config['appid'],
  39. "requestID" => strtoupper(CodeExtend::random(12,3)),
  40. "serviceCode" => "EXP_RECE_CREATE_ORDER",
  41. "timestamp" => time(),
  42. "accessToken" => $this->getAccessToken(),
  43. "msgData" => json_encode([
  44. "language" => "zh-CN",
  45. "orderId" => $orderId,
  46. "monthlyCard" => intval($this->config['account']), // 月结账号
  47. "payMethod" => 1,
  48. "expressTypeId" => intval($expressType),
  49. "isReturnRoutelabel"=> 1,
  50. "cargoDetails" => [
  51. [
  52. "name" => $sendData['express_name'],
  53. "unit" => $sendData['express_unit'],
  54. "currency" => "CNY",
  55. "weight" => 1
  56. ]
  57. ],
  58. "contactInfoList" => [
  59. [
  60. "country" => "CN",
  61. "mobile" => $sendData['express_mobile'],
  62. "contact" => $sendData['express_contact'],
  63. "contactType" => 1,
  64. "address" => $sendData['express_address'],
  65. ],
  66. [
  67. "country" => "CN",
  68. "mobile" => $endData['mobile'],
  69. "contact" => $endData['username'],
  70. "contactType" => 2,
  71. "address" => $endData['address'],
  72. ]
  73. ]
  74. ])
  75. ];
  76. print_r($this->config);
  77. print_r($param);
  78. echo getDateFull()."===uri==={$this->gateway}\n";
  79. $resp = Http::post($this->gateway,$param)->array();
  80. print_r($resp);
  81. if ($resp['apiResultCode'] == "A1000") {
  82. return json_decode($resp['apiResultData'],true);
  83. }
  84. return [];
  85. }
  86. /**
  87. * 取消订单
  88. */
  89. public function cancelOrder(array $data = [])
  90. {
  91. $param = [
  92. "partnerID" => $this->config['appid'],
  93. "requestID" => strtoupper(CodeExtend::random(12,3)),
  94. "serviceCode" => "EXP_RECE_UPDATE_ORDER",
  95. "timestamp" => time(),
  96. "accessToken" => $this->getAccessToken(),
  97. "msgData" => json_encode([
  98. "dealType" => 2,
  99. "orderId" => $data['orderId'],
  100. "waybillNoInfoList" => [
  101. [
  102. "waybillNo" => $data['number'],
  103. "waybillType" => 1
  104. ]
  105. ]
  106. ])
  107. ];
  108. $resp = Http::post($this->gateway,$param)->array();
  109. if ($resp['apiResultCode'] == "A1000") {
  110. return json_decode($resp['apiResultData'],true);
  111. }
  112. return [];
  113. }
  114. /**
  115. * 云打印面单打印2.0接口-面单类API
  116. */
  117. public function orderExpress2Pdf(array $data = [])
  118. {
  119. $param = [
  120. "partnerID" => $this->config['appid'],
  121. "requestID" => strtoupper(CodeExtend::random(12,3)),
  122. "serviceCode" => "COM_RECE_CLOUD_PRINT_WAYBILLS",
  123. "timestamp" => time(),
  124. "accessToken" => $this->getAccessToken(),
  125. "msgData" => json_encode([
  126. "templateCode" => "fm_76130_standard_ZSWHCLLWAGK7",
  127. "fileType" => "pdf",
  128. "sync" => true,
  129. "documents" => [
  130. [
  131. "masterWaybillNo" => $data['number']
  132. ]
  133. ],
  134. "version" => "1.0",
  135. ])
  136. ];
  137. $resp = Http::post($this->gateway,$param)->array();
  138. if ($resp['apiResultCode'] == "A1000") {
  139. return $resp['apiResultData'];
  140. }
  141. return [];
  142. }
  143. /**
  144. * 获取运费
  145. * @param string $orderId
  146. * @return array|mixed
  147. */
  148. public function getOrderFee(string $orderId = "")
  149. {
  150. $param = [
  151. "partnerID" => $this->config['appid'],
  152. "requestID" => strtoupper(CodeExtend::random(12,3)),
  153. "serviceCode" => "EXP_RECE_QUERY_SFWAYBILL",
  154. "timestamp" => time(),
  155. "accessToken" => $this->getAccessToken(),
  156. "msgData" => json_encode([
  157. "trackingType" => 1,
  158. "trackingNum" => "SF7444497015623"
  159. ])
  160. ];
  161. $resp = Http::post($this->gateway,$param)->array();
  162. if ($resp['apiResultCode'] == "A1000") {
  163. return json_decode($resp['apiResultData'],true);
  164. }
  165. return [];
  166. }
  167. /**
  168. * 获取AccessToken
  169. * @return string
  170. */
  171. public function getAccessToken(): string
  172. {
  173. try {
  174. $sfToken = Cache::get($this->config['appid']."-sf-token");
  175. if (!empty($sfToken)) return $sfToken;
  176. $param = [
  177. "partnerID" => $this->config['appid'],
  178. "secret" => $this->config['secret'],
  179. "grantType" => "password"
  180. ];
  181. if ($this->config['version'] == "test") {
  182. $gateway = "https://sfapi-sbox.sf-express.com/";
  183. } else {
  184. $gateway = "https://sfapi.sf-express.com/";
  185. }
  186. print_r($this->config);
  187. print_r($param);
  188. echo getDateFull()."===uri==={$gateway}\n";
  189. $resp = Http::post($gateway."oauth2/accessToken",$param)->array();
  190. print_r($resp);
  191. if ($resp['apiResultCode'] == "A1000") {
  192. Cache::set($this->config['appid']."-sf-token",$resp['accessToken'],$resp['expiresIn']);
  193. return $resp['accessToken'];
  194. }
  195. return "";
  196. } catch (\Throwable $throwable) {
  197. return "";
  198. }
  199. }
  200. }