截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Kuaidi100.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\common\server\printing;
  20. use app\common\model\Express;
  21. use app\common\server\AreaServer;
  22. use app\common\server\ConfigServer;
  23. use think\Exception;
  24. class Kuaidi100
  25. {
  26. /**
  27. * 客户授权key
  28. */
  29. private $key;
  30. /**
  31. * 授权secret
  32. */
  33. private $secret;
  34. /**
  35. * 设备编号
  36. */
  37. private $siid;
  38. public function __construct($shop_id)
  39. {
  40. $kd100 = ConfigServer::get('kd100', null, null, $shop_id);
  41. $this->key = $kd100['kd100_key'] ?? '';
  42. $this->secret = $kd100['kd100_secret'] ?? '';
  43. $this->siid = $kd100['kd100_siid'] ?? '';
  44. }
  45. /**
  46. * @notes 打印电子面单
  47. * @param $data
  48. * @return mixed
  49. * @throws Exception
  50. * @author 段誉
  51. * @date 2023/2/13 17:03
  52. */
  53. public function print($data)
  54. {
  55. try {
  56. if (!$this->key) {
  57. throw new Exception('请设置快递100的授权key');
  58. }
  59. if (!$this->secret) {
  60. throw new Exception('请设置快递100的授权secret');
  61. }
  62. if (!$this->siid) {
  63. throw new Exception('请设置打印机设备编码');
  64. }
  65. if ($data['express']['name'] != '顺丰快递') {
  66. if (!$data['template']['partner_id']) {
  67. throw new Exception('请设置电子面单客户账号');
  68. }
  69. if (!$data['template']['partner_key']) {
  70. throw new Exception('请设置电子面单客户密钥');
  71. }
  72. }
  73. $code = Express::getkuaidi100code($data['express']['code100']);
  74. // 请求参数
  75. $param = array(
  76. 'printType' => 'CLOUD',
  77. 'partnerId' => $data['template']['partner_id'],
  78. 'partnerKey' => $data['template']['partner_key'],
  79. 'net' => $data['template']['net'] ?? $data['express']['name'],
  80. 'kuaidicom' => $data['express']['code100'],
  81. 'tempid' => $data['template']['template_id'],
  82. 'siid' => $this->siid,
  83. 'cargo' => '商品',
  84. 'code' => $code, //申通快递需要code
  85. 'weight' => $data['total_weight'] ?: 1,
  86. 'count' => 1,
  87. 'payType' => $data['template']['pay_type'],
  88. 'expType' => '标准快递',
  89. 'remark' => $data['remark'],
  90. 'recMan' => array(
  91. 'name' => $data['order']['consignee'],
  92. 'mobile' => $data['order']['mobile'],
  93. 'printAddr' => AreaServer::getAddress([
  94. $data['order']['province'],
  95. $data['order']['city'],
  96. $data['order']['city'],
  97. $data['order']['district'],
  98. ], $data['order']['address']),
  99. ),
  100. 'sendMan' => array(
  101. 'name' => $data['sender']['name'],
  102. 'mobile' => $data['sender']['mobile'],
  103. 'printAddr' => AreaServer::getAddress([
  104. $data['sender']['province_id'],
  105. $data['sender']['city_id'],
  106. $data['sender']['district_id'],
  107. ], $data['sender']['address']),
  108. )
  109. );
  110. // 当前时间戳
  111. list($msec, $sec) = explode(' ', microtime());
  112. $t = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  113. // 格式化参数
  114. $post_data = array();
  115. $post_data["param"] = json_encode($param, JSON_UNESCAPED_UNICODE);
  116. $post_data["key"] = $this->key;
  117. $post_data["t"] = $t;
  118. $sign = md5($post_data["param"] . $t . $this->key . $this->secret);
  119. $post_data["sign"] = strtoupper($sign);
  120. //V2接口
  121. $url = 'https://api.kuaidi100.com/label/order?method=order';
  122. $ch = curl_init();
  123. curl_setopt($ch, CURLOPT_POST, 1);
  124. curl_setopt($ch, CURLOPT_HEADER, 0);
  125. curl_setopt($ch, CURLOPT_URL, $url);
  126. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
  127. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  128. $result = json_decode(curl_exec($ch), true);
  129. if (!$result || $result['code'] != '200') {
  130. throw new \Exception($result['message'] ?? '打印电子面单异常,原因未知');
  131. }
  132. return $result;
  133. } catch (\Exception $e) {
  134. throw new Exception($e->getMessage());
  135. }
  136. }
  137. }