Express.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\express\storage;
  12. use app\common\repositories\store\shipping\ExpressPartnerRepository;
  13. use app\common\repositories\store\shipping\ExpressRepository;
  14. use app\common\repositories\system\merchant\MerchantRepository;
  15. use crmeb\services\AccessTokenServeService;
  16. use crmeb\services\BaseExpress;
  17. use think\exception\ValidateException;
  18. /**
  19. * Class Express
  20. * @package crmeb\services\express\storage
  21. */
  22. class Express extends BaseExpress
  23. {
  24. /**
  25. * 注册服务
  26. */
  27. const EXPRESS_OPEN = 'expr/open';
  28. /**
  29. * 电子面单模版 -- 弃用
  30. */
  31. const EXPRESS_TEMP = 'expr/temp';
  32. /**
  33. * 电子面单模版
  34. */
  35. const EXPRESS_TEMP_V2 = 'expr_dump/temp';
  36. /**
  37. * 快递公司
  38. */
  39. const EXPRESS_LIST = 'expr/express';
  40. /**
  41. * 快递查询
  42. */
  43. const EXPRESS_QUERY = 'expr/query';
  44. /**
  45. * 面单打印
  46. */
  47. const EXPRESS_DUMP = 'expr/dump';
  48. /**
  49. * 复打:重新打印之前的电子面单
  50. */
  51. const EXPRESS_REPEAT_DUMP ='expr_dump/repeat_dump';
  52. /** 初始化
  53. * @param array $config
  54. * @return mixed|void
  55. */
  56. protected function initialize(array $config = [])
  57. {
  58. parent::initialize($config); // TODO: Change the autogenerated stub
  59. }
  60. /**
  61. * 开通物流服务
  62. * @return bool|mixed
  63. */
  64. public function open()
  65. {
  66. return $this->accessToken->httpRequest(self::EXPRESS_OPEN, []);
  67. }
  68. /**
  69. * 获取对应快递的电子面单模板
  70. * @param array $params 查询参数
  71. * @return array|mixed
  72. * @author Qinii
  73. */
  74. public function expr_dump(array $params)
  75. {
  76. return $this->accessToken->httpRequest(self::EXPRESS_TEMP_V2, $params,'get');
  77. }
  78. /**
  79. * 获取电子面单模版
  80. * @param $com 快递公司编号
  81. * @param int $page
  82. * @param int $limit
  83. * @return bool|mixed
  84. */
  85. public function temp(string $com)
  86. {
  87. $param = ['com' => $com];
  88. return $this->accessToken->httpRequest(self::EXPRESS_TEMP_V2, $param,'get',true, ['version:v1.1']);
  89. }
  90. /**
  91. * 获取物流公司列表
  92. * @param int $type 快递类型:1,国内运输商;2,国际运输商;3,国际邮政
  93. * @return bool|mixed
  94. */
  95. public function express(int $type = 0, int $page = 0, int $limit = 20)
  96. {
  97. $param = [];
  98. if ($type) $param['type'] = $type;
  99. return $this->accessToken->httpRequest(self::EXPRESS_LIST, $param,'get');
  100. }
  101. /**
  102. * 查询物流信息
  103. * @param $com
  104. * @param $num
  105. * @return bool|mixed
  106. * @return 是否签收 ischeck
  107. * @return 物流状态:status 0在途,1揽收,2疑难,3签收,4退签,5派件,6退回,7转单,10待清关,11清关中,12已清关,13清关异常,14收件人拒签
  108. * @return 物流详情 content
  109. */
  110. public function query(string $num, string $com = '', $phone)
  111. {
  112. $param = [
  113. 'com' => $com,
  114. 'num' => $num,
  115. 'phone' => $phone,
  116. ];
  117. if ($com === null) {
  118. unset($param['com']);
  119. }
  120. return $this->accessToken->httpRequest(self::EXPRESS_QUERY, $param);
  121. }
  122. /**
  123. * 电子面单打印
  124. * @param array $data 必需参数: com(快递公司编码)、to_name(寄件人)、to_tel(寄件人电话)、to_addr(寄件人详细地址)、from_name(收件人)、from_tel(收件人电话)、from_addr(收件人地址)、temp_id(电子面单模板ID)、siid(云打印机编号)、count(商品数量)
  125. * @return bool|mixed
  126. */
  127. public function dump($merId,$data)
  128. {
  129. if (!$data['count']) throw new ValidateException('商品数量缺失');
  130. if (!$data['temp_id']) throw new ValidateException('电子面单模板ID缺失');
  131. if (!$data['to_addr'] || !$data['to_tel'] || !$data['to_name'])
  132. throw new ValidateException('收件人信息缺失');
  133. if (!$data['from_name'] || !$data['from_tel'] || !$data['from_addr'])
  134. throw new ValidateException('寄件人信息缺失');
  135. if (!$data['com']) throw new ValidateException('快递公司编码缺失');
  136. $param = $data;
  137. //修改没有打印机的时候print_type=IMAGE,就会返回面单图片
  138. $param['print_type'] = merchantConfig($merId,'mer_dump_type') ? '' :'IMAGE';
  139. $exp = app()->make(ExpressRepository::class)->getWhere(['code' => $data['com']]);
  140. $expressData = app()->make(ExpressPartnerRepository::class)->getSearch(['express_id' => $exp['id'], 'mer_id' => $merId,])->find();
  141. if ($exp['partner_id'] == 1) {
  142. if (!$expressData) throw new ValidateException('未查询到快递公司的月结账号');
  143. $param['partner_id'] = $expressData['account'];
  144. }
  145. if ($exp['partner_key'] == 1) $param['partner_key'] = $expressData['key'];
  146. if ($exp['net'] == 1) $param['net'] = $expressData['net_name'];
  147. if ($exp['check_man'] == 1) $param['checkMan'] = $expressData['check_man'];
  148. if ($exp['partner_name'] == 1) $param['partner_name'] = $expressData['partner_name'];
  149. if ($exp['is_code'] == 1) $param['code'] = $expressData['code'];
  150. //修改增加header版本号,兼容旧版和新版
  151. return $this->accessToken->httpRequest(self::EXPRESS_DUMP, $param, 'POST');
  152. }
  153. public function repeat_dump($taskId,$siid = '')
  154. {
  155. if (!$taskId) return '';
  156. $param['task_id'] = $taskId;
  157. if ($siid) $param['siid'] = $siid;
  158. $res = $this->accessToken->httpRequest(self::EXPRESS_REPEAT_DUMP, $param, 'POST');
  159. if (isset($res['code']) && $res['code'] == -1) {
  160. throw new ValidateException('平台错误:'.$res['message']);
  161. }
  162. return $res;
  163. }
  164. }