feat(haichao): 新增聚水潭ERP集成功能

- 新增聚水潭回调接口及事件处理机制
- 添加聚水潭商品、订单、售后同步任务
- 实现聚水潭订单推送及售后状态同步
- 新增手动同步聚水潭商品功能
- 在各路由组添加聚水潭中间件支持
This commit is contained in:
2026-04-01 11:55:18 +08:00
parent 359e9eef48
commit 7e422335e4
30 changed files with 2601 additions and 37 deletions

View File

@@ -0,0 +1,296 @@
<?php
namespace app\haichao\callback\event;
use app\Request;
use app\services\order\StoreOrderCartInfoServices;
use app\services\order\StoreOrderDeliveryServices;
use app\services\order\StoreOrderRefundServices;
use app\services\order\StoreOrderServices;
use think\exception\ValidateException;
use think\facade\Log;
class JstOrderPostEvent implements JstEvent
{
public function event(Request $request)
{
$payload = [];
$content = (string)$request->getContent();
if ($content !== '') {
$decoded = json_decode($content, true);
if (is_array($decoded)) {
$payload = $decoded;
} else {
$output = [];
parse_str($content, $output);
if (is_array($output)) {
$payload = $output;
}
}
}
if (!$payload) {
$payload = $request->param();
}
$biz = [];
if (isset($payload['biz']) && $payload['biz'] !== '') {
if (is_array($payload['biz'])) {
$biz = $payload['biz'];
} else {
$bizDecoded = json_decode((string)$payload['biz'], true);
if (is_array($bizDecoded)) {
$biz = $bizDecoded;
}
}
}
$oId = $request->getMore([['o_id', false]])['o_id'] ?: (string)($payload['o_id'] ?? $biz['o_id'] ?? ($payload['oId'] ?? $biz['oId'] ?? ''));
$logisticsCompany = $request->getMore([['logistics_company', false]])['logistics_company'] ?: (string)($payload['logistics_company'] ?? $biz['logistics_company'] ?? ($payload['logisticsCompany'] ?? $biz['logisticsCompany'] ?? ''));
$lId = $request->getMore([['l_id', false]])['l_id'] ?: (string)($payload['l_id'] ?? $biz['l_id'] ?? ($payload['lId'] ?? $biz['lId'] ?? ''));
$lcId = $request->getMore([['lc_id', false]])['lc_id'] ?: (string)($payload['lc_id'] ?? $biz['lc_id'] ?? ($payload['lcId'] ?? $biz['lcId'] ?? ''));
$soId = $request->getMore([['so_id', false]])['so_id'] ?: (string)($payload['so_id'] ?? $biz['so_id'] ?? ($payload['soId'] ?? $biz['soId'] ?? ''));
$items = $request->getMore([['items', false]])['items'] ?: ($payload['items'] ?? $biz['items'] ?? ($payload['item_list'] ?? $biz['item_list'] ?? []));
if (!is_array($items)) {
$items = [];
}
$skus = [];
if (is_array($items)) {
foreach ($items as $it) {
$skus[] = ['sku' => $it['sku_id'] ?? '', 'qty' => (int)($it['qty'] ?? 0)];
}
}
Log::info('jst.cb.orderPost.start ' . json_encode([
'so_id' => $soId,
'o_id' => $oId,
'logistics_company' => $logisticsCompany,
'l_id' => $lId,
'lc_id' => $lcId,
'item_count' => is_array($items) ? count($items) : 0,
'sku_list' => $skus,
], JSON_UNESCAPED_UNICODE));
/** @var StoreOrderServices $storeOrderServices */
$storeOrderServices = app()->make(StoreOrderServices::class);
/** @var StoreOrderDeliveryServices $orderDeliverServices */
$orderDeliverServices = app()->make(StoreOrderDeliveryServices::class);
/** @var StoreOrderRefundServices $storeOrderRefundServices */
$storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
/** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
$storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
if ($soId === '') {
Log::info('jst.cb.orderPost.skip ' . json_encode(['reason' => 'missing so_id'], JSON_UNESCAPED_UNICODE));
return 'missing so_id';
}
$orderInfo = $storeOrderServices->getOne(['order_id' => $soId]);
if (!$orderInfo) throw new ValidateException('order not found ' . $soId);
$oid = $orderInfo['id'];
$splitOrderList = $storeOrderServices->getSplitOrderList(['pid' => $oid]);
$nol = [];
foreach ($splitOrderList as $splitOrder) {
if ($splitOrder['status'] === 0) $nol[] = $splitOrder;
}
$splitOrderList = $nol;
Log::info('jst.cb.orderPost.split.count ' . json_encode(['so_id' => $soId, 'count' => count($splitOrderList)], JSON_UNESCAPED_UNICODE));
$cardIds = $orderInfo['cart_id'];
$num = 0;
foreach ($cardIds as $cardId) {
$cardInfo = $storeOrderCartInfoServices->getOne(['cart_id' => $cardId])->toArray();
$num += $cardInfo['cart_num'];
}
$num2 = 0;
foreach ($items as $item) {
$num2 += $item['qty'];
}
if ($num !== $num2) {
Log::warning('jst.cb.orderPost.qty_mismatch ' . json_encode(['so_id' => $soId, 'order_cart_num' => $num, 'ship_qty_sum' => $num2], JSON_UNESCAPED_UNICODE));
}
$existSo = false;
if ($splitOrderList) {
foreach ($splitOrderList as $splitOrder) {
if ($splitOrder['order_id'] == $soId) {
$existSo = true;
}
}
} else {
$existSo = true;
}
if (!$existSo) {
Log::warning('jst.cb.orderPost.no_exist_so ' . json_encode(['so_id' => $soId], JSON_UNESCAPED_UNICODE));
foreach ($splitOrderList as $item) {
foreach ($item['cart_id'] as $cartId) {
$cartInfo = $item['_info'][$cartId]['cart_info'];
$productInfo = $cartInfo['productInfo'];
$cartfa = [];
foreach ($items as $sentProduct) {
if ($productInfo['attrInfo']['suk'] == $sentProduct['sku_id'] ||
$productInfo['attrInfo']['code'] == $sentProduct['sku_id'] ||
($productInfo['attrInfo']['unique'] . 'ok') == $sentProduct['sku_id']) {
$cartfa[] = [
'cart_id' => $cartId,
'cart_num' => $sentProduct['qty'],
];
}
}
try {
$deliverArgs = [
'type' => 1,
'delivery_name' => $logisticsCompany,
'delivery_id' => $lId,
'delivery_code' => $lcId,
'express_record_type' => "1",
'express_temp_id' => md5(time()),
'cart_ids' => $cartfa
];
$orderDeliverServices->splitDelivery($item['id'], $deliverArgs, 0, true);
Log::info('jst.cb.orderPost.success ' . json_encode(['so_id' => $soId, 'oid' => (int)$item['id'], 'mode' => 'split'], JSON_UNESCAPED_UNICODE));
return 'ok';
} catch (\Throwable $exception) {
Log::error('jst.cb.orderPost.split.error ' . json_encode(['oid' => $item['id'], 'msg' => $exception->getMessage()], JSON_UNESCAPED_UNICODE));
throw new ValidateException('split delivery error ' . $exception->getMessage());
}
}
}
Log::warning('jst.cb.orderPost.no_match.split ' . json_encode(['so_id' => $soId], JSON_UNESCAPED_UNICODE));
return 'no match';
}
try {
if (count($splitOrderList) > 1 || count($orderInfo['cart_id']) > 1) {
if (count($splitOrderList) > 0) {
foreach ($splitOrderList as $item) {
foreach ($item['cart_id'] as $cartId) {
$cartInfo = $item['_info'][$cartId]['cart_info'];
$productInfo = $cartInfo['productInfo'];
foreach ($items as $postItem) {
$postItemSku = $postItem['sku_id'];
if ($productInfo['attrInfo']['suk'] == $postItemSku || $productInfo['attrInfo']['code'] == $postItemSku) {
try {
$deliverArgs = [
'type' => 1,
'delivery_name' => $logisticsCompany,
'delivery_id' => $lId,
'delivery_code' => $lcId,
'express_record_type' => "1",
'express_temp_id' => md5(time()),
'cart_ids' => [
[
'cart_id' => $cartId,
'cart_num' => $cartInfo['cart_num'],
]
]
];
$orderDeliverServices->splitDelivery($oid, $deliverArgs, 0, true);
Log::info('jst.cb.orderPost.success ' . json_encode(['so_id' => $soId, 'oid' => (int)$oid, 'mode' => 'split.pid', 'cart_id' => (int)$cartId], JSON_UNESCAPED_UNICODE));
return 'ok';
} catch (\Throwable $exception) {
Log::error('jst.cb.orderPost.split.pid.error ' . json_encode(['pid' => $oid, 'cart_id' => $cartId, 'msg' => $exception->getMessage()], JSON_UNESCAPED_UNICODE));
throw new ValidateException('split delivery error ' . $exception->getMessage());
}
}
}
}
}
Log::warning('jst.cb.orderPost.no_match.pid ' . json_encode(['so_id' => $soId, 'pid' => (int)$oid], JSON_UNESCAPED_UNICODE));
return 'no match';
}
if (count($orderInfo['cart_id']) > 1) {
$cartfa = [];
foreach ($items as $sentProduct) {
foreach ($orderInfo['cart_id'] as $cartId) {
$cartInfo = $storeOrderCartInfoServices->getOne(['cart_id' => $cartId]);
$productInfo = $cartInfo['cart_info']['productInfo'] ?? [];
if (!$productInfo) continue;
if ($productInfo['attrInfo']['suk'] == $sentProduct['sku_id'] ||
$productInfo['attrInfo']['code'] == $sentProduct['sku_id'] ||
($productInfo['attrInfo']['unique'] . 'ok') == $sentProduct['sku_id']) {
$cartfa[] = [
'cart_id' => $cartId,
'cart_num' => $cartInfo['cart_num'],
];
}
}
}
Log::info('jst.cb.orderPost.multi.mapping ' . json_encode(['pid' => $oid, 'match' => $cartfa], JSON_UNESCAPED_UNICODE));
try {
$deliverArgs = [
'type' => 1,
'delivery_id' => $lId,
'delivery_code' => $lcId,
'express_record_type' => "1",
'express_temp_id' => md5(time()),
'cart_ids' => $cartfa
];
$orderDeliverServices->splitDelivery($oid, $deliverArgs, 0, true);
Log::info('jst.cb.orderPost.success ' . json_encode(['so_id' => $soId, 'oid' => (int)$oid, 'mode' => 'multi'], JSON_UNESCAPED_UNICODE));
return 'ok';
} catch (\Throwable $exception) {
Log::error('jst.cb.orderPost.multi.error ' . json_encode(['pid' => $oid, 'msg' => $exception->getMessage()], JSON_UNESCAPED_UNICODE));
throw new ValidateException('split delivery error ' . $exception->getMessage());
}
}
} else {
if ($num == $num2) {
$deliverArgs = [
'type' => 1,
'delivery_name' => $logisticsCompany,
'delivery_id' => $lId,
'delivery_code' => $lcId,
'express_record_type' => "1",
'express_temp_id' => md5(time()),
];
$orderDeliverServices->delivery($oid, $deliverArgs);
Log::info('jst.cb.orderPost.success ' . json_encode(['so_id' => $soId, 'oid' => (int)$oid, 'mode' => 'delivery'], JSON_UNESCAPED_UNICODE));
return 'ok';
}
}
} catch (\Throwable $e) {
if ($e->getMessage() != '订单已发货请勿重复操作!') {
Log::error('jst.cb.orderPost.error ' . json_encode(['oid' => $oid, 'msg' => $e->getMessage()], JSON_UNESCAPED_UNICODE));
throw new ValidateException($e->getMessage());
}
Log::info('jst.cb.orderPost.repeat ' . json_encode(['so_id' => $soId, 'oid' => (int)$oid], JSON_UNESCAPED_UNICODE));
return 'ok';
}
$cardIds = $orderInfo['cart_id'];
if (count($cardIds) >= 1) {
$cartfa = [];
foreach ($cardIds as $cartId) {
$cardInfo = $storeOrderCartInfoServices->getOne(['cart_id' => $cartId])->toArray();
$productInfo = $cardInfo['cart_info']['productInfo'] ?? [];
foreach ($items as $sentProduct) {
if ($productInfo['attrInfo']['suk'] == $sentProduct['sku_id'] ||
$productInfo['attrInfo']['code'] == $sentProduct['sku_id'] ||
($productInfo['attrInfo']['unique'] . 'ok') == $sentProduct['sku_id']) {
$cartfa[] = [
'cart_id' => $cartId,
'cart_num' => $sentProduct['qty'],
];
}
}
}
try {
$deliverArgs = [
'type' => 1,
'delivery_name' => $logisticsCompany,
'delivery_id' => $lId,
'delivery_code' => $lcId,
'express_record_type' => "1",
'express_temp_id' => md5(time()),
'cart_ids' => $cartfa
];
$orderDeliverServices->splitDelivery($oid, $deliverArgs);
Log::info('jst.cb.orderPost.success ' . json_encode(['so_id' => $soId, 'oid' => (int)$oid, 'mode' => 'cartfa'], JSON_UNESCAPED_UNICODE));
return 'ok';
} catch (\Throwable $exception) {
Log::error('jst.cb.orderPost.cartfa.error ' . json_encode(['pid' => $oid, 'msg' => $exception->getMessage()], JSON_UNESCAPED_UNICODE));
throw new ValidateException('split delivery error ' . $exception->getMessage());
}
}
Log::warning('jst.cb.orderPost.unknown ' . json_encode(['so_id' => $soId], JSON_UNESCAPED_UNICODE));
throw new ValidateException('unknown delivery ' . $soId);
}
}