车辆编号

This commit is contained in:
2026-01-14 10:45:16 +08:00
parent 006e6bfcc1
commit fc8c1e21ec
2 changed files with 103 additions and 33 deletions
+1 -1
View File
@@ -40,7 +40,7 @@ class UsedCar extends Model
/** /**
* 允许自动填充模型字段 * 允许自动填充模型字段
*/ */
protected $fillable = ['id', 'user_id', 'product_name', 'vin', 'price', 'source_place', 'plate_place', 'plate_time', 'displacement','mileage','power', 'body_color', 'images', 'condition_desc', 'attachment', 'crawl_title', 'crawl_brand', 'crawl_series', 'crawl_year', 'crawl_msrp', 'crawl_car_id', 'status', 'sort', 'created_at', 'updated_at']; protected $fillable = ['id','code', 'user_id', 'product_name', 'vin', 'price', 'source_place', 'plate_place', 'plate_time', 'displacement','mileage','power', 'body_color', 'images', 'condition_desc', 'attachment', 'crawl_title', 'crawl_brand', 'crawl_series', 'crawl_year', 'crawl_msrp', 'crawl_car_id', 'status', 'sort', 'created_at', 'updated_at'];
/** /**
* 不允许自动填充模型字段 * 不允许自动填充模型字段
+102 -32
View File
@@ -9,6 +9,7 @@ use GuzzleHttp\Client;
use support\exception\BusinessException; use support\exception\BusinessException;
use support\Log; use support\Log;
use support\Request; use support\Request;
use app\common\model\User;
class UsedCarService class UsedCarService
{ {
@@ -85,22 +86,91 @@ class UsedCarService
public function beforeServiceStore($data, Request $request): array public function beforeServiceStore($data, Request $request): array
{ {
if(empty($data['product_name'])){ if (empty($data['product_name'])) {
throw new BusinessException('产品名称不能为空'); throw new BusinessException('产品名称不能为空');
} }
if(empty($data['images'])){ if (empty($data['images'])) {
throw new BusinessException('产品图片不能为空'); throw new BusinessException('产品图片不能为空');
} }
if(empty($data['price'])){ if (empty($data['price'])) {
$data['price'] = 0; $data['price'] = 0;
} }
if(!empty($data['attachment'])){ if (!empty($data['attachment'])) {
$data['attachment'] = json_encode($data['attachment']); $data['attachment'] = json_encode($data['attachment']);
} }
$data['user_id'] = $request->user['id']; $data['user_id'] = $request->user['id'];
$data['code'] = $this->getcode($request->user['id']);
return $data; return $data;
} }
public function getcode($userId){
$todayStart = strtotime('today');
$count = UsedCar::where('user_id',$userId)->where('created_at','>=',$todayStart)->count();
$countCode = str_pad($count, 3, '0', STR_PAD_LEFT);
$userCode = str_pad($userId, 3, '0', STR_PAD_LEFT);
$user = User::where('id',$userId)->first();
$nameInitials = $this->getNameInitials($user['name']);
return $nameInitials . $userCode . date('Ymd') . $countCode;
}
/**
* 获取中文名拼音首字母
*/
function getNameInitials(string $name): string
{
$initials = '';
$len = mb_strlen($name);
for ($i = 0; $i < $len; $i++) {
$char = mb_substr($name, $i, 1);
$initials .= $this->getFirstChar($char);
}
return strtoupper($initials);
}
function getFirstChar(string $char): string
{
$s = iconv('UTF-8', 'GB2312//IGNORE', $char);
$asc = ord($s[0]);
if ($asc >= 65 && $asc <= 90) return $char;
$code = ord($s[0]) * 256 + ord($s[1]) - 65536;
$map = [
[-20319, -20284, 'A'],
[-20283, -19776, 'B'],
[-19775, -19219, 'C'],
[-19218, -18711, 'D'],
[-18710, -18527, 'E'],
[-18526, -18240, 'F'],
[-18239, -17923, 'G'],
[-17922, -17418, 'H'],
[-17417, -16475, 'J'],
[-16474, -16213, 'K'],
[-16212, -15641, 'L'],
[-15640, -15166, 'M'],
[-15165, -14923, 'N'],
[-14922, -14915, 'O'],
[-14914, -14631, 'P'],
[-14630, -14150, 'Q'],
[-14149, -14091, 'R'],
[-14090, -13319, 'S'],
[-13318, -12839, 'T'],
[-12838, -12557, 'W'],
[-12556, -11848, 'X'],
[-11847, -11056, 'Y'],
[-11055, -10247, 'Z']
];
foreach ($map as [$start, $end, $letter]) {
if ($code >= $start && $code <= $end) {
return $letter;
}
}
return '';
}
/** /**
@@ -120,30 +190,30 @@ class UsedCarService
$href = $crawlService->modelSearch($keyword); $href = $crawlService->modelSearch($keyword);
$seriesId = $this->extractSeriesId($href); //懂车帝车系id $seriesId = $this->extractSeriesId($href); //懂车帝车系id
// if (empty($seriesId)) { //没有抓取到懂车帝系列id时,按关键词为品牌名抓取所有的系列名 // if (empty($seriesId)) { //没有抓取到懂车帝系列id时,按关键词为品牌名抓取所有的系列名
// $crawlSeries = $crawlService->brandSeries($keyword); // $crawlSeries = $crawlService->brandSeries($keyword);
// } // }
//按关键词为品牌名抓取所有的系列名 //按关键词为品牌名抓取所有的系列名
// $crawlSeries = $crawlService->brandSeries($keyword); // $crawlSeries = $crawlService->brandSeries($keyword);
if (empty($seriesId)) { if (empty($seriesId)) {
throw new \Exception('抓取失败,请使用更精确的关键词'); throw new \Exception('抓取失败,请使用更精确的关键词');
} }
// $brandSerieses = []; // $brandSerieses = [];
// if(empty($seriesId) && !empty($crawlSeries) || count($crawlSeries)>1){ //关键词为品牌名时抓取所有车系的懂车帝id // if(empty($seriesId) && !empty($crawlSeries) || count($crawlSeries)>1){ //关键词为品牌名时抓取所有车系的懂车帝id
// $href = $crawlService->modelSearch($crawlSeries[0]); // $href = $crawlService->modelSearch($crawlSeries[0]);
// $seriesId = $this->extractSeriesId($href); //懂车帝车系id // $seriesId = $this->extractSeriesId($href); //懂车帝车系id
// //抓取品牌与系列 // //抓取品牌与系列
// $brandSeries = $crawlService->crawlBrandSeries($seriesId); // $brandSeries = $crawlService->crawlBrandSeries($seriesId);
// foreach ($crawlSeries as $item) { // foreach ($crawlSeries as $item) {
//// $brandSerieses[] = $crawlService->brandSeries($keyword); //// $brandSerieses[] = $crawlService->brandSeries($keyword);
// $brandSerieses[] = ['brand' => $brandSeries['brand'], 'series' => $item]; // $brandSerieses[] = ['brand' => $brandSeries['brand'], 'series' => $item];
// } // }
// }elseif (!empty($seriesId) && empty($crawlSeries)){ // }elseif (!empty($seriesId) && empty($crawlSeries)){
// //抓取品牌与系列 // //抓取品牌与系列
// $brandSerieses[] = $crawlService->crawlBrandSeries($seriesId); // $brandSerieses[] = $crawlService->crawlBrandSeries($seriesId);
// } // }
//抓取品牌与系列 //抓取品牌与系列
$brandSerieses[] = $crawlService->crawlBrandSeries($seriesId); $brandSerieses[] = $crawlService->crawlBrandSeries($seriesId);
@@ -156,7 +226,7 @@ class UsedCarService
//获取车型数据 //获取车型数据
$item['model'] = []; $item['model'] = [];
if (count($brandSerieses)<2) { if (count($brandSerieses) < 2) {
$item['model'] = $this->crawlSeriesModel($seriesId); $item['model'] = $this->crawlSeriesModel($seriesId);
} }
$result[] = $item; $result[] = $item;
@@ -187,30 +257,30 @@ class UsedCarService
] ]
]); ]);
$data = json_decode($response->getBody()->getContents(), true); $data = json_decode($response->getBody()->getContents(), true);
if($data['status'] != 0 || (!isset($data['data']) && !isset($data['data']['tab_list']))){ if ($data['status'] != 0 || (!isset($data['data']) && !isset($data['data']['tab_list']))) {
Log::error('抓取车型数据失败' . $data['message'], $data); Log::error('抓取车型数据失败' . $data['message'], $data);
throw new \Exception('抓取车型数据失败' . $data['message']); throw new \Exception('抓取车型数据失败' . $data['message']);
} }
$models = []; $models = [];
foreach ($data['data']['tab_list'] as $item) { foreach ($data['data']['tab_list'] as $item) {
if($item['tab_text'] == '在售' || $item['tab_text'] == '停售'){ if ($item['tab_text'] == '在售' || $item['tab_text'] == '停售') {
unset($item['data'][0]); unset($item['data'][0]);
$models = array_merge($models, $item['data']); $models = array_merge($models, $item['data']);
} }
} }
foreach ($models as $key => $model) { foreach ($models as $key => $model) {
if(!isset($model["info"]['car_name']) || $model['type'] != "1115"){ if (!isset($model["info"]['car_name']) || $model['type'] != "1115") {
unset($models[$key]); unset($models[$key]);
continue; continue;
} }
$official_price = 0; $official_price = 0;
if(!isset($model["info"]['official_price'])){ if (!isset($model["info"]['official_price'])) {
$official_price = (int)(str_replace("万", "", $model["info"]['official_price_str']) * 10000) ?? 0; $official_price = (int)(str_replace("万", "", $model["info"]['official_price_str']) * 10000) ?? 0;
}else{ } else {
$official_price = $model["info"]['official_price'] * 10000; $official_price = $model["info"]['official_price'] * 10000;
} }
$models[$key] = [ $models[$key] = [
'title' => $model["info"]['year'] . '款 ' .$model["info"]['car_name'], 'title' => $model["info"]['year'] . '款 ' . $model["info"]['car_name'],
'brand_id' => $model["info"]['brand_id'], 'brand_id' => $model["info"]['brand_id'],
'brand_name' => $model["info"]['brand_name'], 'brand_name' => $model["info"]['brand_name'],
'series_id' => $model["info"]['series_id'], 'series_id' => $model["info"]['series_id'],
@@ -222,7 +292,6 @@ class UsedCarService
} }
return array_values($models); return array_values($models);
} catch (\Exception $e) { } catch (\Exception $e) {
throw new \Exception($e->getMessage()); throw new \Exception($e->getMessage());
} }
@@ -233,7 +302,8 @@ class UsedCarService
* @param string $url 完整的URL * @param string $url 完整的URL
* @return string|null series ID或null * @return string|null series ID或null
*/ */
function extractSeriesId($url) { function extractSeriesId($url)
{
// 解析URL获取路径部分 // 解析URL获取路径部分
$path = parse_url($url, PHP_URL_PATH); $path = parse_url($url, PHP_URL_PATH);
@@ -289,4 +359,4 @@ class UsedCarService
return $path; return $path;
} }
} }