OrderShippingInfo.php 8.9 KB

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