Express.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace ln\services\express\storage;
  3. use app\common\repositories\store\shipping\ExpressPartnerRepository;
  4. use app\common\repositories\store\shipping\ExpressRepository;
  5. use app\common\repositories\system\merchant\MerchantRepository;
  6. use ln\services\AccessTokenServeService;
  7. use ln\services\BaseExpress;
  8. use think\exception\ValidateException;
  9. /**
  10. * Class Express
  11. * @package ln\services\express\storage
  12. */
  13. class Express extends BaseExpress
  14. {
  15. /**
  16. * 注册服务
  17. */
  18. const EXPRESS_OPEN = 'expr/open';
  19. /**
  20. * 电子面单模版
  21. */
  22. const EXPRESS_TEMP = 'expr/temp';
  23. /**
  24. * 快递公司
  25. */
  26. const EXPRESS_LIST = 'expr/express';
  27. /**
  28. * 快递查询
  29. */
  30. const EXPRESS_QUERY = 'expr/query';
  31. /**
  32. * 面单打印
  33. */
  34. const EXPRESS_DUMP = 'expr/dump';
  35. /** 初始化
  36. * @param array $config
  37. * @return mixed|void
  38. */
  39. protected function initialize(array $config = [])
  40. {
  41. parent::initialize($config); // TODO: Change the autogenerated stub
  42. }
  43. /**
  44. * 开通物流服务
  45. * @return bool|mixed
  46. */
  47. public function open()
  48. {
  49. return $this->accessToken->httpRequest(self::EXPRESS_OPEN, []);
  50. }
  51. /**
  52. * 获取电子面单模版
  53. * @param $com 快递公司编号
  54. * @param int $page
  55. * @param int $limit
  56. * @return bool|mixed
  57. */
  58. public function temp(string $com)
  59. {
  60. $param = [
  61. 'com' => $com
  62. ];
  63. return $this->accessToken->httpRequest(self::EXPRESS_TEMP, $param);
  64. }
  65. /**
  66. * 获取物流公司列表
  67. * @param int $type 快递类型:1,国内运输商;2,国际运输商;3,国际邮政
  68. * @return bool|mixed
  69. */
  70. public function express(int $type = 0, int $page = 0, int $limit = 20)
  71. {
  72. $param = [];
  73. if ($type) $param['type'] = $type;
  74. return $this->accessToken->httpRequest(self::EXPRESS_LIST, $param);
  75. }
  76. /**
  77. * 查询物流信息
  78. * @param $com
  79. * @param $num
  80. * @return bool|mixed
  81. * @return 是否签收 ischeck
  82. * @return 物流状态:status 0在途,1揽收,2疑难,3签收,4退签,5派件,6退回,7转单,10待清关,11清关中,12已清关,13清关异常,14收件人拒签
  83. * @return 物流详情 content
  84. */
  85. public function query(string $num, string $com = '')
  86. {
  87. $param = [
  88. 'com' => $com,
  89. 'num' => $num
  90. ];
  91. if ($com === null) {
  92. unset($param['com']);
  93. }
  94. return $this->accessToken->httpRequest(self::EXPRESS_QUERY, $param);
  95. }
  96. /**
  97. * 电子面单打印
  98. * @param array $data 必需参数: com(快递公司编码)、to_name(寄件人)、to_tel(寄件人电话)、to_addr(寄件人详细地址)、from_name(收件人)、from_tel(收件人电话)、from_addr(收件人地址)、temp_id(电子面单模板ID)、siid(云打印机编号)、count(商品数量)
  99. * @return bool|mixed
  100. */
  101. public function dump($merId,$data)
  102. {
  103. $param = $data;
  104. $param['com'] = $data['com'] ?? '';
  105. if (!$param['com']) throw new ValidateException('快递公司编码缺失');
  106. $param['to_name'] = $data['to_name'] ?? '';
  107. $param['to_tel'] = $data['to_tel'] ?? '';
  108. $param['order_id'] = $data['order_id'] ?? '';
  109. $param['to_addr'] = $data['to_addr'] ?? '';
  110. if (!$param['to_addr'] || !$param['to_tel'] || !$param['to_name']) throw new ValidateException('寄件人信息缺失');
  111. $param['from_name'] = $data['from_name'] ?? '';
  112. $param['from_tel'] = $data['from_tel'] ?? '';
  113. $param['from_addr'] = $data['from_addr'] ?? '';
  114. if (!$param['from_name'] || !$param['from_tel'] || !$param['from_addr']) throw new ValidateException('收件人信息缺失');
  115. $param['temp_id'] = $data['temp_id'] ?? '';
  116. if (!$param['temp_id']) {
  117. throw new ValidateException('电子面单模板ID缺失');
  118. }
  119. $param['siid'] = merchantConfig($merId,'mer_config_siid');
  120. if (!$param['siid']) {
  121. throw new ValidateException('云打印机编号缺失');
  122. }
  123. $param['count'] = $data['count'] ?? '';
  124. $param['cargo'] = $data['cargo'] ?? '';
  125. if (!$param['count']) {
  126. throw new ValidateException('商品数量缺失');
  127. }
  128. $exp = app()->make(ExpressRepository::class)->getWhere(['code' => $data['com']]);
  129. $expressData = app()->make(ExpressPartnerRepository::class)->getSearch([
  130. 'express_id' => $exp['id'],
  131. 'mer_id' => $merId,
  132. ])->find();
  133. if ($expressData['partner_id'] == 1) $param['partner_id'] = $expressData['account'];
  134. if ($expressData['partner_key'] == 1) $param['partner_key'] = $expressData['key'];
  135. if ($expressData['net'] == 1) $param['net'] = $expressData['net_name'];
  136. return $this->accessToken->httpRequest(self::EXPRESS_DUMP, $param, 'POST');
  137. }
  138. }