Express.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\shipping\ExpressServices;
  13. use crmeb\exceptions\ApiException;
  14. use crmeb\services\express\BaseExpress;
  15. use crmeb\exceptions\AdminException;
  16. /**
  17. * Class Express
  18. * @package crmeb\services\express\storage
  19. */
  20. class Express extends BaseExpress
  21. {
  22. //注册服务
  23. const EXPRESS_OPEN = 'v2/expr/open';
  24. //电子面单模版
  25. const EXPRESS_TEMP = 'v2/expr_dump/temp';
  26. //快递公司
  27. const EXPRESS_LIST = 'v2/expr/express';
  28. //快递查询
  29. const EXPRESS_QUERY = 'v2/expr/query';
  30. //面单打印
  31. const EXPRESS_DUMP = 'v2/expr/dump';
  32. //获取物流公司信息
  33. const SHIPMENT_KUAIDI_NUMS = 'v2/shipment/get_kuaidi_coms';
  34. //创建商家寄件订单
  35. const SHIPMENT_CREATE_ORDER = 'v2/shipment/create_order';
  36. //取消商家寄件
  37. const SHIPMENT_CANCEL_ORDER = 'v2/shipment/cancel_order';
  38. //获取商家寄件订单列表
  39. const SHIPMENT_INDEX = 'v2/shipment/index';
  40. //获取商家寄件订单预扣金额
  41. const SHIPMENT_PRICE = 'v2/shipment/price';
  42. /**
  43. * 初始化
  44. * @param array $config
  45. * @return mixed|void
  46. */
  47. protected function initialize(array $config = [])
  48. {
  49. parent::initialize($config); // TODO: Change the autogenerated stub
  50. }
  51. /**
  52. * 商家寄件获取快递公司
  53. * @return array|mixed
  54. * @author 等风来
  55. * @email 136327134@qq.com
  56. * @date 2023/5/15
  57. */
  58. public function getKuaidiComs()
  59. {
  60. $list = $this->accessToken->httpRequest(self::SHIPMENT_KUAIDI_NUMS, [], 'GET');
  61. foreach ($list as &$item) {
  62. $item['code'] = $item['value'];
  63. $item['value'] = $item['label'];
  64. $num = 1;
  65. foreach ($item['list'] as &$value) {
  66. $value['title'] = $item['label'] . '模版' . $num;
  67. $num++;
  68. }
  69. }
  70. return $list;
  71. }
  72. /**
  73. * 商家寄件创建订单
  74. * @param array $data
  75. * @return array|mixed
  76. * @author 等风来
  77. * @email 136327134@qq.com
  78. * @date 2023/5/15
  79. */
  80. public function shippmentCreateOrder(array $data)
  81. {
  82. $siid = sys_config('config_export_siid');
  83. $param = [
  84. 'kuaidicom' => $data['kuaidicom'],
  85. 'man_name' => $data['man_name'],
  86. 'phone' => $data['phone'],
  87. 'address' => $data['address'],
  88. 'send_real_name' => $data['send_real_name'],
  89. 'send_phone' => $data['send_phone'],
  90. 'send_address' => $data['send_address'],
  91. 'call_back_url' => sys_config('site_url') . '/api/order_call_back',
  92. 'return_type' => $siid ? '10' : '20',
  93. 'siid' => $siid,
  94. 'tempid' => $data['temp_id'],
  95. 'cargo' => $data['cargo'],
  96. 'weight' => $data['weight'],
  97. 'day_type' => $data['day_type'],
  98. 'pickup_start_time' => $data['pickup_start_time'],
  99. 'pickup_end_time' => $data['pickup_end_time'],
  100. ];
  101. return $this->accessToken->httpRequest(self::SHIPMENT_CREATE_ORDER, $param, 'post');
  102. }
  103. /**
  104. * 取消商家寄件订单
  105. * @param array $data
  106. * @return array|mixed
  107. * @author 等风来
  108. * @email 136327134@qq.com
  109. * @date 2023/5/15
  110. */
  111. public function shipmentCancelOrder(array $data)
  112. {
  113. $param = [
  114. 'task_id' => $data['task_id'],//快递100商家寄件任务id
  115. 'order_id' => $data['order_id'],//快递100商家寄件发起的订单号。并不是系统中的订单号
  116. 'cancel_msg' => $data['cancel_msg'],//取消原因
  117. ];
  118. return $this->accessToken->httpRequest(self::SHIPMENT_CANCEL_ORDER, $param);
  119. }
  120. /**
  121. * 获取商家寄件订单列表
  122. * @param array $data
  123. * @return array|mixed
  124. * @author 等风来
  125. * @email 136327134@qq.com
  126. * @date 2023/5/15
  127. */
  128. public function getShipmentOrderList(array $data)
  129. {
  130. $param = [
  131. 'kuaidi_num' => $data['kuaidi_num'] ?? '',
  132. 'courier_name' => $data['courier_name'] ?? '',
  133. 'page' => $data['page'] ?? 1,
  134. 'limit' => $data['limit'] ?? 10,
  135. ];
  136. return $this->accessToken->httpRequest(self::SHIPMENT_INDEX, $param, 'GET');
  137. }
  138. /**
  139. * @param array $data
  140. * @return array|mixed
  141. * @author 等风来
  142. * @email 136327134@qq.com
  143. * @date 2023/6/16
  144. */
  145. public function getPrice(array $data)
  146. {
  147. if (empty($data['kuaidicom'])) {
  148. throw new ApiException('快递编码必须填写');
  149. }
  150. if (empty($data['send_address'])) {
  151. throw new ApiException('寄件地址必须填写');
  152. }
  153. if (empty($data['address'])) {
  154. throw new ApiException('收件地址必须填写');
  155. }
  156. $param = [
  157. 'kuaidicom' => $data['kuaidicom'],
  158. 'send_address' => $data['send_address'],
  159. 'address' => $data['address'] ?? '',
  160. 'weight' => $data['weight'] ?? '',
  161. 'service_type' => $data['service_type'] ?? '',
  162. ];
  163. return $this->accessToken->httpRequest(self::SHIPMENT_PRICE, $param);
  164. }
  165. /**
  166. * 开通物流服务
  167. * @return bool|mixed
  168. */
  169. public function open()
  170. {
  171. return $this->accessToken->httpRequest(self::EXPRESS_OPEN, []);
  172. }
  173. /**
  174. * 获取电子面单模版
  175. * @param $com 快递公司编号
  176. * @param int $page
  177. * @param int $limit
  178. * @return bool|mixed
  179. */
  180. public function temp(string $com)
  181. {
  182. $param = [
  183. 'com' => $com
  184. ];
  185. $header = [];
  186. if (!sys_config('config_export_siid')) {
  187. $header = ['version:v1.1'];
  188. }
  189. return $this->accessToken->httpRequest(self::EXPRESS_TEMP, $param, 'GET', true, $header);
  190. }
  191. /**
  192. * 获取物流公司列表
  193. * @param int $type 快递类型:1,国内运输商;2,国际运输商;3,国际邮政
  194. * @return bool|mixed
  195. */
  196. public function express(int $type = 0, int $page = 0, int $limit = 20)
  197. {
  198. if ($type) {
  199. $param = [
  200. 'type' => $type,
  201. 'page' => $page,
  202. 'limit' => $limit
  203. ];
  204. } else {
  205. $param = [];
  206. }
  207. return $this->accessToken->httpRequest(self::EXPRESS_LIST, $param, 'GET');
  208. }
  209. /**
  210. * 查询物流信息
  211. * @param $com
  212. * @param $num
  213. * @return bool|mixed
  214. * @return 是否签收 ischeck
  215. * @return 物流状态:status 0在途,1揽收,2疑难,3签收,4退签,5派件,6退回,7转单,10待清关,11清关中,12已清关,13清关异常,14收件人拒签
  216. * @return 物流详情 content
  217. */
  218. public function query(string $num, string $com = '', $phone = '')
  219. {
  220. $param = [
  221. 'com' => $com,
  222. 'num' => $num,
  223. 'phone' => $phone
  224. ];
  225. if ($com === null) {
  226. unset($param['com']);
  227. }
  228. return $this->accessToken->httpRequest(self::EXPRESS_QUERY, $param, 'post');
  229. }
  230. /**
  231. * 电子面单打印
  232. * @param array $data 必需参数: com(快递公司编码)、to_name(寄件人)、to_tel(寄件人电话)、to_addr(寄件人详细地址)、from_name(收件人)、from_tel(收件人电话)、from_addr(收件人地址)、temp_id(电子面单模板ID)、siid(云打印机编号)、count(商品数量)
  233. * @return bool|mixed
  234. * @throws \think\db\exception\DataNotFoundException
  235. * @throws \think\db\exception\DbException
  236. * @throws \think\db\exception\ModelNotFoundException
  237. */
  238. public function dump($data)
  239. {
  240. $param = $data;
  241. $param['com'] = $data['com'] ?? '';
  242. if (!$param['com']) throw new AdminException(400713);
  243. $param['to_name'] = $data['to_name'] ?? '';
  244. $param['to_tel'] = $data['to_tel'] ?? '';
  245. $param['order_id'] = $data['order_id'] ?? '';
  246. $param['to_addr'] = $data['to_addr'] ?? '';
  247. if (!$param['to_addr'] || !$param['to_tel'] || !$param['to_name']) throw new AdminException(400714);
  248. $param['from_name'] = $data['from_name'] ?? '';
  249. $param['from_tel'] = $data['from_tel'] ?? '';
  250. $param['from_addr'] = $data['from_addr'] ?? '';
  251. if (!$param['from_name'] || !$param['from_tel'] || !$param['from_addr']) throw new AdminException(400715);
  252. $param['temp_id'] = $data['temp_id'] ?? '';
  253. if (!$param['temp_id']) {
  254. throw new AdminException(400712);
  255. }
  256. $param['siid'] = sys_config('config_export_siid');
  257. // if (!$param['siid']) {
  258. // throw new AdminException(400716);
  259. // }
  260. $param['count'] = $data['count'] ?? '';
  261. $param['cargo'] = $data['cargo'] ?? '';
  262. if (!$param['count']) {
  263. throw new AdminException(400717);
  264. }
  265. /** @var ExpressServices $expressServices */
  266. $expressServices = app()->make(ExpressServices::class);
  267. $expressData = $expressServices->getOneByWhere(['code' => $param['com']])->toArray();
  268. if (isset($data['cargo'])) $param['cargo'] = $data['cargo'];
  269. if ($expressData['partner_id'] == 1) $param['partner_id'] = $expressData['account'];
  270. if ($expressData['partner_key'] == 1) $param['partner_key'] = $expressData['key'];
  271. if ($expressData['net'] == 1) $param['net'] = $expressData['net_name'];
  272. if ($expressData['check_man'] == 1) $param['checkMan'] = $expressData['courier_name'];
  273. if ($expressData['partner_name'] == 1) $param['partnerName'] = $expressData['customer_name'];
  274. if ($expressData['is_code'] == 1) $param['code'] = $expressData['code_name'];
  275. if (!$data['siid']) {
  276. $param['print_type'] = 'IMAGE';
  277. }
  278. $header = [];
  279. if (!sys_config('config_export_siid')) {
  280. $header = ['version:v1.1'];
  281. }
  282. return $this->accessToken->httpRequest(self::EXPRESS_DUMP, $param, 'POST', true, $header);
  283. }
  284. }