diff --git a/app/Events/CancelLogisticEvent.php b/app/Events/CancelLogisticEvent.php new file mode 100644 index 0000000..6801b08 --- /dev/null +++ b/app/Events/CancelLogisticEvent.php @@ -0,0 +1,40 @@ +shopId = $shopId; + $this->orderSn = $orderSn; + } + + /** + * Get the channels the event should broadcast on. + * + * @return \Illuminate\Broadcasting\Channel|array + */ + public function broadcastOn() + { + return new PrivateChannel('channel-name'); + } +} diff --git a/app/Http/Controllers/Business/BusinessOrderController.php b/app/Http/Controllers/Business/BusinessOrderController.php index 4653c5d..12955fc 100644 --- a/app/Http/Controllers/Business/BusinessOrderController.php +++ b/app/Http/Controllers/Business/BusinessOrderController.php @@ -179,7 +179,6 @@ class BusinessOrderController extends Controller public function print(Request $request) { - $shopIds = Shop::query() ->where('plat_id', Shop::$PLAT_KTT) ->pluck('id'); diff --git a/app/Http/Controllers/Business/WaybillController.php b/app/Http/Controllers/Business/WaybillController.php index 0dd4ad1..9765351 100644 --- a/app/Http/Controllers/Business/WaybillController.php +++ b/app/Http/Controllers/Business/WaybillController.php @@ -2,6 +2,8 @@ namespace App\Http\Controllers\Business; +use App\Events\BusinessOrderCancelEvent; +use App\Events\CancelLogisticEvent; use App\Exports\BusinessOrderExport; use App\Exports\OrderBlankExport; use App\Http\Controllers\Controller; @@ -10,6 +12,7 @@ use App\Models\BusinessOrderItem; use App\Models\GoodsSku; use App\Models\Shop; use App\Models\Waybill; +use App\Services\Business\BusinessFactory; use App\Services\WayBill\JingDong\JingDongService; use App\Services\WayBill\JingDong\WayBillService; use App\Utils\DateTimeUtils; @@ -34,5 +37,38 @@ class WaybillController extends Controller return $jingDongService->queryTrace($waybill); } + public function cancel(Request $request) + { + $params = $request->validate([ + 'order_id' => 'required', + ], [ + 'order_id.required' => '订单id', + ]); + $waybill = Waybill::query()->where("order_id", $params['order_id'])->firstOrFail(); + if (empty($waybill->waybill_code)) { + throw new \Exception("无快递单号可取消"); + } + try{ + //取消快团团物流 + $shop = Shop::query()->findOrFail($waybill->shop_id); + $client = BusinessFactory::init()->make($shop['plat_id'])->setShop($shop); + $client->cancelLogistic($waybill->order_sn, $waybill->waybill_code); + //取消京东物流 + $jingDongService = new JingDongService(); + $jingDongService->cancelOrder($waybill); + }catch(\Exception $exception) { + Log::error("取消快团团或者京东物流异常", ["error" => $exception->getMessage()]); + } + + + $waybill->waybill_code = ''; + $waybill->encryptedData = ''; + $waybill->status = Waybill::$STATUS_INIT; + $waybill->cancel = 1; + $waybill->save(); + BusinessOrder::query()->where("id", $params['order_id'])->update(['print_status' => 0]); + + return response(['message' => 'success']); + } } diff --git a/app/Listeners/CancelLogisticListener.php b/app/Listeners/CancelLogisticListener.php index d894b00..fa847e8 100644 --- a/app/Listeners/CancelLogisticListener.php +++ b/app/Listeners/CancelLogisticListener.php @@ -2,6 +2,7 @@ namespace App\Listeners; +use App\Events\CancelLogisticEvent; use App\Events\CreateLogisticEvent; use App\Models\Shop; use App\Models\Waybill; @@ -11,9 +12,6 @@ use Illuminate\Queue\InteractsWithQueue; class CancelLogisticListener implements ShouldQueue { - public $connection = 'redis'; - - public $queue = 'listeners'; /** * Create the event listener. @@ -31,7 +29,7 @@ class CancelLogisticListener implements ShouldQueue * @param CreateLogisticEvent $event * @return void */ - public function handle(CreateLogisticEvent $event) + public function handle(CancelLogisticEvent $event) { $waybillNo = Waybill::query() ->where('shop_id', $event->shopId) diff --git a/app/Listeners/CreateLogisticListener.php b/app/Listeners/CreateLogisticListener.php index 7c07758..183f9cc 100644 --- a/app/Listeners/CreateLogisticListener.php +++ b/app/Listeners/CreateLogisticListener.php @@ -11,10 +11,6 @@ use Illuminate\Queue\InteractsWithQueue; class CreateLogisticListener implements ShouldQueue { - public $connection = 'redis'; - - public $queue = 'listeners'; - /** * Create the event listener. * @@ -35,6 +31,6 @@ class CreateLogisticListener implements ShouldQueue { $shop = Shop::query()->findOrFail($event->shopId);; $client = BusinessFactory::init()->make($shop['plat_id'])->setShop($shop); - //$client->createLogistic($event->orderSn, $event->waybillNo); + $client->createLogistic($event->orderSn, $event->waybillNo); } } diff --git a/app/Models/Waybill.php b/app/Models/Waybill.php index b920338..0b92974 100644 --- a/app/Models/Waybill.php +++ b/app/Models/Waybill.php @@ -11,6 +11,7 @@ class Waybill extends Model public static $BUSINESS_EXPRESS_CODE = 247; public static $AIR_FREIGHT_CODE = 266; + public static $STATUS_INIT = 0; public static $STATUS_CREATE_WAYBILL_CODE = 1; public static $STATUS_CREATE_WAYBILL_ENCRYPTED_DATA = 2; public static $STATUS_PRINT_SUCCESS = 3; diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 4947c35..5c6abd0 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -3,6 +3,7 @@ namespace App\Providers; use App\Events\BusinessOrdersUpdate; +use App\Events\CancelLogisticEvent; use App\Events\StockUpdateEvent; use App\Events\GroupSetEvent; use App\Events\BatchStockUpdateEvent; @@ -47,6 +48,9 @@ class EventServiceProvider extends ServiceProvider CreateLogisticEvent::class => [ CreateLogisticListener::class ], + CancelLogisticEvent::class => [ + CancelLogisticListener::class + ], ]; /** diff --git a/app/Services/Business/BusinessClient.php b/app/Services/Business/BusinessClient.php index 1cbe5fc..99f8ef7 100644 --- a/app/Services/Business/BusinessClient.php +++ b/app/Services/Business/BusinessClient.php @@ -4,6 +4,7 @@ namespace App\Services\Business; use App\Events\BusinessOrderCancelEvent; use App\Events\BusinessOrdersUpdate; +use App\Events\CancelLogisticEvent; use App\Models\BusinessGoodsSku; use App\Models\BusinessOrder; use App\Models\BusinessOrderItem; @@ -55,7 +56,7 @@ abstract class BusinessClient $orderRecord->update($order); } if (!empty($order['cancel_status'])) { - event(new BusinessOrderCancelEvent($shopId, $order['order_sn'])); + event(new CancelLogisticEvent($shopId, $order['order_sn'])); } $goodsSkuNum = 0; foreach ($order['sub_order_list'] as $item) { @@ -154,7 +155,7 @@ abstract class BusinessClient 'headers' => ['Content-type' => 'application/x-www-form-urlencoded;charset=UTF-8'], 'form_params' => $params ]; - $res = (new Client())->request($method, $url, $headers); + $res = (new Client(['verify'=>false]))->request($method, $url, $headers); $size = $res->getBody()->getSize(); $res = json_decode($res->getBody()->getContents(), true); $paramsJson = json_encode($params, 256); diff --git a/routes/api.php b/routes/api.php index 84eb2e8..02cd6e0 100644 --- a/routes/api.php +++ b/routes/api.php @@ -73,6 +73,7 @@ Route::middleware(['auth:api', 'check.permissions'])->group(function () { Route::get('print/orders', [BusinessOrderController::class, 'print'])->name('order.print'); Route::put('print/success', [BusinessOrderController::class, 'printSuccess'])->name('order.printSuccess'); Route::get('waybill/queryTrace', [\App\Http\Controllers\Business\WaybillController::class, 'queryTrace']); + Route::post('waybill/cancel', [\App\Http\Controllers\Business\WaybillController::class, 'cancel']); // 发货信息 Route::get('shop/ship/senders', [ShipController::class, 'getSenders'])->name('shop_ship.senders.get'); Route::post('shop/ship/senders', [ShipController::class, 'saveSenders'])->name('shop_ship.senders.save'); diff --git a/routes/console.php b/routes/console.php index 75dd0cd..dff17a1 100644 --- a/routes/console.php +++ b/routes/console.php @@ -1,5 +1,6 @@ find(6582); + BusinessFactory::init()->make($order->shop->plat_id)->setShop($order->shop)->queryStatusAndSync($order); $this->comment(Inspiring::quote()); })->describe('Display an inspiring quote');