OrderShippingInfo.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace app\common\Service\Order\ShippingInfo;
  3. use app\common\model\OrderGoods as OrderGoodsModel;
  4. use app\common\model\OrderExpress as OrderExpressModel;
  5. use app\common\model\OrderAddress as OrderAddressModel;
  6. use app\common\Enum\OrderGoodsEnum;
  7. use app\common\Enum\GoodsEnum;
  8. class OrderShippingInfo extends Base
  9. {
  10. protected $orderItems = null;
  11. protected $deliveryTypes = [];
  12. /**
  13. * 获取整个订单的 shippingParams 参数
  14. *
  15. * @return array
  16. */
  17. public function getShippingParams()
  18. {
  19. $wechatPay = $this->getWechatPay();
  20. $this->setSendOrderItems();
  21. $uploadParams = [];
  22. if (in_array(GoodsEnum::DELIVERY_TYPE_EXPRESS, $this->deliveryTypes)) {
  23. // 有 快递物流 商品
  24. $expressUploadParams = $this->getExpressShippingParams();
  25. $uploadParams = array_merge($uploadParams, $expressUploadParams);
  26. }
  27. if (!$uploadParams && array_intersect(['autosend', 'custom'], $this->deliveryTypes)) {
  28. // 有 自动发货,或者手动发货 商品
  29. $virtualParams = $this->getVirtualShippingParams();
  30. $uploadParams[] = $virtualParams;
  31. }
  32. if (!$uploadParams && in_array('selfetch', $this->deliveryTypes)) {
  33. // 有 到店自提 商品
  34. $selfParams = $this->getSelfetchShippingParams();
  35. $uploadParams[] = $selfParams;
  36. }
  37. if (!$uploadParams && in_array('store_delivery', $this->deliveryTypes)) {
  38. // 有 店铺配送 商品
  39. $storeDeliveryParams = $this->getStoreDeliveryShippingParams();
  40. $uploadParams[] = $storeDeliveryParams;
  41. }
  42. // 处理微信相关参数
  43. return $this->setWechatParams($uploadParams, $wechatPay);
  44. }
  45. /**
  46. * 修改物流是获取指定 包裹的 shippingParams
  47. *
  48. * @param \think\Model $express
  49. * @return array
  50. */
  51. public function getChangeShippingParams($express)
  52. {
  53. $wechatPay = $this->getWechatPay();
  54. $this->setSendOrderItems();
  55. $orderExpresses = collection([$express]); // 指定包裹
  56. // 获取包裹的 params
  57. $uploadParams = $this->getExpressShippingParamsByExpresses($orderExpresses);
  58. // 处理微信相关参数
  59. return $this->setWechatParams($uploadParams, $wechatPay);
  60. }
  61. /**
  62. * 获取订单所有包裹的 shippingParams
  63. *
  64. * @return array
  65. */
  66. private function getExpressShippingParams()
  67. {
  68. $orderExpresses = collection(OrderExpressModel::where('order_id', $this->order['id'])->select());
  69. return $this->getExpressShippingParamsByExpresses($orderExpresses);
  70. }
  71. /**
  72. * 获取订单指定包裹的 shippingParams
  73. *
  74. * @param \think\Model $order
  75. * @param \think\Collection $orderExpresses
  76. * @return array
  77. */
  78. private function getExpressShippingParamsByExpresses($orderExpresses)
  79. {
  80. $uploadParams = [];
  81. if (!$orderExpresses->isEmpty()) {
  82. $orderAddress = OrderAddressModel::where('order_id', $this->order['id'])->find();
  83. $receiver_contact = $orderAddress ? mb_substr($orderAddress->mobile, 0, 3) . '****' . mb_substr($orderAddress->mobile, -4) : '130****0000';
  84. $shippingList = [];
  85. foreach ($orderExpresses as $orderExpress) {
  86. $currentItems = $this->getItemsByCondition('order_express_id', $orderExpress->id);
  87. $item_desc = [];
  88. foreach ($currentItems as $currentItem) {
  89. $item_desc[] = $currentItem['goods_title'] . '*' . $currentItem['nums'];
  90. }
  91. $item_desc = join(', ', $item_desc);
  92. $item_desc = mb_strlen($item_desc) > 110 ? mb_substr($item_desc, 0, 110) . ' 等商品' : $item_desc; // 处理字符串
  93. $shippingList[] = [
  94. 'tracking_no' => $orderExpress['express_no'],
  95. 'express_company' => $orderExpress['express_code'],
  96. 'item_desc' => $item_desc,
  97. 'contact' => [
  98. 'receiver_contact' => $receiver_contact
  99. ]
  100. ];
  101. }
  102. if ($shippingList) {
  103. // 发货
  104. $uploadParams[] = [
  105. 'logistics_type' => $this->getLogisticsType('express'),
  106. 'shipping_list' => $shippingList,
  107. ];
  108. }
  109. }
  110. return $uploadParams;
  111. }
  112. /**
  113. * 获取订单中虚拟商品的 shippingParams
  114. *
  115. * @return array
  116. */
  117. private function getVirtualShippingParams()
  118. {
  119. // 是否存在虚拟发货商品
  120. $virtualItems = $this->getItemsByCondition('_type', ['autosend', 'custom'], 'in_array');
  121. if (!$virtualItems->isEmpty()) {
  122. $shippingList = [];
  123. $item_desc = [];
  124. foreach ($virtualItems as $virtualItem) {
  125. $item_desc[] = $virtualItem['goods_title'] . '*' . $virtualItem['goods_num'];
  126. }
  127. $item_desc = join(', ', $item_desc);
  128. $item_desc = mb_strlen($item_desc) > 110 ? mb_substr($item_desc, 0, 110) . ' 等商品' : $item_desc; // 处理字符串
  129. $shippingList[] = [
  130. 'item_desc' => $item_desc,
  131. ];
  132. // 发货
  133. $currentParams = [
  134. 'logistics_type' => $this->getLogisticsType('autosend'),
  135. 'shipping_list' => $shippingList,
  136. ];
  137. }
  138. return $currentParams ?? null;
  139. }
  140. /**
  141. * 获取订单中到店自提商品的 shippingParams
  142. *
  143. * @return array
  144. */
  145. public function getSelfetchShippingParams()
  146. {
  147. // 到店自提商品
  148. $selfetchItems = $this->getItemsByCondition('delivery_type', ['selfetch'], 'in_array');
  149. if (!$selfetchItems->isEmpty()) {
  150. $shippingList = [];
  151. $item_desc = [];
  152. foreach ($selfetchItems as $selfetchItem) {
  153. $item_desc[] = $selfetchItem['goods_title'] . '*' . $selfetchItem['goods_num'];
  154. }
  155. $item_desc = join(', ', $item_desc);
  156. $item_desc = mb_strlen($item_desc) > 110 ? mb_substr($item_desc, 0, 110) . ' 等商品' : $item_desc; // 处理字符串
  157. $shippingList[] = [
  158. 'item_desc' => $item_desc,
  159. ];
  160. // 发货
  161. $currentParams = [
  162. 'logistics_type' => $this->getLogisticsType('selfetch'),
  163. 'shipping_list' => $shippingList,
  164. ];
  165. }
  166. return $currentParams ?? null;
  167. }
  168. /**
  169. * 获取订单中店铺配送商品的 shippingParams
  170. *
  171. * @return array
  172. */
  173. public function getStoreDeliveryShippingParams()
  174. {
  175. // 到店自提商品
  176. $storeDeliveryItems = $this->getItemsByCondition('delivery_type', ['store_delivery'], 'in_array');
  177. if (!$storeDeliveryItems->isEmpty()) {
  178. $shippingList = [];
  179. $item_desc = [];
  180. foreach ($storeDeliveryItems as $storeDeliveryItem) {
  181. $item_desc[] = $storeDeliveryItem['goods_title'] . '*' . $storeDeliveryItem['goods_num'];
  182. }
  183. $item_desc = join(', ', $item_desc);
  184. $item_desc = mb_strlen($item_desc) > 110 ? mb_substr($item_desc, 0, 110) . ' 等商品' : $item_desc; // 处理字符串
  185. $shippingList[] = [
  186. 'item_desc' => $item_desc,
  187. ];
  188. // 发货
  189. $currentParams = [
  190. 'logistics_type' => $this->getLogisticsType('store_delivery'),
  191. 'shipping_list' => $shippingList,
  192. ];
  193. }
  194. return $currentParams ?? null;
  195. }
  196. /**
  197. * 设置 orderItems (这里是订单中的所有 items)
  198. *
  199. * @return void
  200. */
  201. private function setSendOrderItems()
  202. {
  203. $orderItems = OrderGoodsModel::where('order_id', $this->order['id'])
  204. // ->where('refund_status', OrderItem::REFUND_STATUS_NOREFUND)
  205. ->whereIn('delivery_status',
  206. [
  207. OrderGoodsEnum::DELIVERY_STATUS_SENDED,
  208. OrderGoodsEnum::DELIVERY_STATUS_GETED
  209. ])
  210. ->select();
  211. $this->orderItems = $orderItems instanceof \think\Collection ? $orderItems : collection($orderItems);
  212. $this->deliveryTypes = array_values(array_unique(array_filter($this->orderItems->column('delivery_type'))));
  213. }
  214. /**
  215. * 根据条件获取指定 itemd
  216. *
  217. * @param string $field
  218. * @param mixed $value
  219. * @return \think\Collection
  220. */
  221. private function getItemsByCondition($field, $value, $exp = '')
  222. {
  223. $new = [];
  224. foreach ($this->orderItems as $item) {
  225. if ($exp == 'in_array') {
  226. if (in_array($item[$field], $value)) {
  227. $new[] = $item;
  228. }
  229. } else {
  230. if ($item[$field] == $value) {
  231. $new[] = $item;
  232. }
  233. }
  234. }
  235. return collection($new);
  236. }
  237. }