ZtExpress.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\extra\tools;
  3. class ZtExpress
  4. {
  5. /**
  6. * 接口地址
  7. * @var string
  8. */
  9. protected string $gateway = "https://japi-test.zto.com";
  10. protected array $config = [];
  11. /**
  12. * 设置配置权限
  13. * @param array $data
  14. * @return $this
  15. */
  16. public function config(array $data = [])
  17. {
  18. $this->config = $data;
  19. if ($data['version'] == "test")
  20. {
  21. $this->gateway = "https://japi-test.zto.com";
  22. } else {
  23. $this->gateway = "https://japi.zto.com";
  24. }
  25. return $this;
  26. }
  27. /**
  28. * 下单
  29. * @param array $sendData 发货信息
  30. * @param array $endData 收件人信息
  31. * @return array
  32. */
  33. public function createOrder(array $sendData = [],array $endData = [],string $orderId = "",int $expressType = 1): array
  34. {
  35. $param = [
  36. "partnerType" => 2,
  37. "orderType" => 1,
  38. "partnerOrderCode" => $orderId,
  39. "senderInfo" => [
  40. "senderName" => $endData['username'],
  41. "senderMobile" => $endData['mobile'],
  42. "senderProvince" => "",
  43. "senderCity" => "",
  44. "senderDistrict" => "",
  45. "senderAddress" => "",
  46. ]
  47. ];
  48. if ($this->config['version'] == "test")
  49. {
  50. $param['accountInfo']['accountPassword'] = $this->config['account'];
  51. } else {
  52. $param['accountInfo']['accountId'] = $this->config['account'];
  53. }
  54. }
  55. /**
  56. * 获取签名
  57. * @param array $params
  58. * @return string
  59. */
  60. protected function getSign(array $params = []): string
  61. {
  62. $fixedParams = array();
  63. foreach ($params as $k => $v) {
  64. if (gettype($v) != "string") {
  65. $fixedParams += [$k => json_encode($v)];
  66. } else {
  67. $fixedParams += [$k => $v];
  68. }
  69. }
  70. $str_to_digest = "";
  71. foreach ($fixedParams as $k => $v) {
  72. $str_to_digest = $str_to_digest .$k ."=" .$v ."&";
  73. }
  74. $str_to_digest = substr($str_to_digest, 0, -1) .$this->config['secret'];
  75. return base64_encode(md5($str_to_digest, true));
  76. }
  77. }