Express.php 11 KB

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