京东打印

This commit is contained in:
杨建炊 2024-12-26 19:19:43 +08:00
parent 968e5137a8
commit e850b3d336
4 changed files with 58 additions and 75 deletions

View File

@ -189,12 +189,9 @@ class BusinessOrderController extends Controller
$waybill = new WayBillService();
$waybill->setOrders($businessOrders);
$contents = $waybill->getWayBillContents();
// 待打印数据
[$documents, $orderIds] = $waybill->getDocumentsAndOrderIds($contents);
return response([
'documents' => $this->combinationPrintDocuments($documents),
'order_ids' => implode(',', $orderIds),
'data' => $contents
]);
}

View File

@ -1,6 +1,6 @@
<?php
namespace App\Services\Ship\JingDong;
namespace App\Services\WayBill\JingDong;
use App\Enum\Pickup\ConsignType;
use App\Models\BusinessOrder;
@ -85,7 +85,7 @@ class JingDongService
$response = $client->execute($request, $options);
$response = json_decode($response->getBody(), true);
Log::info("京东返回请求response", [$response]);
if (!isset($response['code']) || ($response['code'] != 0 && $response['code'] != 1000)) {
if (!isset($response['code']) || ($response['code'] != 0 && $response['code'] != 1 && $response['code'] != 1000)) {
throw new \Exception($response['msg'] ?? "code异常");
}
return $response;
@ -95,13 +95,11 @@ class JingDongService
private function buildCargoes($extend)
{
return [
[
"quantity" => $extend["quantity"] ?? 1,
"goodsName" => "鲜花",
"volume" => ($extend['volume'] ?? 0) > 0 ? $extend['volume'] : 100,
"weight" => $extend['weight'] ?? 1,
"packageQty" => 1
]
];
}
@ -119,7 +117,7 @@ class JingDongService
$path = "/freshmedicinedelivery/delivery/create/order/v1";
$pickupData = $pickup->toArray();
$body = [
"orderId" => $pickupData['object_id'],
"orderId" => $pickupData['order_sn'],
"senderContactRequest" => [
'senderName' => $pickupData['sender_name'] ?? '',
'senderPhone' => $pickupData['sender_mobile'] ?? '',
@ -128,7 +126,7 @@ class JingDongService
],
"customerCode" => $this->customerCode,
"remark" => $pickup['note'] ?? '',
"salePlatform" => '0010001',//京东
"salePlatform" => '0030001',//其他平台
"cargoesRequest" => $this->buildCargoes($pickup),
"receiverContactRequest" => [
'receiverName' => $pickupData['recipient_name'] ?? '',
@ -141,7 +139,7 @@ class JingDongService
//"pickUpStartTime" => Carbon::today()->startOfDay()->addHours(19)->toDateTimeString(),
//"pickUpEndTime" => Carbon::today()->startOfDay()->addHours(21)->toDateTimeString(),
];
dd($body);
$response = $this->request($body, $path);
return $response['data'];
@ -162,6 +160,27 @@ class JingDongService
return $response['data'];
}
public function pullData(Waybill $pickup)
{
//云打印
$this->domain = "jdcloudprint";
$path = "/PullDataService/pullData";
$pickupData = $pickup->toArray();
$body = [
"cpCode" => "JD",
"wayBillInfos" => [[
'orderNo' => $pickupData['order_sn'],
'jdWayBillCode' => $pickupData['waybill_code'],
]],
"parameters" => ["ewCustomerCode" => $this->customerCode],
"objectId" => $pickupData['object_id'],
];
$response = $this->request($body, $path);
return $response['prePrintDatas'];
}
public function queryOrder(Waybill $pickup)
{
$path = "/ecap/v1/orders/details/query";

View File

@ -8,22 +8,14 @@ use App\Models\ShopSender;
use App\Models\ShopShip;
use App\Models\Waybill;
use App\Services\Business\KuaiTuanTuan\FaceSheet;
use App\Services\Ship\JingDong\JingDongService;
class WayBillService
{
public $orders;
public $objectId;
public $adminUser;
// 快递便携式一联单
public $sfOne = 'https://file-link.pinduoduo.com/sf_one';
public $sfOneTemplate = 'sf_one1688973997895.xml';
// 快递三联面单
public $sfThree = 'https://file-link.pinduoduo.com/sf_three';
public $sfThreeTemplate = 'sf_three1677381064344.xml';
// 标准模板
public $sfStd = 'https://file-link.pinduoduo.com/sf_std';
public $sfStdTemplate = 'sf_std1677380804913.xml';
public $templateUrl = 'https://template-content.jd.com/template-oss?tempCode=jdkd76x130';
// 时效类型
public $timedDeliveryCode;
@ -36,61 +28,35 @@ class WayBillService
// 已下单过的订单不再下单
$contents = [];
$this->adminUser = auth('api')->user();
$jingDongService = new JingDongService();
foreach ($this->orders as $shopId => $order) {
// 订单取消的情况暂不处理
$shopSend = $this->getShopSend($shopId);
foreach ($order as $item) {
[$sender, $orderInfo, $senderConfig] = $this->prepareRequest($item, $shopSend);
$waybill = $this->saveWayBill($item, $shopSend, $senderConfig);
if (empty($waybill->id)) {
$jingDongService = new JingDongService();
if (empty($waybill->waybill_code)) {
$resp = $jingDongService->createOrder($waybill);
if (isset($resp['pdd_waybill_get_response'])) {
$data = $resp['pdd_waybill_get_response']['modules'][0];
$printData = json_decode($data['print_data'], true);
$waybill->request_id = $resp['pdd_waybill_get_response']['request_id'];
$waybill->encryptedData = $printData['encryptedData'];
$waybill->signature = $printData['signature'];
$waybill->templateUrl = $printData['templateUrl'];
$waybill->ver = $printData['ver'];
$waybill->waybill_code = $data['waybill_code'];
if (isset($resp['waybillNo'])) {
$waybill->waybill_code = $resp['waybillNo'];
$waybill->save();
// 返回待打印内容
$contents[$waybill->id] = [
'addData' => [
'sender' => $sender,
],
'encryptedData' => $printData['encryptedData'],
'signature' => $printData['signature'],
'templateUrl' => env('APP_URL') . '/ktt/' . $this->sfOneTemplate, // 电子面单模板
'ver' => $printData['ver'],
'userid' => $waybill->user_id,
'items' => $item['items'],
'documentID' => $waybill->id,
'order_id' => $item['id'],
'participate_no' => $waybill->participate_no,
'note' => $waybill->note,
];
event(new CreateLogisticEvent($shopShip->shop_id, $waybill->order_sn, $data['waybill_code']));
//物流发货
event(new CreateLogisticEvent($order->shop_id, $waybill->order_sn, $waybill->waybill_code));
}
} else {
$contents[$waybill->id] = [
'addData' => [
'sender' => $sender,
],
'encryptedData' => $waybill->encryptedData,
'signature' => $waybill->signature,
'templateUrl' => env('APP_URL') . '/ktt/' . $this->sfOneTemplate, // 电子面单模板
'ver' => $waybill->ver,
'userid' => $waybill->user_id,
'items' => json_decode($waybill->items, true),
'documentID' => $waybill->id,
'order_id' => $item['id'],
'participate_no' => $waybill->participate_no,
'note' => $waybill->note,
];
}
//返回面单待打印数据
if (empty($waybill->encryptedData)) {
// 延时0.5秒 防止订单查询不到
usleep(0.5 * 1000000);
$resp = $jingDongService->pullData($waybill);
if (!empty($resp[0]['perPrintData'])) {
$waybill->templateUrl = $this->templateUrl;
$waybill->encryptedData = $resp[0]['perPrintData'];
$waybill->save();
}
}
$contents[$item['id']] = $waybill;
}
}
@ -129,7 +95,7 @@ class WayBillService
['order_sn' => $order['order_sn']]
);
$waybill->shop_id = $order->shop_id;
$waybill->shop_id = $order['shop_id'];
$waybill->object_id = $this->objectId;
$waybill->sender_country = $senderConfig['country'];
@ -138,7 +104,7 @@ class WayBillService
$waybill->sender_district = $senderConfig['district'];
$waybill->sender_detail = $senderConfig['detail'];
$waybill->sender_name = $senderConfig['name'];
$waybill->sender_mobile = $senderConfig['mobile'];
$waybill->sender_mobile = (int)$senderConfig['mobile'];
$waybill->recipient_province = $order['recipient_province'];
$waybill->recipient_city = $order['recipient_city'];
@ -154,7 +120,7 @@ class WayBillService
$waybill->participate_no = $order['participate_no'];
$waybill->note = $order['note'];
$waybill->items = json_encode($order['items'], 256);
$waybill->save();
return $waybill;
}
@ -231,7 +197,7 @@ class WayBillService
'name' => $order['recipient_name'],
'mobile' => $order['recipient_mobile'],
],
'template_url' => $this->sfOne,
'template_url' => $this->templateUrl,
];
return [$sender, $orderInfo, $senderConfig];
@ -251,6 +217,7 @@ class WayBillService
foreach ($orders as $order) {
$info = [
'id' => $order['id'],
'shop_id' => $order['shop_id'],
'order_sn' => $order['order_sn'],
'recipient_province' => $order['receiver_address_province'],
'recipient_city' => $order['receiver_address_city'],

View File

@ -31,7 +31,7 @@ class DefaultExecutor implements Executor
$uri = $this->baseUri . $request->getPath();
$httpRequest = new Request($request->getMethod(), $uri, $request->getHeaders(), $request->getBody());
$client = new Client();
$client = new Client(['verify' => false]);
try {
$httpResponse = $client->send($httpRequest, [
"query" => $request->getQueries(),