OrderShippingInfo.php 9.0 KB

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