Merge branch 'develop'

# Conflicts:
#	app/common/service/StockOrdersService.php
This commit is contained in:
mzl
2025-11-04 15:43:42 +08:00
parent aabd0f4634
commit 6c59fdef10
5 changed files with 264 additions and 62 deletions
+196 -58
View File
@@ -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());
}
}
}