OrderClient.php 7.9 KB

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