From 6d8adb0700045ecdd9a74b88df3367d9a0c423f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=B8=96=E7=95=8C?= <642747453@qq.com> Date: Thu, 27 Jul 2023 21:28:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20#10000=20=E4=B8=8B=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/Test.php | 3 +- app/Http/Controllers/Shop/ShopsController.php | 1 + app/Models/ShopShip.php | 5 + app/Models/Waybill.php | 10 + .../Business/KuaiTuanTuan/FaceSheet.php | 42 +--- app/Services/Ship/WayBill.php | 220 ++++++++++++++++++ ...07_26_184009_create_shop_senders_table.php | 1 + ...023_07_27_184020_create_waybills_table.php | 69 ++++++ .../frontend/src/views/plat/orderList.vue | 12 +- 9 files changed, 321 insertions(+), 42 deletions(-) create mode 100644 app/Models/Waybill.php create mode 100644 app/Services/Ship/WayBill.php create mode 100644 database/migrations/2023_07_27_184020_create_waybills_table.php diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index e1c3d4a..5f4eaec 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -48,9 +48,9 @@ class Test extends Command { parent::__construct(); } + public function handle() { - } /** @@ -110,6 +110,7 @@ class Test extends Command foreach ($resp['pdd_waybill_search_response']['waybill_apply_subscription_cols'] as $subCols) { foreach ($subCols['branch_account_cols'] as $accountCols) { foreach ($accountCols['shipp_address_cols'] as $item) { + $item['wp_code'] = $subCols['wp_code']; ShopSender::query()->updateOrCreate( ['shop_id' => $shopId, 'shop_ship_id' => $shopShip->id, 'detail' => $item['detail']], $item diff --git a/app/Http/Controllers/Shop/ShopsController.php b/app/Http/Controllers/Shop/ShopsController.php index 1793cc7..1688b6a 100644 --- a/app/Http/Controllers/Shop/ShopsController.php +++ b/app/Http/Controllers/Shop/ShopsController.php @@ -246,6 +246,7 @@ class ShopsController extends Controller foreach ($resp['pdd_waybill_search_response']['waybill_apply_subscription_cols'] as $subCols) { foreach ($subCols['branch_account_cols'] as $accountCols) { foreach ($accountCols['shipp_address_cols'] as $item) { + $item['wp_code'] = $subCols['wp_code']; ShopSender::query()->updateOrCreate( ['shop_id' => $shopId, 'shop_ship_id' => $shopShip->id, 'detail' => $item['detail']], $item diff --git a/app/Models/ShopShip.php b/app/Models/ShopShip.php index 539a526..e760a2f 100644 --- a/app/Models/ShopShip.php +++ b/app/Models/ShopShip.php @@ -31,4 +31,9 @@ class ShopShip extends Model { return $value ? date('Y-m-d H:i:s', $value) : ''; } + + public function senders() + { + return $this->hasMany(ShopSender::class, 'shop_ship_id'); + } } diff --git a/app/Models/Waybill.php b/app/Models/Waybill.php new file mode 100644 index 0000000..a7d3e7e --- /dev/null +++ b/app/Models/Waybill.php @@ -0,0 +1,10 @@ +doRequest($type, $appendParams); } - public function getWayBill() + public function getWayBill($sender, $orderInfo, $wpCode) { $type = 'pdd.waybill.get'; $appendParams = [ 'param_waybill_cloud_print_apply_new_request' => [ - 'sender' => [ - 'address' => [ - 'city' => '昆明市', - 'country' => '中国', - 'detail' => '肖家营花卉市场1期-3', - 'district' => '官渡区', - 'province' => '云南省', - ], - 'name' => '赵先生', - 'mobile' => '13093715108', - ], - 'trade_order_info_dtos' => [[ - 'object_id' => 'SF_' . time(), - 'order_info' => [ - 'order_channels_type' => 'PDD', - 'trade_order_list' => ['PO-230712-007140873850822'] - ], - 'package_info' => [ - 'items' => [[ - 'count' => 1, - 'name' => '花落花卉', - ]] - ], - 'recipient' => [ - 'address' => [ - 'city' => '成都市', - 'detail' => '河之洲3期6栋2单元', - 'district' => '崇州市', - 'province' => '四川省', - ], - 'name' => '吕鸶', - 'mobile' => '13194975115' - ], - 'template_url' => 'https://file-link.pinduoduo.com/sf_one', - 'user_id' => '2256799818137902', - ]], - 'wp_code' => 'SF', + 'sender' => $sender, + 'trade_order_info_dtos' => [$orderInfo], + 'wp_code' => $wpCode, ] ]; $appendParams['param_waybill_cloud_print_apply_new_request'] = json_encode($appendParams['param_waybill_cloud_print_apply_new_request'], 256); diff --git a/app/Services/Ship/WayBill.php b/app/Services/Ship/WayBill.php new file mode 100644 index 0000000..f895435 --- /dev/null +++ b/app/Services/Ship/WayBill.php @@ -0,0 +1,220 @@ +orders as $shopId => $order) { + $shop = $this->getShop($shopId); + $faceSheet = new FaceSheet(); + $faceSheet->setShop($shop); + foreach ($order as $item) { + [$sender, $orderInfo, $wpCode] = $this->prepareRequest($item, $shop); + $waybill = $this->saveWayBill($item, $shop); + $resp = $faceSheet->getWayBill($sender, $orderInfo, $wpCode); + if (isset($resp['pdd_waybill_get_response'])) { + $data = $resp['pdd_waybill_get_response']['modules'][0]; + $data = json_decode($data['print_data'], true); + $waybill->request_id = $resp['pdd_waybill_get_response']['request_id']; + $waybill->encryptedData = $data['encryptedData']; + $waybill->signature = $data['signature']; + $waybill->templateUrl = $data['templateUrl']; + $waybill->ver = $data['ver']; + $waybill->waybill_code = $data['waybill_code']; + $waybill->save(); + } + } + } + } + + private function saveWayBill($order, $shop) + { + $senderConfig = $shop->senders[0]; + $waybill = new Waybill(); + $waybill->shop_id = $shop->id; + $waybill->object_id = $this->objectId; + + $waybill->sender_country = $senderConfig['country']; + $waybill->sender_province = $senderConfig['province']; + $waybill->sender_city = $senderConfig['city']; + $waybill->sender_district = $senderConfig['district']; + $waybill->sender_detail = $senderConfig['detail']; + $waybill->sender_name = $senderConfig['name']; + $waybill->sender_mobile = $senderConfig['mobile']; + + $waybill->recipient_province = $order['recipient_province']; + $waybill->recipient_city = $order['recipient_city']; + $waybill->recipient_district = $order['recipient_district']; + $waybill->recipient_detail = $order['recipient_detail']; + $waybill->recipient_name = $order['recipient_name']; + $waybill->recipient_mobile = $order['recipient_mobile']; + + $waybill->user_id = $shop->owner_id; + $waybill->wp_code = $senderConfig['wp_code']; + $waybill->order_sn = $order['order_sn']; + $waybill->items = json_encode($order['items'], 256); + + return $waybill; + } + + private function prepareRequest($order, $shop) + { + $senderConfig = $shop->senders[0]; + $sender = [ + 'address' => [ + 'city' => $senderConfig['city'], + 'country' => $senderConfig['country'], + 'detail' => $senderConfig['detail'], + 'district' => $senderConfig['district'], + 'province' => $senderConfig['province'], + ], + 'name' => $senderConfig['name'], + 'mobile' => $senderConfig['mobile'], + ]; + + $this->setObjectId(); + + $items = []; + foreach ($order['items'] as $item) { + if ($item['should_print']) { + $items[] = [ + 'name' => $item['name'], + 'count' => $item['count'], + ]; + } + } + + $orderInfo = [ + 'object_id' => $this->objectId, + 'order_info' => [ + 'order_channels_type' => 'PDD', + 'trade_order_list' => [$order['order_sn']], + ], + 'package_info' => [ + 'items' => $items, + ], + 'recipient' => [ + 'address' => [ + 'city' => $order['recipient_city'], + 'detail' => $order['recipient_detail'], + 'district' => $order['recipient_district'], + 'province' => $order['recipient_province'], + ], + 'name' => $order['recipient_name'], + 'mobile' => $order['recipient_mobile'], + ], + 'template_url' => $this->templateUrl, + 'user_id' => $shop->owner_id, + ]; + + return [$sender, $orderInfo, $senderConfig['wp_code']]; + } + + public function setObjectId() + { + $this->objectId = date('YmdHis') . str_pad(mt_rand(1, 9999999), 7, '0', STR_PAD_LEFT); + + return $this; + } + + public function setOrders($orders) + { + $orders = $orders->toArray(); + // 取出所有组合商品编码 + $combinationExternalIds = []; + foreach ($orders as $order) { + foreach ($order['items'] as $item) { + if (false !== strpos($item['external_sku_id'], 'Z')) { + $combinationExternalIds[] = $item['external_sku_id']; + } + } + } + // 获取组合商品详情 + $combinations = []; + if ($combinationExternalIds) { + $goodsSkus = GoodsSku::query() + ->select(['id', 'external_sku_id']) + ->with([ + 'combinationGoods:id,goods_sku_id,item_id,item_num', + 'combinationGoods.goodsSkuItem:id,external_sku_id', + ]) + ->whereIn('external_sku_id', $combinationExternalIds) + ->get(); + foreach ($goodsSkus as $goodsSku) { + foreach ($goodsSku['combination_goods'] as $item) { + $combinations[$goodsSku['external_sku_id']][] = [ + 'count' => $item['item_num'], + 'external_sku_id' => $item['goods_sku_item']['external_sku_id'], + ]; + } + } + } + // 订单拆分组合 + foreach ($orders as $order) { + $info = [ + 'order_sn' => $order['order_sn'], + 'recipient_province' => $order['receiver_address_province'], + 'recipient_city' => $order['receiver_address_city'], + 'recipient_district' => $order['receiver_address_district'], + 'recipient_detail' => $order['receiver_address_detail'], + 'recipient_name' => $order['receiver_name'], + 'recipient_mobile' => $order['receiver_mobile'], + 'items' => [] + ]; + foreach ($order['items'] as $item) { + $count = $item['goods_number'] - $item['already_cancel_number']; + if (isset($combinations[$item['external_sku_id']])) { + $info['items'][] = [ + 'is_single' => false, + 'should_print' => true, + 'name' => $item['goods_name'], + 'count' => $count, + 'external_sku_id' => $item['external_sku_id'], + ]; + foreach ($combinations[$item['external_sku_id']] as $combinationItem) { + $info['items'][] = [ + 'is_single' => true, + 'should_print' => false, + 'count' => $count * $combinationItem['count'], + 'external_sku_id' => $combinationItem['external_sku_id'], + ]; + } + } else { + $info['items'][] = [ + 'is_single' => true, + 'should_print' => true, + 'name' => $item['goods_name'], + 'count' => $count, + 'external_sku_id' => $item['external_sku_id'], + ]; + } + } + $this->orders[$order['shop_id']][] = $info; + } + + return $this; + } + + private function getShop($shopId) + { + return ShopShip::query() + ->select(['id', 'shop_id', 'access_token', 'owner_id']) + ->where('shop_id', $shopId) + ->with([ + 'senders' => function ($query) { + $query->orderBy('sort')->first(); + } + ]) + ->get(); + } +} diff --git a/database/migrations/2023_07_26_184009_create_shop_senders_table.php b/database/migrations/2023_07_26_184009_create_shop_senders_table.php index 51f97c7..61a2e9e 100644 --- a/database/migrations/2023_07_26_184009_create_shop_senders_table.php +++ b/database/migrations/2023_07_26_184009_create_shop_senders_table.php @@ -29,6 +29,7 @@ class CreateShopSendersTable extends Migration $table->integer('mobile')->nullable(); $table->integer('sort')->default(1); $table->integer('status')->default(1); + $table->string('wp_code')->default(''); $table->timestamps(); }); } diff --git a/database/migrations/2023_07_27_184020_create_waybills_table.php b/database/migrations/2023_07_27_184020_create_waybills_table.php new file mode 100644 index 0000000..b0e2051 --- /dev/null +++ b/database/migrations/2023_07_27_184020_create_waybills_table.php @@ -0,0 +1,69 @@ +bigIncrements('id'); + $table->integer('shop_id'); + $table->string('request_id')->default(''); + $table->text('encryptedData')->nullable(); + $table->string('signature')->default(''); + $table->string('templateUrl'); + $table->string('ver')->default(''); + $table->string('waybill_code')->default(''); + $table->string('object_id', 32); + $table->integer('key')->default(0); + + $table->string('sender_country'); + $table->string('sender_province'); + $table->string('sender_city'); + $table->string('sender_district'); + $table->string('sender_detail'); + $table->string('sender_name'); + $table->integer('sender_mobile'); + + $table->string('recipient_country')->default(''); + $table->string('recipient_province'); + $table->string('recipient_city'); + $table->string('recipient_district'); + $table->string('recipient_detail'); + $table->string('recipient_name'); + $table->integer('recipient_mobile'); + + $table->bigInteger('user_id'); + $table->string('wp_code'); + $table->string('order_channels_type')->default('PDD'); + + $table->string('order_sn'); + $table->text('items'); + + $table->timestamps(); + + $table->index('object_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('waybills'); + } +} diff --git a/resources/frontend/src/views/plat/orderList.vue b/resources/frontend/src/views/plat/orderList.vue index 40e06c1..187aee6 100644 --- a/resources/frontend/src/views/plat/orderList.vue +++ b/resources/frontend/src/views/plat/orderList.vue @@ -98,7 +98,10 @@ - + + + - + @@ -223,11 +226,14 @@ export default { }, mounted() { // 展示店铺列表 - this.getPlatOrderList(); this.getShopsList(); }, created() { this.initConfirmAt(); + this.getPlatOrderList({ + confirm_at_start: this.form.confirm_at[0], + confirm_at_end: this.form.confirm_at[1] + }); }, methods: { initConfirmAt() {