1
This commit is contained in:
@@ -525,42 +525,77 @@ class WordpressSiteService
|
||||
if(empty($brand_name)){
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 获取API凭证,支持模型对象和数组
|
||||
$apiUser = is_object($site) ? $site->api_user : ($site['api_user'] ?? null);
|
||||
$apiSecret = is_object($site) ? $site->api_secret : ($site['api_secret'] ?? null);
|
||||
|
||||
if (empty($apiUser) || empty($apiSecret)) {
|
||||
Log::error('WordPress站点API凭证为空', [
|
||||
'site_type' => is_object($site) ? get_class($site) : 'array',
|
||||
]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
$client = new Client([
|
||||
'base_uri' => $domain . '/wp-json/wp/v2/',
|
||||
RequestOptions::AUTH => [$site['api_user'], $site['api_secret']],
|
||||
// 'auth' => ['your_username', 'your_password'], // 或者使用应用密码
|
||||
RequestOptions::AUTH => [$apiUser, $apiSecret],
|
||||
RequestOptions::VERIFY => false, // 禁用 SSL 验证
|
||||
]);
|
||||
|
||||
// 生成slug
|
||||
$slug = $this->generate_slug($brand_name);
|
||||
|
||||
// 查询品牌是否已存在
|
||||
$response = $client->get('pa_brand', [
|
||||
'query' => [
|
||||
'slug' => $slug // 用品牌名称生成slug查询
|
||||
]
|
||||
]);
|
||||
try {
|
||||
// 查询品牌是否已存在
|
||||
$response = $client->get('pa_brand', [
|
||||
'query' => [
|
||||
'slug' => $slug
|
||||
]
|
||||
]);
|
||||
|
||||
$brands = json_decode($response->getBody(), true);
|
||||
$brands = json_decode($response->getBody()->getContents(), true);
|
||||
|
||||
// 如果品牌存在,返回ID
|
||||
if (!empty($brands)) {
|
||||
return $brands[0]['id']; // 返回第一个匹配的品牌ID
|
||||
// 如果品牌存在,返回ID
|
||||
if (!empty($brands) && isset($brands[0]['id'])) {
|
||||
return $brands[0]['id'];
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error('查询品牌失败', [
|
||||
'brand_name' => $brand_name,
|
||||
'slug' => $slug,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
|
||||
// 品牌不存在,先创建
|
||||
$response = $client->post('pa_brand', [
|
||||
'json' => [
|
||||
'name' => $brand_name,
|
||||
'slug' => $slug
|
||||
]
|
||||
]);
|
||||
try {
|
||||
$response = $client->post('pa_brand', [
|
||||
'json' => [
|
||||
'name' => $brand_name,
|
||||
'slug' => $slug
|
||||
]
|
||||
]);
|
||||
|
||||
$brand = json_decode($response->getBody(), true);
|
||||
$brand = json_decode($response->getBody(), true);
|
||||
|
||||
// 返回创建的品牌ID
|
||||
return $brand['id'];
|
||||
if (empty($brand) || !isset($brand['id'])) {
|
||||
Log::error('创建品牌失败,响应无效', [
|
||||
'brand_name' => $brand_name,
|
||||
'response' => $response->getBody()->getContents(),
|
||||
]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 返回创建的品牌ID
|
||||
return $brand['id'];
|
||||
} catch (\Exception $e) {
|
||||
Log::error('创建品牌失败', [
|
||||
'brand_name' => $brand_name,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
function generate_slug($brand_name) {
|
||||
@@ -769,7 +804,16 @@ class WordpressSiteService
|
||||
public function uploadImageToWp(string $domain,$api_user, $api_secret, string $imageUrl = "fdesk_test/usedcar_crawl/20251125/ff847a6a2b3114b4c2c230455b74cd2e.jpg"): array
|
||||
{
|
||||
$process = 'image/resize,w_800/quality,Q_50/format,webp';
|
||||
$processedUrl = (new OssService)->generatePrivateUrlResize($imageUrl,3600,$process);
|
||||
|
||||
try {
|
||||
$processedUrl = (new OssService)->generatePrivateUrlResize($imageUrl,3600,$process);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('生成OSS URL失败', [
|
||||
'image_url' => $imageUrl,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
throw new \Exception('生成图片URL失败: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
$client = new Client();
|
||||
|
||||
@@ -797,11 +841,26 @@ class WordpressSiteService
|
||||
|
||||
try {
|
||||
// 1. 下载图片到本地临时文件
|
||||
$imageContent = file_get_contents($processedUrl);
|
||||
$imageContent = @file_get_contents($processedUrl);
|
||||
if ($imageContent === false) {
|
||||
Log::error('从OSS下载图片失败', [
|
||||
'original_url' => $imageUrl,
|
||||
'processed_url' => $processedUrl,
|
||||
]);
|
||||
throw new \Exception('图片下载失败:' . $processedUrl);
|
||||
}
|
||||
|
||||
// 检查下载的内容大小
|
||||
if (strlen($imageContent) < 100) {
|
||||
Log::error('下载的图片内容过小', [
|
||||
'original_url' => $imageUrl,
|
||||
'processed_url' => $processedUrl,
|
||||
'size' => strlen($imageContent),
|
||||
'content' => $imageContent,
|
||||
]);
|
||||
throw new \Exception('下载的图片内容无效');
|
||||
}
|
||||
|
||||
// 写入临时文件
|
||||
$bytesWritten = file_put_contents($tempFilePath, $imageContent);
|
||||
if ($bytesWritten === false) {
|
||||
@@ -831,15 +890,36 @@ class WordpressSiteService
|
||||
}
|
||||
|
||||
$responseBody = $response->getBody()->getContents();
|
||||
$statusCode = $response->getStatusCode();
|
||||
|
||||
// 检查HTTP状态码
|
||||
if ($statusCode !== 201 && $statusCode !== 200) {
|
||||
Log::error('WordPress图片上传失败,状态码异常', [
|
||||
'image_url' => $imageUrl,
|
||||
'status_code' => $statusCode,
|
||||
'response' => substr($responseBody, 0, 1000),
|
||||
]);
|
||||
throw new \Exception("WordPress返回状态码: {$statusCode}");
|
||||
}
|
||||
|
||||
$result = json_decode($responseBody, true);
|
||||
|
||||
if (empty($result) || !is_array($result)) {
|
||||
Log::error('WordPress图片上传响应解析失败', [
|
||||
'image_url' => $imageUrl,
|
||||
'response' => substr($responseBody, 0, 500),
|
||||
'status_code' => $response->getStatusCode(),
|
||||
'response' => substr($responseBody, 0, 1000),
|
||||
'status_code' => $statusCode,
|
||||
'json_error' => json_last_error_msg(),
|
||||
]);
|
||||
throw new \Exception('WordPress响应解析失败');
|
||||
throw new \Exception('WordPress响应解析失败: ' . json_last_error_msg());
|
||||
}
|
||||
|
||||
if (!isset($result['id'])) {
|
||||
Log::error('WordPress图片上传响应缺少ID', [
|
||||
'image_url' => $imageUrl,
|
||||
'response' => $responseBody,
|
||||
]);
|
||||
throw new \Exception('WordPress响应缺少媒体ID');
|
||||
}
|
||||
|
||||
return $result;
|
||||
@@ -1192,6 +1272,15 @@ class WordpressSiteService
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否有成功上传的图片
|
||||
if (empty($uploadedImages)) {
|
||||
Log::warning('没有成功上传任何图片', [
|
||||
'car_id' => $carId,
|
||||
'original_images_count' => count($images),
|
||||
]);
|
||||
// 继续创建产品,但没有图片
|
||||
}
|
||||
|
||||
// 获取FAQ数据
|
||||
$faqs = \app\common\model\UsedCarFaq::where('car_id', $carId)
|
||||
->where('status', 1)
|
||||
|
||||
Reference in New Issue
Block a user