From 7c7e66ae9a2f81e95f84218947990479d34c541e Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Thu, 12 Mar 2026 14:21:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=93=E5=8F=96=E7=A1=AE=E8=AE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UsedCarDetailController.php | 38 +++++++++++++++++-- app/common/service/UsedCarDetailService.php | 33 +++++++++++++++- config/route.php | 3 +- 3 files changed, 68 insertions(+), 6 deletions(-) diff --git a/app/common/controller/UsedCarDetailController.php b/app/common/controller/UsedCarDetailController.php index c7ddea3..cbf83e8 100644 --- a/app/common/controller/UsedCarDetailController.php +++ b/app/common/controller/UsedCarDetailController.php @@ -326,12 +326,31 @@ class UsedCarDetailController extends BaseController } /** - * 抓取懂车帝二手车数据并保存 + * 保存懂车帝二手车数据 * * @param Request $request * @return \support\Response */ - public function crawlAndStore(Request $request) + public function crawlStore(Request $request) + { + try { + $params = $request->post(); + $userId = $request->user['id'] ?? 0; + $result = $this->service->crawlStore($params, $userId); + return $this->success($result, '保存成功,WordPress推送任务已加入队列,将在后台处理'); + } catch (\Throwable $e) { + return $this->error($e->getMessage()); + } + + } + + /** + * 抓取懂车帝二手车数据 + * + * @param Request $request + * @return \support\Response + */ + public function crawl(Request $request) { try { $url = $request->post('url'); @@ -350,9 +369,20 @@ class UsedCarDetailController extends BaseController throw new BusinessException('用户未登录'); } - $result = $this->service->crawlAndStore($url, $userId); + $result = $this->service->crawl($url, $userId); + if(!empty($result['images'])){ + $images = explode(',',$result['images']); + $ossService = new OssService(); + $images_pull = []; + foreach ($images as $image){ + $images_pull[] = $ossService->generatePrivateUrl($image); + } + $result['images_pull'] = $images_pull; + }else{ + $result['images_pull'] = []; + } - return $this->success($result, '抓取成功,WordPress推送任务已加入队列,将在后台处理'); + return $this->success($result, '抓取成功'); } catch (\Throwable $e) { Log::error('抓取懂车帝二手车失败', [ 'url' => $request->post('url'), diff --git a/app/common/service/UsedCarDetailService.php b/app/common/service/UsedCarDetailService.php index 0d43e55..242c853 100644 --- a/app/common/service/UsedCarDetailService.php +++ b/app/common/service/UsedCarDetailService.php @@ -880,7 +880,7 @@ class UsedCarDetailService * @return array 返回创建的车辆数据 * @throws \Exception */ - public function crawlAndStore(string $url, int $userId): array + public function crawl(string $url, int $userId): array { // 检查该用户是否已抓取过此URL的车辆 $existingCar = UsedCarDetail::where('user_id', $userId) @@ -946,7 +946,38 @@ class UsedCarDetailService return !is_array($value); }); $data = array_merge($data, $params); + return $data; + // 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();*/ + } + + /** + * 保存懂车帝二手车数据 + * + * @param string $url 懂车帝二手车链接 + * @param int $userId 用户ID + * @return array 返回创建的车辆数据 + * @throws \Exception + */ + public function crawlStore(array $data, int $userId): array + { + $data['user_id'] = $userId; // 5. 创建记录 $car = UsedCarDetail::create($data); diff --git a/config/route.php b/config/route.php index a07aebe..6a2862a 100644 --- a/config/route.php +++ b/config/route.php @@ -92,7 +92,8 @@ Route::group('/common', function () { Route::any('/audit/stats', [app\common\controller\UsedCarDetailController::class, 'auditStats']); // 抓取相关 - Route::any('/crawl/store', [app\common\controller\UsedCarDetailController::class, 'crawlAndStore']); + Route::any('/crawl', [app\common\controller\UsedCarDetailController::class, 'crawl']); + Route::any('/crawl/store', [app\common\controller\UsedCarDetailController::class, 'crawlStore']); // 重新推送 Route::any('/retry/push', [app\common\controller\UsedCarDetailController::class, 'retryPush']);