抓取车辆

This commit is contained in:
2026-03-11 15:01:54 +08:00
parent bfbed7fd2d
commit 260e76449b
5 changed files with 249 additions and 12 deletions
+122 -7
View File
@@ -464,11 +464,11 @@ class UsedCarDetailService
->where('used_car_id', $carId)
->where('status', 1)
->first();
if ($existing) {
$msg = '车辆已推送,跳过';
Log::info('WordPress推送:' . $msg, ['car_id' => $carId]);
return ['success' => true, 'message' => $msg];
}
// if ($existing) {
// $msg = '车辆已推送,跳过';
// Log::info('WordPress推送:' . $msg, ['car_id' => $carId]);
// return ['success' => true, 'message' => $msg];
// }
// 调用推送服务
$wpService = new \app\common\service\WordpressSiteService();
@@ -793,8 +793,9 @@ class UsedCarDetailService
{
$result = [];
// 从 car_info 直接获取的字段
$result['official_guide_price'] = $carInfo['official_price'] ?? '';
// 从 car_info 直接获取的字段,清理掉"万"等字符
$officialPrice = $carInfo['official_price'] ?? '';
$result['official_guide_price'] = $this->convertPriceToYuan($officialPrice);
// 1. 基本信息
$result['manufacturer'] = $this->getFieldValue($groups, 'sub_brand_name') ?? '';
@@ -900,4 +901,118 @@ class UsedCarDetailService
}
return '';
}
/**
* 抓取懂车帝二手车数据并保存
*
* @param string $url 懂车帝二手车链接
* @param int $userId 用户ID
* @return array 返回创建的车辆数据
* @throws \Exception
*/
public function crawlAndStore(string $url, int $userId): array
{
// 1. 抓取二手车基本信息
$usedCarCrawl = new \app\common\utils\crawl\service\UsedCarCrawlSingle();
$info = $usedCarCrawl->crawlCar($url);
// 2. 如果有 car_id,抓取详细参数
$params = [];
if (!empty($info['car_id'])) {
try {
$params = $this->crawlParams($info['car_id']);
} catch (\Throwable $e) {
Log::error('抓取车辆参数失败', [
'car_id' => $info['car_id'],
'error' => $e->getMessage(),
]);
// 参数抓取失败不影响主流程
}
}
// 3. 构建数据库记录数据
// 处理图片:如果是数组,转为逗号分隔字符串
$imagesValue = $info['image'] ?? '';
if (is_array($imagesValue)) {
$imagesValue = implode(',', array_filter($imagesValue));
}
$data = [
'code' => $this->generateCode(),
'user_id' => $userId,
'product_name' => $info['title'] ?? $info['car_name'] ?? '',
'price' => $this->convertPriceToYuan($info['price'] ?? 0),
'source_place' => $info['source_place'] ?? '',
'plate_time' => $info['plate_time'] ?? '',
'displacement' => $info['capacity'] ?? '',
'mileage' => $this->convertMileageToKm($info['usage_mileage'] ?? 0),
'body_color' => $info['body_color'] ?? '',
'condition_desc' => $info['condition_desc'] ?? '',
'source_url' => $url,
'crawl_title' => $info['title'] ?? '',
'crawl_brand' => $info['brand_name'] ?? '',
'crawl_brand_id' => $info['brand_id'] ?? 0,
'crawl_series' => $info['series_name'] ?? '',
'crawl_series_id' => $info['series_id'] ?? 0,
'crawl_year' => $info['year'] ?? '',
'crawl_msrp' => $info['new_price'] ?? '',
'crawl_car_id' => $info['car_id'] ?? 0,
'status' => 0, // 待审核
'images' => $imagesValue,
'power' => $info['horsepower'] ?? '',
];
// 4. 合并详细参数,过滤掉数组类型的值
$params = array_filter($params, function($value) {
return !is_array($value);
});
$data = array_merge($data, $params);
// 5. 创建记录
$car = UsedCarDetail::create($data);
return $car->toArray();
}
/**
* 生成车辆编码
*/
private function generateCode(): string
{
return 'UC' . date('YmdHis') . rand(1000, 9999);
}
/**
* 将价格(万)转换为元
*/
private function convertPriceToYuan($price): float
{
if (empty($price)) {
return 0;
}
// 如果是数字,直接乘以10000
if (is_numeric($price)) {
return floatval($price) * 10000;
}
// 如果是字符串,移除"万"字后乘以10000
$price = str_replace('万', '', $price);
return floatval($price) * 10000;
}
/**
* 将里程(万公里)转换为公里
*/
private function convertMileageToKm($mileage): float
{
if (empty($mileage)) {
return 0;
}
// 如果是数字,直接乘以10000
if (is_numeric($mileage)) {
return floatval($mileage) * 10000;
}
// 如果是字符串,移除"万公里"后乘以10000
$mileage = str_replace(['万公里', '万'], '', $mileage);
return floatval($mileage) * 10000;
}
}