From 6c59fdef106c5dfaccefc67485d8318693a2fbca Mon Sep 17 00:00:00 2001 From: mzl Date: Tue, 4 Nov 2025 15:43:42 +0800 Subject: [PATCH] Merge branch 'develop' # Conflicts: # app/common/service/StockOrdersService.php --- app/admin/service/BrandSeriesService.php | 254 ++++++++++++++---- .../controller/BrandSeriesController.php | 42 ++- app/common/service/CrawlService.php | 14 + app/common/service/StockOrdersService.php | 14 +- config/route.php | 2 + 5 files changed, 264 insertions(+), 62 deletions(-) diff --git a/app/admin/service/BrandSeriesService.php b/app/admin/service/BrandSeriesService.php index f1526d3..594605b 100644 --- a/app/admin/service/BrandSeriesService.php +++ b/app/admin/service/BrandSeriesService.php @@ -168,50 +168,8 @@ class BrandSeriesService throw new \Exception('抓取失败,未找到该系列车型'); } - $client = new Client([ - 'verify' => false, // 禁用 SSL 验证 - 'timeout' => 10 - ]); try { - $response = $client->get('https://www.dongchedi.com/motor/pc/car/series/car_list', [ - 'query' => ['series_id' => $seriesId], - 'headers' => [ - 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' - ] - ]); - $data = json_decode($response->getBody()->getContents(), true); - if($data['status'] != 0 || (!isset($data['data']) && !isset($data['data']['tab_list']))){ - Log::error('抓取车型数据失败' . $data['message'], $data); - throw new \Exception('抓取车型数据失败' . $data['message']); - } - $models = []; - foreach ($data['data']['tab_list'] as $item) { - if($item['tab_text'] == '在售' || $item['tab_text'] == '停售'){ - unset($item['data'][0]); - $models = array_merge($models, $item['data']); - } - } - foreach ($models as $key => $model) { - if(!isset($model["info"]['car_name']) || $model['type'] != "1115"){ - unset($models[$key]); - continue; - } - $official_price = 0; - if(!isset($model["info"]['official_price'])){ - $official_price = (int)(str_replace("万", "", $model["info"]['official_price_str']) * 10000) ?? 0; - }else{ - $official_price = $model["info"]['official_price'] * 10000; - } - $models[$key] = [ - 'title' => $model["info"]['year'] . '款 ' .$model["info"]['car_name'], - 'brand_name' => $model["info"]['brand_name'], - 'series_name' => $model["info"]['series_name'], - 'year' => $model["info"]['year'], - 'msrp' => $official_price, - 'car_id' => $model["info"]['id'], - ]; - } - return array_values($models); + return $this->crawlSeriesModel($seriesId); } catch (\Exception $e) { throw new \Exception($e->getMessage()); } @@ -296,7 +254,150 @@ class BrandSeriesService // 验证系列是否存在 $series = BrandSeries::where('id', $series_id)->first(); if (!$series) { - throw new \Exception("系列ID {$series_id} 不存在"); + throw new \Exception("车辆系列未收录,联系管理员处理"); + } + // 获取品牌信息 + $brand = Brand::where('id', $series->brand_id)->first(); + if (!$brand) { + throw new \Exception("车辆品牌未收录,联系管理员处理"); + } + + // 保存车型数据 + $createdCount = 0; + $repeat = 0; + $fail = 0; + $msg = ''; + + foreach ($models as $item) { + $modelData = [ + 'brand_code' => $brand->brand_code, + 'brand_name' => $brand->brand_name, + 'series_code' => $series->series_code, + 'series_name' => $series->series_name, + 'model_name' => $item['title'], + 'model_year' => $item['year'], + 'msrp' => $item['msrp'], + ]; + //指纹数组 + $fingerprintArr = [ + 'brand_name' => $modelData['brand_name'], + 'series_name' => $modelData['series_name'], + 'model_name' => $modelData['model_name'], + 'model_year' => $modelData['model_year'], + 'msrp' => $modelData['msrp'], + ]; + // 生成指纹 + $modelData['fingerprint'] = Fingerprint::makeFromArray($fingerprintArr); + + $modelData['model_code'] = ModelNaming::buildModelCode($modelData); + $modelData['sku_code'] = SkuCodeBuilder::build($modelData); + + try { + $model = VehVehicleSku::where('brand_name', $brand->brand_name)->where('series_name', $series->series_name)->where('model_name', $item['title'])->first(); + if (!empty($model)) { + $repeat++; + continue; + } + CrudUtil::create(VehVehicleSku::class, $modelData); + $createdCount++; + } catch (\Exception $e) { + // 记录错误但继续处理其他车型 + $msg .= "创建车型失败: " . $e->getMessage() . "\n"; + Log::error("创建车型失败:" . $msg, ['series_id' => $series_id, 'model' => $item]); + $fail++; + } + } + + Log::info('series-crawl-' . $series_id . ' ' . $msg); + $msg = '创建车型成功' . $createdCount . '条'; + $msg .= (!empty($repeat)) ? ',重复' . $repeat . '条' : ''; + $msg .= (!empty($fail)) ? ',失败' . $fail . '条' : ''; + return $msg; + } + + /** + * 抓取车型数据(通过接口) + * @param Request $request + * @return array + * @throws \Exception + */ + public function crawlKeyword(Request $request): array + { + $keyword = $request->get('keyword'); + + // 执行抓取 + $crawlService = new CrawlService(); + + //懂车帝模糊搜索返回车系地址 + $href = $crawlService->modelSearch($keyword); + $seriesId = $this->extractSeriesId($href); + + if (empty($seriesId)) { + throw new \Exception('抓取失败,未找到该系列车型'); + } + + //抓取品牌与系列 + $brandSeries = $crawlService->crawlBrandSeries($seriesId); + + + // 验证品牌是否存在 + $brand = Brand::where('brand_name', $brandSeries['brand'])->first(); + // 获取品牌信息 + $series = BrandSeries::where('series_name', $brandSeries['series'])->first(); + $result['brand'] = ['brand_name' => $brandSeries['brand'], 'exists' => $brand?1:0]; + if($brand){ + $result['brand']['id'] = $brand['id']; + } + $result['series'] = ['series_name' => $brandSeries['series'], 'exists' => $series?1:0]; + if($series){ + $result['series']['id'] = $series['id']; + if(empty($result['brand']['id'])){ + $result['brand']['id'] = $series['brand_id']; + } + } + + try { + //获取车型数据 + $result['model'] = $this->crawlSeriesModel($seriesId); + return $result; + } catch (\Exception $e) { + throw new \Exception($e->getMessage()); + } + } + + /** + * @param Request $request + * @return array|string + * @throws \Exception + */ + public function crawlKeywordCreate(Request $request) + { + $series_id = $request->input('series_id'); + $brand_id = $request->input('brand_id'); + $series_name = $request->input('series_name'); + $models = $request->input('models'); + + if(empty($brand_id)){ + throw new \Exception("请选择品牌"); + } + if(empty($series_id) && empty($series_name)){ + throw new \Exception("请选择车系"); + } + + if (empty($series_id)) { + $series = BrandSeries::where('brand_id', $brand_id)->where('series_name', $series_name)->first(); + if (empty($brandseries)) { //如果没有该车系则创建 + $seriesService = new \app\admin\service\BrandSeriesService(); + $seriesData = [ + 'brand_id' => $brand_id, + 'series_code' => $seriesService->generateUniqueModificationCode($brand_id), + 'series_name' => $series_name, + ]; + $series = CrudUtil::create(BrandSeries::class, $seriesData); + } + $series_id = $series->id; + }else{ + $series = BrandSeries::where('id', $series_id)->first(); } // 获取品牌信息 $brand = Brand::where('id', $series->brand_id)->first(); @@ -354,24 +455,61 @@ class BrandSeriesService return $msg; } - /** - * 从车型名中提取年份 - * @param string $carModel 车型名称 - * @return string|null 提取到的年份或null + * 获取车系下车型数据 + * @return array + * @throws \GuzzleHttp\Exception\GuzzleException */ - function extractYearFromCarModel($carModel) + public function crawlSeriesModel($seriesId) { - // 匹配4位数字 + "款" 的模式 - if (preg_match('/(\d{4})款/', $carModel, $matches)) { - return $matches[1]; - } + $client = new Client([ + 'verify' => false, // 禁用 SSL 验证 + 'timeout' => 10 + ]); + try { + $response = $client->get('https://www.dongchedi.com/motor/pc/car/series/car_list', [ + 'query' => ['series_id' => $seriesId], + 'headers' => [ + 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' + ] + ]); + $data = json_decode($response->getBody()->getContents(), true); + if($data['status'] != 0 || (!isset($data['data']) && !isset($data['data']['tab_list']))){ + Log::error('抓取车型数据失败' . $data['message'], $data); + throw new \Exception('抓取车型数据失败' . $data['message']); + } + $models = []; + foreach ($data['data']['tab_list'] as $item) { + if($item['tab_text'] == '在售' || $item['tab_text'] == '停售'){ + unset($item['data'][0]); + $models = array_merge($models, $item['data']); + } + } + foreach ($models as $key => $model) { + if(!isset($model["info"]['car_name']) || $model['type'] != "1115"){ + unset($models[$key]); + continue; + } + $official_price = 0; + if(!isset($model["info"]['official_price'])){ + $official_price = (int)(str_replace("万", "", $model["info"]['official_price_str']) * 10000) ?? 0; + }else{ + $official_price = $model["info"]['official_price'] * 10000; + } + $models[$key] = [ + 'title' => $model["info"]['year'] . '款 ' .$model["info"]['car_name'], + 'brand_name' => $model["info"]['brand_name'], + 'series_name' => $model["info"]['series_name'], + 'year' => $model["info"]['year'], + 'msrp' => $official_price, + 'car_id' => $model["info"]['id'], + ]; + } - // 匹配4位数字 + 空格或结尾的模式 - if (preg_match('/^(\d{4})(?=\s|$)/', $carModel, $matches)) { - return $matches[1]; - } + return array_values($models); - return null; + } catch (\Exception $e) { + throw new \Exception($e->getMessage()); + } } } \ No newline at end of file diff --git a/app/common/controller/BrandSeriesController.php b/app/common/controller/BrandSeriesController.php index 00faa00..3cd32d3 100644 --- a/app/common/controller/BrandSeriesController.php +++ b/app/common/controller/BrandSeriesController.php @@ -48,7 +48,7 @@ class BrandSeriesController extends BaseController } /** - * 异步抓取系列车型 + * 抓取系列车型 * @param Request $request * @return \support\Response */ @@ -58,7 +58,7 @@ class BrandSeriesController extends BaseController if($result){ return $this->success($result,'抓取成功'); }else{ - return $this->error('抓取任务提交失败'); + return $this->error('抓取任务失败'); } } catch (\Exception $e) { return $this->error($e->getMessage()); @@ -66,7 +66,7 @@ class BrandSeriesController extends BaseController } /** - * 异步抓取系列车型 + * 保存抓取的系列车型 * @param Request $request * @return \support\Response */ @@ -82,4 +82,40 @@ class BrandSeriesController extends BaseController return $this->error($e->getMessage()); } } + + /** + * 关键词抓取品牌、车系、车型 + * @param Request $request + * @return \support\Response + */ + public function crawlKeyword(Request $request){ + try { + $result = $this->service->crawlKeyword($request); + if($result){ + return $this->success($result,'抓取成功'); + }else{ + return $this->error('抓取任务失败'); + } + } catch (\Exception $e) { + return $this->error($e->getMessage()); + } + } + + /** + * 保存抓取的系列车型 + * @param Request $request + * @return \support\Response + */ + public function crawlKeywordCreate(Request $request){ + try { + $result = $this->service->crawlKeywordCreate($request); + if($result){ + return $this->success($result,'成功'); + }else{ + return $this->error('创建失败'); + } + } catch (\Exception $e) { + return $this->error($e->getMessage()); + } + } } \ No newline at end of file diff --git a/app/common/service/CrawlService.php b/app/common/service/CrawlService.php index a2c69d4..53e7026 100644 --- a/app/common/service/CrawlService.php +++ b/app/common/service/CrawlService.php @@ -73,4 +73,18 @@ class CrawlService }); return $href; } + + /** + * 关键词搜索的更多页面,通过面包屑抓取品牌与系列 + * @param $series + * @return mixed|string + */ + public function crawlBrandSeries($series) + { + $url = "https://www.dongchedi.com/auto/series/" . $series . "?anchorId=carModels"; + $ql = QueryList::get($url); + $result = $ql->find('div.breadcrumb_wrap__2GLeU span')->texts()->all(); + + return ['brand' => $result[2], 'series' => $result[3]]; + } } \ No newline at end of file diff --git a/app/common/service/StockOrdersService.php b/app/common/service/StockOrdersService.php index 9d5e4df..6da7a24 100644 --- a/app/common/service/StockOrdersService.php +++ b/app/common/service/StockOrdersService.php @@ -1114,5 +1114,17 @@ class StockOrdersService } - + public function serviceDestroy(Request $request, string $scene = 'destroy') + { + $config = $this->buildQueryConfig($request, $scene); + $item = $this->logicShow($config); + if (!$item) { + throw new BusinessException('数据不存在'); + } + if ($item->approval_status == 1 || $item->approval_status == 2) { + throw new BusinessException('审核中或者已审核的订单不能删除'); + } + $deleted = $this->logicDeleteBy($config); + return $this->postProcess(ucfirst($scene), $deleted); + } } \ No newline at end of file diff --git a/config/route.php b/config/route.php index c31e972..36492d9 100644 --- a/config/route.php +++ b/config/route.php @@ -109,6 +109,8 @@ Route::group('/common', function () { Route::any('/destroy', [app\common\controller\BrandSeriesController::class, 'destroy']); Route::any('/crawlModels', [app\common\controller\BrandSeriesController::class, 'crawlModels']); Route::any('/crawlModelCreate', [app\common\controller\BrandSeriesController::class, 'crawlModelCreate']); + Route::any('/crawlKeyword', [app\common\controller\BrandSeriesController::class, 'crawlKeyword']); + Route::any('/crawlKeywordCreate', [app\common\controller\BrandSeriesController::class, 'crawlKeywordCreate']); }); });