OrderClient.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace crmeb\services\easywechat\orderShipping;
  3. use crmeb\exceptions\AdminException;
  4. use crmeb\services\CacheService;
  5. use EasyWeChat\Core\AccessToken;
  6. use EasyWeChat\Core\Exceptions\HttpException;
  7. use think\facade\Cache;
  8. class OrderClient extends BaseOrder
  9. {
  10. const redis_prefix = 'mini_order';
  11. const express_company = 'ZTO'; // 默认发货快递公司为(中通快递)
  12. /**
  13. * @var \Redis
  14. */
  15. protected $redis;
  16. /**
  17. * @return object|\Redis|null
  18. *
  19. * @date 2023/05/10
  20. * @author yyw
  21. */
  22. protected function getRedis()
  23. {
  24. if (empty($this->redis)) {
  25. $this->redis = Cache::store('redis')->handler();
  26. }
  27. return $this->redis;
  28. }
  29. /**
  30. * 处理联系人
  31. * @param array $contact
  32. * @return array
  33. *
  34. * @date 2023/05/10
  35. * @author yyw
  36. */
  37. protected function handleContact(array $contact = []): array
  38. {
  39. if (isset($contact)) {
  40. if (isset($contact['consignor_contact']) && $contact['consignor_contact']) {
  41. $contact['consignor_contact'] = Utility::encryptTel($contact['consignor_contact']);
  42. }
  43. if (isset($contact['receiver_contact']) && $contact['receiver_contact']) {
  44. $contact['receiver_contact'] = Utility::encryptTel($contact['receiver_contact']);
  45. }
  46. }
  47. return $contact;
  48. }
  49. /**
  50. * 发货
  51. * @param string $out_trade_no
  52. * @param int $logistics_type
  53. * @param array $shipping_list
  54. * @param string $payer_openid
  55. * @param int $delivery_mode
  56. * @param bool $is_all_delivered
  57. * @return array
  58. * @throws HttpException
  59. *
  60. * @date 2023/05/10
  61. * @author yyw
  62. */
  63. public function shippingByTradeNo(string $out_trade_no, int $logistics_type, array $shipping_list, string $payer_openid, $path, int $delivery_mode = 1, bool $is_all_delivered = true)
  64. {
  65. if (!$this->checkManaged()) {
  66. throw new AdminException('开通小程序订单管理服务后重试');
  67. }
  68. $params = [
  69. 'order_key' => [
  70. 'order_number_type' => 1,
  71. 'mchid' => $this->config['config']['mini_program']['merchant_id'],
  72. 'out_trade_no' => $out_trade_no,
  73. ],
  74. 'logistics_type' => $logistics_type,
  75. 'delivery_mode' => $delivery_mode,
  76. 'upload_time' => date(DATE_RFC3339),
  77. 'payer' => [
  78. 'openid' => $payer_openid
  79. ]
  80. ];
  81. if ($delivery_mode == 2) {
  82. $params['is_all_delivered'] = $is_all_delivered;
  83. }
  84. foreach ($shipping_list as $shipping) {
  85. $contact = $this->handleContact($shipping['contact'] ?? []);
  86. $params['shipping_list'][] = [
  87. 'tracking_no' => $shipping['tracking_no'] ?? '',
  88. 'express_company' => isset($shipping['express_company']) ? $this->getDelivery($shipping['express_company']) : '',
  89. 'item_desc' => $shipping['item_desc'],
  90. 'contact' => $contact
  91. ];
  92. }
  93. // 跳转路径
  94. // $this->setMesJumpPath($path);
  95. return $this->shipping($params);
  96. }
  97. /**
  98. * 合单
  99. * @param string $out_trade_no
  100. * @param int $logistics_type
  101. * @param array $sub_orders
  102. * @param string $payer_openid
  103. * @param int $delivery_mode
  104. * @param bool $is_all_delivered
  105. * @return array
  106. * @throws HttpException
  107. *
  108. * @date 2023/05/10
  109. * @author yyw
  110. */
  111. public function combinedShippingByTradeNo(string $out_trade_no, int $logistics_type, array $sub_orders, string $payer_openid, int $delivery_mode = 2, bool $is_all_delivered = false)
  112. {
  113. if (!$this->checkManaged()) {
  114. throw new AdminException('开通小程序订单管理服务后重试');
  115. }
  116. $params = [
  117. 'order_key' => [
  118. 'order_number_type' => 1,
  119. 'mchid' => $this->config['config']['mini_program']['merchant_id'],
  120. 'out_trade_no' => $out_trade_no,
  121. ],
  122. 'upload_time' => date(DATE_RFC3339),
  123. 'payer' => [
  124. 'openid' => $payer_openid
  125. ]
  126. ];
  127. foreach ($sub_orders as $order) {
  128. $sub_order = [
  129. 'order_key' => [
  130. 'order_number_type' => 1,
  131. 'mchid' => $this->config['config']['mini_program']['merchant_id'],
  132. 'out_trade_no' => $order['out_trade_no'],
  133. 'logistics_type' => $logistics_type,
  134. ],
  135. 'delivery_mode' => $delivery_mode,
  136. 'is_all_delivered' => $is_all_delivered
  137. ];
  138. foreach ($sub_orders['shipping_list'] as $shipping) {
  139. $contact = $this->handleContact($shipping['contact'] ?? []);
  140. $sub_order['shipping_list'][] = [
  141. 'tracking_no' => $shipping['tracking_no'] ?? '',
  142. 'express_company' => isset($shipping['express_company']) ? $this->getDelivery($shipping['express_company']) : '',
  143. 'item_desc' => $shipping['item_desc'],
  144. 'contact' => $contact
  145. ];
  146. }
  147. $params['sub_orders'][] = $sub_order;
  148. }
  149. return $this->combinedShipping($params);
  150. }
  151. /**
  152. * 签收通知
  153. * @param string $merchant_trade_no
  154. * @param string $received_time
  155. * @return array
  156. * @throws HttpException
  157. *
  158. * @date 2023/05/10
  159. * @author yyw
  160. */
  161. public function notifyConfirmByTradeNo(string $merchant_trade_no, string $received_time)
  162. {
  163. $params = [
  164. 'merchant_id' => $this->config['config']['mini_program']['merchant_id'],
  165. 'merchant_trade_no' => $merchant_trade_no,
  166. 'received_time' => $received_time
  167. ];
  168. return $this->notifyConfirm($params);
  169. }
  170. /**
  171. * 设置跳转连接
  172. * @param $path
  173. * @return array
  174. * @throws \EasyWeChat\Core\Exceptions\HttpException
  175. *
  176. * @date 2023/05/10
  177. * @author yyw
  178. */
  179. public function setMesJumpPathAndCheck($path)
  180. {
  181. if (!$this->checkManaged()) {
  182. throw new AdminException('开通小程序订单管理服务后重试');
  183. }
  184. return $this->setMesJumpPath($path);
  185. }
  186. /**
  187. * 设置小程序管理服务开通状态
  188. * @return bool
  189. * @throws HttpException
  190. *
  191. * @date 2023/05/09
  192. * @author yyw
  193. */
  194. public function setManaged()
  195. {
  196. $res = $this->isManaged();
  197. if ($res['is_trade_managed']) {
  198. $key = self::redis_prefix . '_is_trade_managed';
  199. $this->getRedis()->set($key, $res['is_trade_managed']);
  200. return true;
  201. } else {
  202. return false;
  203. }
  204. }
  205. /**
  206. * @return bool
  207. * @throws HttpException
  208. *
  209. * @date 2023/05/10
  210. * @author yyw
  211. */
  212. public function checkManaged()
  213. {
  214. $key = self::redis_prefix . '_is_trade_managed';
  215. if ($this->getRedis()->exists($key)) {
  216. return true;
  217. } else {
  218. return $this->setManaged();
  219. }
  220. }
  221. /**
  222. * 同步去微信物流列表
  223. * @return array
  224. * @throws HttpException
  225. *
  226. * @date 2023/05/10
  227. * @author yyw
  228. */
  229. public function setDeliveryList()
  230. {
  231. $list = $this->getDeliveryList();
  232. if ($list) {
  233. $key = self::redis_prefix . '_delivery_list';
  234. $date = array_column($list['delivery_list'], 'delivery_id', 'delivery_name');
  235. // 创建缓存
  236. $this->getRedis()->hMSet($key, $date);
  237. return $date;
  238. } else {
  239. throw new AdminException('物流公司列表异常');
  240. }
  241. }
  242. /**
  243. * 获取物流公司编码
  244. * @param $company_name
  245. * @return array|mixed
  246. * @throws HttpException
  247. *
  248. * @date 2023/05/10
  249. * @author yyw
  250. */
  251. public function getDelivery($company_name)
  252. {
  253. $key = self::redis_prefix . '_delivery_list';
  254. if (!$this->getRedis()->exists($key)) {
  255. $date = $this->setDeliveryList();
  256. if (!isset($date[$company_name])) {
  257. // throw new AdminException('物流公司异常1');
  258. }
  259. $express_company = $date[$company_name];
  260. } else {
  261. $express_company = $this->getRedis()->hMGet($key, [$company_name])[$company_name] ?? '';
  262. }
  263. if (empty($express_company)) {
  264. // throw new AdminException('物流公司异常2');
  265. $express_company = self::express_company;
  266. }
  267. return $express_company;
  268. }
  269. }