Express.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\services\other\ExpressServices;
  13. use crmeb\basic\BaseExpress;
  14. use crmeb\exceptions\ApiException;
  15. use crmeb\services\AccessTokenServeService;
  16. /**
  17. * Class Express
  18. * @package crmeb\services\express\storage
  19. */
  20. class Express extends BaseExpress
  21. {
  22. /**
  23. * 注册服务
  24. */
  25. const EXPRESS_OPEN = 'expr/open';
  26. /**
  27. * 电子面单模版
  28. */
  29. const EXPRESS_TEMP = 'expr/temp';
  30. /**
  31. * 快递公司
  32. */
  33. const EXPRESS_LIST = 'expr/express';
  34. /**
  35. * 快递查询
  36. */
  37. const EXPRESS_QUERY = 'expr/query';
  38. /**
  39. * 面单打印
  40. */
  41. const EXPRESS_DUMP = 'expr/dump';
  42. /** 初始化
  43. * @param array $config
  44. * @return mixed|void
  45. */
  46. protected function initialize(array $config = [])
  47. {
  48. parent::initialize($config);
  49. }
  50. /**
  51. * 开通物流服务
  52. * @return bool|mixed
  53. */
  54. public function open()
  55. {
  56. return $this->accessToken->httpRequest(self::EXPRESS_OPEN, []);
  57. }
  58. /**
  59. * 获取电子面单模版
  60. * @param $com 快递公司编号
  61. * @param int $page
  62. * @param int $limit
  63. * @return bool|mixed
  64. */
  65. public function temp(string $com)
  66. {
  67. $param = [
  68. 'com' => $com
  69. ];
  70. $header = [];
  71. if (!sys_config('config_export_siid')) {
  72. $header = ['version:v1.1'];
  73. }
  74. return $this->accessToken->httpRequest(self::EXPRESS_TEMP, $param, 'POST', true, $header);
  75. }
  76. /**
  77. * 获取物流公司列表
  78. * @param int $type 快递类型:1,国内运输商;2,国际运输商;3,国际邮政
  79. * @return bool|mixed
  80. */
  81. public function express(int $type = 0, int $page = 0, int $limit = 20)
  82. {
  83. if ($type) {
  84. $param = [
  85. 'type' => $type,
  86. 'page' => $page,
  87. 'limit' => $limit
  88. ];
  89. } else {
  90. $param = [];
  91. }
  92. return $this->accessToken->httpRequest(self::EXPRESS_LIST, $param);
  93. }
  94. /**
  95. * 查询物流信息
  96. * @param $com
  97. * @param $num
  98. * @return bool|mixed
  99. * @return 是否签收 ischeck
  100. * @return 物流状态:status 0在途,1揽收,2疑难,3签收,4退签,5派件,6退回,7转单,10待清关,11清关中,12已清关,13清关异常,14收件人拒签
  101. * @return 物流详情 content
  102. */
  103. public function query(string $num, string $com = '')
  104. {
  105. $param = [
  106. 'com' => $com,
  107. 'num' => $num
  108. ];
  109. if ($com === null) {
  110. unset($param['com']);
  111. }
  112. return $this->accessToken->httpRequest(self::EXPRESS_QUERY, $param);
  113. }
  114. /**
  115. * 电子面单打印
  116. * @param array $data 必需参数: com(快递公司编码)、to_name(寄件人)、to_tel(寄件人电话)、to_addr(寄件人详细地址)、from_name(收件人)、from_tel(收件人电话)、from_addr(收件人地址)、temp_id(电子面单模板ID)、siid(云打印机编号)、count(商品数量)
  117. * @return bool|mixed
  118. */
  119. public function dump($data)
  120. {
  121. $param = $data;
  122. $param['com'] = $data['com'] ?? '';
  123. if (!$param['com']) throw new ApiException('快递公司编码缺失');
  124. $param['to_name'] = $data['to_name'] ?? '';
  125. $param['to_tel'] = $data['to_tel'] ?? '';
  126. $param['order_id'] = $data['order_id'] ?? '';
  127. $param['to_addr'] = $data['to_addr'] ?? '';
  128. if (!$param['to_addr'] || !$param['to_tel'] || !$param['to_name']) throw new ApiException('寄件人信息缺失');
  129. $param['from_name'] = $data['from_name'] ?? '';
  130. $param['from_tel'] = $data['from_tel'] ?? '';
  131. $param['from_addr'] = $data['from_addr'] ?? '';
  132. if (!$param['from_name'] || !$param['from_tel'] || !$param['from_addr']) throw new ApiException('收件人信息缺失');
  133. $param['temp_id'] = $data['temp_id'] ?? '';
  134. if (!$param['temp_id']) {
  135. throw new ApiException('电子面单模板ID缺失');
  136. }
  137. $param['siid'] = sys_config('config_export_siid');
  138. // if (!$param['siid']) {
  139. // throw new ApiException('云打印机编号缺失');
  140. // }
  141. $param['count'] = $data['count'] ?? '';
  142. $param['cargo'] = $data['cargo'] ?? '';
  143. if (!$param['count']) {
  144. throw new ApiException('商品数量缺失');
  145. }
  146. /** @var ExpressServices $expressServices */
  147. $expressServices = app()->make(ExpressServices::class);
  148. $expressData = $expressServices->getOneByWhere(['code' => $param['com']])->toArray();
  149. if (isset($data['cargo'])) $param['cargo'] = $data['cargo'];
  150. if ($expressData['partner_id'] == 1) $param['partner_id'] = $expressData['account'];
  151. if ($expressData['partner_key'] == 1) $param['partner_key'] = $expressData['key'];
  152. if ($expressData['net'] == 1) $param['net'] = $expressData['net_name'];
  153. if ($expressData['check_man'] == 1) $param['checkMan'] = $expressData['courier_name'];
  154. if ($expressData['partner_name'] == 1) $param['partnerName'] = $expressData['customer_name'];
  155. if ($expressData['is_code'] == 1) $param['code'] = $expressData['code_name'];
  156. //修改没有打印机的时候print_type=IMAGE,就会返回面单图片
  157. $header = [];
  158. if (!$data['siid']) {
  159. $param['print_type'] = 'IMAGE';
  160. $header = ['version:v1.1'];
  161. }
  162. return $this->accessToken->httpRequest(self::EXPRESS_DUMP, $param, 'POST', true, $header);
  163. }
  164. }