This commit is contained in:
f4ilji
2024-11-24 16:18:37 +05:00
parent 77cc7b2c76
commit 37a06cd898
2 changed files with 64 additions and 43 deletions
+2
View File
@@ -29,6 +29,7 @@ use DateTime;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use Inertia\Inertia; use Inertia\Inertia;
@@ -36,6 +37,7 @@ class MainController extends Controller
{ {
public function index() public function index()
{ {
$admissionCampaign = $this->getAdmissionCampaign(); $admissionCampaign = $this->getAdmissionCampaign();
$educations = $this->getEducationsData(); $educations = $this->getEducationsData();
$sliders = $this->getActiveSliders(); $sliders = $this->getActiveSliders();
+62 -43
View File
@@ -31,58 +31,77 @@ class ImportApiDataPost implements ShouldQueue
public function handle() public function handle()
{ {
try { try {
// Получаем первую страницу данных
$response = Http::get('https://crawdad-fresh-bream.ngrok-free.app/api/posts')->object(); $response = Http::get('https://crawdad-fresh-bream.ngrok-free.app/api/posts')->object();
$last_page = $response->last_page;
Log::info('Last page: ' . $last_page);
foreach ($response as $item) { // Проходим по всем страницам
try { for ($page = 1; $page <= $last_page; $page++) {
$postResponse = Http::get('https://crawdad-fresh-bream.ngrok-free.app/api/posts/' . $item->ID)->object(); $response = Http::get("https://crawdad-fresh-bream.ngrok-free.app/api/posts?page=$page")->object();
$results = $response->data;
// Проверка на наличие данных // Обрабатываем каждую запись
if (!isset($postResponse->post)) { foreach ($results as $post) {
Log::error('Post data not found for ID: ' . $item->ID); Log::info('Processing post ID: ' . $post->ID);
continue;
}
$postData = $postResponse->post; try {
$images = $postResponse->images ?? []; // Обработка отсутствия изображений // Получаем массив изображений
$images = $post->images ?? []; // Обработка отсутствия изображений
$imagePaths = [];
// Обработка изображения // Проверяем, есть ли изображения
$imagePaths = []; if (empty($images)) {
foreach ($images as $image) { Log::warning('No images found for post ID: ' . $post->ID);
$imagePaths[] = 'upload/' . $image->SUBDIR . '/' . $image->FILE_NAME; } else {
} // Проходимся по массиву изображений
foreach ($images as $image) {
// Проверяем наличие необходимых свойств
if (isset($image->SUBDIR) && isset($image->FILE_NAME)) {
$imagePaths[] = 'upload/' . $image->SUBDIR . '/' . $image->FILE_NAME;
} else {
Log::warning('Image data is incomplete for post ID: ' . $post->ID);
}
}
}
$content = strip_tags($postData->DETAIL_TEXT, '<a>'); // Логируем пути к изображениям для отладки
$contentData = [ Log::info('Image paths for post ID ' . $post->ID . ': ' . json_encode($imagePaths));
[
'type' => 'paragraph', // Обработка контента поста
'data' => [ $content = strip_tags($post->DETAIL_TEXT, '<a>');
'content' => $content, $contentData = [
[
'type' => 'paragraph',
'data' => [
'content' => $content,
],
], ],
], ];
];
$slug = $this->generateSlug($postData->NAME); // Генерация уникального slug
$slug = $this->generateSlug($post->NAME);
// Создание нового поста // Создание нового поста
Post::create([ Post::create([
'id' => $postData->ID, 'id' => $post->ID,
'title' => $postData->NAME, 'title' => $post->NAME,
'slug' => $slug, 'slug' => $slug,
'preview_text' => strip_tags($postData->PREVIEW_TEXT), 'preview_text' => strip_tags($post->PREVIEW_TEXT),
'authors' => array('Без автора'), 'authors' => ['Без автора'],
'content' => $contentData, // Преобразуем в JSON 'content' => $contentData, // Преобразуем в JSON
'status' => 'published', 'status' => 'published',
'images' => $imagePaths, 'images' => $imagePaths, // Сохраняем пути к изображениям
'search_data' => $content, // Преобразуем в JSON 'search_data' => $content, // Преобразуем в JSON
'reading_time' => 2, 'reading_time' => 2,
'user_id' => 1, 'user_id' => 1,
'publish_at' => $postData->DATE_CREATE, 'publish_at' => $post->DATE_CREATE,
'created_at' => $postData->DATE_CREATE, 'created_at' => $post->DATE_CREATE,
'updated_at' => $postData->DATE_CREATE, 'updated_at' => $post->DATE_CREATE,
]); ]);
} catch (\Exception $e) { } catch (\Exception $e) {
Log::error('Error importing post ID: ' . $item->ID . ' - ' . $e->getMessage()); Log::error('Error importing post ID: ' . $post->ID . ' - ' . $e->getMessage());
}
} }
} }
} catch (\Exception $e) { } catch (\Exception $e) {