Kuaidi100Service.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\common\service\express_assistant;
  3. use app\common\enum\DeliveryEnum;
  4. use app\common\enum\ExpressEnum;
  5. use app\common\enum\ThirdPartyEnum;
  6. use app\common\service\ConfigService;
  7. use app\common\service\RegionService;
  8. class Kuaidi100Service
  9. {
  10. // 授权key
  11. private $key;
  12. // 授权密钥
  13. private $secret;
  14. // 设备编号
  15. private $siid;
  16. /**
  17. * 构造方法
  18. * @param $type 面单类型
  19. */
  20. public function __construct($type)
  21. {
  22. $this->init($type);
  23. }
  24. public function init($type)
  25. {
  26. $typeEnName = ThirdPartyEnum::getThirdPartyDesc($type, 'en');
  27. $this->key = ConfigService::get($typeEnName, 'key', '');
  28. $this->secret = ConfigService::get($typeEnName, 'secret', '');
  29. $this->siid = ConfigService::get($typeEnName, 'siid', '');
  30. }
  31. /**
  32. * @notes 打印电子面单
  33. * @param $data
  34. * @throws \Exception
  35. * @author Tab
  36. * @date 2021/11/22 18:18
  37. */
  38. public function print($data)
  39. {
  40. if (empty($this->key) || empty($this->secret) || empty($this->siid)) {
  41. throw new \Exception('请先进行面单设置');
  42. }
  43. if (! in_array($data['express']['code100'], [ 'jd', 'shunfeng' ]) && empty($data['template']['template_id'])) {
  44. throw new \Exception('请在模板中设置电子面单客户账号');
  45. }
  46. if (! in_array($data['express']['code100'], [ 'jd', 'shunfeng' ]) && empty($data['template']['partner_key'])) {
  47. throw new \Exception('请在模板中设置电子面单密钥');
  48. }
  49. $code = ExpressEnum::getkuaidi100code($data['express']['code100']);
  50. $param = array(
  51. 'printType' => 'CLOUD',
  52. 'partnerId' => $data['template']['partner_id'],
  53. 'partnerKey' => $data['template']['partner_key'],
  54. 'net' => $data['template']['net'] ?? $data['express']['name'],
  55. 'kuaidicom' => $data['express']['code100'],
  56. 'tempid' => $data['template']['template_id'],
  57. 'siid' => $this->siid,
  58. 'cargo' => '商品',
  59. 'code' => $code, //申通快递需要code
  60. 'weight' => $data['totalWeight'] ? $data['totalWeight'] : 1,
  61. 'count' => 1,
  62. 'payType' => DeliveryEnum::getKuaidi100Desc($data['template']['pay_type']),
  63. 'expType' => '标准快递',
  64. 'remark' => $data['remark'],
  65. 'recMan' => array(
  66. 'name' => $data['order']['address']->contact,
  67. 'mobile' => $data['order']['address']->mobile,
  68. 'printAddr' => RegionService::getAddress([
  69. $data['order']['address']->province,
  70. $data['order']['address']->city,
  71. $data['order']['address']->district,
  72. ], $data['order']['address']->address)
  73. ),
  74. 'sendMan' => array(
  75. 'name' => $data['sender']['name'],
  76. 'mobile' => $data['sender']['mobile'],
  77. 'printAddr' => RegionService::getAddress([
  78. $data['sender']['province_id'],
  79. $data['sender']['city_id'],
  80. $data['sender']['district_id'],
  81. ], $data['sender']['address'])
  82. )
  83. );
  84. // 当前时间戳
  85. list($msec, $sec) = explode(' ', microtime());
  86. $t = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  87. // 格式化参数
  88. $postData = array();
  89. $postData["param"] = json_encode($param, JSON_UNESCAPED_UNICODE);
  90. $postData["method"] = "order";
  91. $postData["key"] = $this->key;
  92. $postData["t"] = $t;
  93. $sign = md5($postData["param"] . $t . $this->key . $this->secret);
  94. $postData["sign"] = strtoupper($sign);
  95. // 发送请求
  96. // $url = 'http://poll.kuaidi100.com/printapi/printtask.do?method=eOrder';
  97. //v2接口
  98. $url = 'https://api.kuaidi100.com/label/order?method=order';
  99. $ch = curl_init();
  100. curl_setopt($ch, CURLOPT_POST, 1);
  101. curl_setopt($ch, CURLOPT_HEADER, 0);
  102. curl_setopt($ch, CURLOPT_URL, $url);
  103. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
  104. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  105. $result = json_decode(curl_exec($ch), true);
  106. if (!$result || $result['code'] != '200') {
  107. throw new \Exception($result['message'] ?? '打印电子面单异常,原因未知');
  108. }
  109. return $result;
  110. }
  111. }