推送队列

This commit is contained in:
2026-03-11 16:01:18 +08:00
parent 261fa05517
commit ff3201375d
13 changed files with 188 additions and 887 deletions
+32 -56
View File
@@ -424,70 +424,33 @@ class UsedCarDetailService
// 更新车辆状态
$result = UsedCarDetail::where('id', $carId)->update(['status' => $auditStatus]);
// 审核通过后自动推送到WordPress
$pushResult = ['success' => true, 'message' => ''];
// 审核通过后,将推送任务发送到队列
$pushQueued = false;
if ($auditStatus == 1) {
$pushResult = $this->pushToWordpressAfterAudit($carId);
// 推送失败,将车辆状态修改为3
if (!$pushResult['success']) {
UsedCarDetail::where('id', $carId)->update(['status' => 3]);
try {
// 发送到 Redis 队列异步处理
\Webman\RedisQueue\Redis::send('wordpress-push', [
'car_id' => $carId,
]);
$pushQueued = true;
Log::info('WordPress推送任务已加入队列', ['car_id' => $carId]);
} catch (\Throwable $e) {
Log::error('WordPress推送任务加入队列失败', [
'car_id' => $carId,
'error' => $e->getMessage(),
]);
$pushQueued = false;
}
}
// 返回审核结果和推送结果
// 返回审核结果
return [
'audit_success' => true,
'push_success' => $pushResult['success'],
'push_message' => $pushResult['message'],
'message' => $pushResult['success'] ? '审核通过并推送成功' : '审核通过,但推送失败:' . $pushResult['message'],
'push_queued' => $pushQueued,
'message' => $pushQueued ? '审核通过,WordPress推送任务已加入队列' : '审核通过',
];
}
/**
* 审核通过后推送车辆到WordPress
* @return array ['success' => bool, 'message' => string]
*/
private function pushToWordpressAfterAudit(int $carId): array
{
try {
// 获取启用的WordPress站点
$site = \app\common\model\WordpressSite::where('status', 1)->first();
if (!$site) {
$msg = '没有启用的WordPress站点';
Log::warning('WordPress推送失败:' . $msg, ['car_id' => $carId]);
return ['success' => false, 'message' => $msg];
}
// 检查是否已推送过
$existing = \app\common\model\WordpressUsedCar::where('wp_site_id', $site->id)
->where('used_car_id', $carId)
->where('status', 1)
->first();
// if ($existing) {
// $msg = '车辆已推送,跳过';
// Log::info('WordPress推送:' . $msg, ['car_id' => $carId]);
// return ['success' => true, 'message' => $msg];
// }
// 调用推送服务
$wpService = new \app\common\service\WordpressSiteService();
$wpService->createProductForNewSite($site, $carId);
Log::info('WordPress推送成功', ['car_id' => $carId, 'site_id' => $site->id]);
return ['success' => true, 'message' => ''];
} catch (\Throwable $e) {
$msg = $e->getMessage();
Log::error('WordPress推送失败', [
'car_id' => $carId,
'error' => $msg,
'trace' => $e->getTraceAsString(),
]);
return ['success' => false, 'message' => $msg];
}
}
/**
* 获取审核记录列表
*/
@@ -957,7 +920,7 @@ class UsedCarDetailService
'crawl_year' => $info['year'] ?? '',
'crawl_msrp' => $info['new_price'] ?? '',
'crawl_car_id' => $info['car_id'] ?? 0,
'status' => 0, // 待审核
'status' => 3, // 待推送状态
'images' => $imagesValue,
'power' => $info['horsepower'] ?? '',
];
@@ -971,6 +934,19 @@ class UsedCarDetailService
// 5. 创建记录
$car = UsedCarDetail::create($data);
// 6. 将推送任务发送到队列
try {
\Webman\RedisQueue\Redis::send('wordpress-push', [
'car_id' => $car->id,
]);
Log::info('WordPress推送任务已加入队列', ['car_id' => $car->id]);
} catch (\Throwable $e) {
Log::error('WordPress推送任务加入队列失败', [
'car_id' => $car->id,
'error' => $e->getMessage(),
]);
}
return $car->toArray();
}