更新若干功能
Some checks failed
Tests / PHP 8.2 (push) Has been cancelled
Tests / PHP 8.3 (push) Has been cancelled
Tests / PHP 8.4 (push) Has been cancelled

This commit is contained in:
2026-08-20 19:16:49 +08:00
parent 72bc6502eb
commit 4ddf5e3c5f
48 changed files with 1908 additions and 18 deletions

View File

@@ -91,6 +91,7 @@ class OrderCoreService
continue;
}
$quantity = max(1, (int) $item['quantity']);
StockService::getInstance()->assertAvailable((int) $item['price_sheet_id'], $quantity);
$unitPrice = (int) $item['unit_price'];
if ($unitPrice <= 0) {
$unitPrice = $price->resolveUnitPrice($sheet['routine'] ?? '', (string) $item['material_key'], $multiplier);
@@ -152,6 +153,9 @@ class OrderCoreService
}
unset($row);
OrderItemModel::insert($rows);
foreach ($rows as $row) {
StockService::getInstance()->deduct((int) $row['price_sheet_id'], (int) $row['quantity']);
}
ListModel::where('id', $list['id'])->update(['status' => 1, 'updated_at' => $now]);
});
@@ -270,6 +274,9 @@ class OrderCoreService
}
$this->applyPaid($orderId, (int) $payment['amount'], $now);
});
if ($status === OrderPaymentModel::STATUS_CONFIRMED) {
$this->notifySubscribe($orderId, 'pay');
}
return $this->detail($orderId);
}
@@ -308,11 +315,14 @@ class OrderCoreService
public function confirmWechatPay(string $outTradeNo, string $transactionId, int $amount): bool
{
$done = false;
DB::connection('business')->transaction(function () use ($outTradeNo, $transactionId, $amount, &$done) {
$fresh = false;
$orderId = 0;
DB::connection('business')->transaction(function () use ($outTradeNo, $transactionId, $amount, &$done, &$fresh, &$orderId) {
$payment = OrderPaymentModel::where('out_trade_no', $outTradeNo)->lockForUpdate()->first();
if (empty($payment)) {
return;
}
$orderId = (int) $payment['order_id'];
if ((int) $payment['status'] === OrderPaymentModel::STATUS_CONFIRMED) {
$done = true;
return;
@@ -325,9 +335,13 @@ class OrderCoreService
'audited_at' => $now,
'updated_at' => $now,
]);
$this->applyPaid((int) $payment['order_id'], $amount > 0 ? $amount : (int) $payment['amount'], $now);
$this->applyPaid($orderId, $amount > 0 ? $amount : (int) $payment['amount'], $now);
$done = true;
$fresh = true;
});
if ($fresh && $orderId > 0) {
$this->notifySubscribe($orderId, 'pay');
}
return $done;
}
@@ -367,6 +381,7 @@ class OrderCoreService
'updated_at' => $now,
]);
});
$this->notifySubscribe($orderId, 'ship');
return $this->detail($orderId);
}
@@ -405,4 +420,47 @@ class OrderCoreService
}
return $order->toArray();
}
/**
* 付款/发货后尽力发订阅消息,失败不影响主流程
*/
private function notifySubscribe(int $orderId, string $scene): void
{
if ($orderId <= 0) {
return;
}
try {
$order = OrderModel::where('id', $orderId)->first();
if (empty($order)) {
return;
}
$user = WxUserModel::where('id', (int) $order['user_id'])->first(['open_id']);
$openId = (string) ($user['open_id'] ?? '');
if ($openId === '') {
return;
}
$app = \App\Service\wx\WxAppService::getInstance()->current();
$tplKey = $scene === 'ship' ? 'subscribe_ship_tpl' : 'subscribe_pay_tpl';
$templateId = trim((string) ($app[$tplKey] ?? ''));
if ($templateId === '') {
return;
}
$price = PriceService::getInstance();
$page = 'pages/order/detail?id=' . $orderId;
$data = $scene === 'ship'
? [
'character_string1' => ['value' => (string) $order['order_no']],
'thing2' => ['value' => '订单已发货'],
'time3' => ['value' => date('Y-m-d H:i')],
]
: [
'character_string1' => ['value' => (string) $order['order_no']],
'amount2' => ['value' => $price->centsToYuan((int) $order['total_amount'])],
'thing3' => ['value' => '付款成功'],
];
\App\Service\wx\WxMiniService::getInstance()->sendSubscribe($openId, $templateId, $page, $data);
} catch (\Throwable) {
// 订阅消息是附加能力,模板字段对不上或用户未授权都不能挡住收款/发货
}
}
}