diff --git a/app/common/controller/UsedCarDetailController.php b/app/common/controller/UsedCarDetailController.php index f5fbb40..6251110 100644 --- a/app/common/controller/UsedCarDetailController.php +++ b/app/common/controller/UsedCarDetailController.php @@ -360,4 +360,35 @@ class UsedCarDetailController extends BaseController return $this->error($e->getMessage()); } } + + /** + * 重新推送失败的车辆到WordPress + * + * @param Request $request + * @return \support\Response + */ + public function retryPush(Request $request) + { + try { + $carId = $request->post('id'); + + if (empty($carId)) { + throw new BusinessException('车辆ID不能为空'); + } + + $count = $this->service->retryPush($carId); + + if ($count === 0) { + return $this->error('车辆不存在或状态不是推送失败'); + } + + return $this->success(['count' => $count], 'WordPress推送任务已重新加入队列'); + } catch (\Throwable $e) { + Log::error('重新推送失败', [ + 'car_id' => $request->post('id'), + 'error' => $e->getMessage(), + ]); + return $this->error($e->getMessage()); + } + } } \ No newline at end of file diff --git a/app/common/service/UsedCarDetailService.php b/app/common/service/UsedCarDetailService.php index 83e0e9e..9558777 100644 --- a/app/common/service/UsedCarDetailService.php +++ b/app/common/service/UsedCarDetailService.php @@ -875,6 +875,15 @@ class UsedCarDetailService */ public function crawlAndStore(string $url, int $userId): array { + // 检查该用户是否已抓取过此URL的车辆 + $existingCar = UsedCarDetail::where('user_id', $userId) + ->where('source_url', $url) + ->first(); + + if ($existingCar) { + throw new \Exception('该车辆信息已存在,无需重复抓取'); + } + // 1. 抓取二手车基本信息 $usedCarCrawl = new \app\common\utils\crawl\service\UsedCarCrawlSingle(); $info = $usedCarCrawl->crawlCar($url); @@ -991,4 +1000,31 @@ class UsedCarDetailService $mileage = str_replace(['万公里', '万'], '', $mileage); return floatval($mileage) * 10000; } + + /** + * 重新推送单辆失败的车到WordPress + * + * @param int $carId 车辆ID + * @return int 成功加入队列返回1,否则返回0 + */ + public function retryPush(int $carId): int + { + $car = UsedCarDetail::where('id', $carId) + ->where('status', 5) // 只处理推送失败的 + ->first(); + + if (!$car) { + return 0; + } + + // 更新状态为待推送 + $car->update(['status' => 3]); + + // 加入队列 + \Webman\RedisQueue\Redis::send('wordpress-push', ['car_id' => $carId]); + + Log::info('车辆重新加入推送队列', ['car_id' => $carId]); + + return 1; + } } \ No newline at end of file diff --git a/config/route.php b/config/route.php index 660a756..a07aebe 100644 --- a/config/route.php +++ b/config/route.php @@ -93,6 +93,9 @@ Route::group('/common', function () { // 抓取相关 Route::any('/crawl/store', [app\common\controller\UsedCarDetailController::class, 'crawlAndStore']); + + // 重新推送 + Route::any('/retry/push', [app\common\controller\UsedCarDetailController::class, 'retryPush']); }); Route::group('/user', function () {