OrderClient.php 8.3 KB

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