fixing bugs in seo generate, fix bug set publish time in post, changes in import posts command, changes in pagination

This commit is contained in:
F4ilji
2025-04-19 15:07:29 +05:00
parent 1399e2b8de
commit 03aca1725b
10 changed files with 86 additions and 57 deletions
+1 -3
View File
@@ -113,14 +113,12 @@ class PostForm
->helperText('Активируйте для публикации в указанное время'), ->helperText('Активируйте для публикации в указанное время'),
DateTimePicker::make('publish_setting.publish_at') DateTimePicker::make('publish_setting.publish_at')
->label('Дата и время публикации') ->label('Дата и время публикации')
->native(false)
->displayFormat('d/m/Y H:i') ->displayFormat('d/m/Y H:i')
->seconds(false) ->seconds(false)
->minutesStep(15)
->helperText('Выберите дату и время публикации') ->helperText('Выберите дату и время публикации')
->required(fn (Forms\Get $get) => $get('publish_setting.publish_after')) ->required(fn (Forms\Get $get) => $get('publish_setting.publish_after'))
->disabled(fn (Forms\Get $get) => !$get('publish_setting.publish_after')) ->disabled(fn (Forms\Get $get) => !$get('publish_setting.publish_after'))
->minDate(now()) ->native(true)
->maxDate(now()->addMonth()), ->maxDate(now()->addMonth()),
]), ]),
]), ]),
@@ -41,7 +41,7 @@ class CreatePost extends CreateRecord
protected function processPostData(array $data): array protected function processPostData(array $data): array
{ {
return (new PostDataProcessor())->process($data); return (new PostDataProcessor())->process($data, 'create');
} }
protected function afterCreate(): void protected function afterCreate(): void
@@ -49,7 +49,7 @@ class EditPost extends EditRecord
protected function processPostData(array $data): array protected function processPostData(array $data): array
{ {
return (new PostDataProcessor())->process($data); return (new PostDataProcessor())->process($data, 'edit');
} }
protected function afterSave(): void protected function afterSave(): void
@@ -68,10 +68,12 @@ class ClientPostController extends Controller
return $query->withAnyTags($slugsArray); return $query->withAnyTags($slugsArray);
}) })
->orderBy('publish_at', $request->input('sort', 'desc')) ->orderBy('publish_at', $request->input('sort', 'desc'))
->paginate(6) ->paginate(9)
->withQueryString()); ->withQueryString()
);
}); });
$categories = Cache::remember('categories', now()->addHours(48), function () { $categories = Cache::remember('categories', now()->addHours(48), function () {
return CategoryResource::collection(Category::has('posts')->get()); return CategoryResource::collection(Category::has('posts')->get());
}); });
+31 -19
View File
@@ -32,13 +32,13 @@ class ImportApiDataPost implements ShouldQueue
{ {
try { try {
// Получаем первую страницу данных // Получаем первую страницу данных
$response = Http::get('https://crawdad-fresh-bream.ngrok-free.app/api/posts')->object(); $response = Http::get(env('TRANSFER_PROXY_URL') . '/api/posts')->object();
$last_page = $response->last_page; $last_page = $response->last_page;
Log::info('Last page: ' . $last_page); Log::info('Last page: ' . $last_page);
// Проходим по всем страницам // Проходим по всем страницам
for ($page = 1; $page <= $last_page; $page++) { for ($page = 1; $page <= $last_page; $page++) {
$response = Http::get("https://crawdad-fresh-bream.ngrok-free.app/api/posts?page=$page")->object(); $response = Http::get(env('TRANSFER_PROXY_URL') . "/api/posts?page=$page")->object();
$results = $response->data; $results = $response->data;
// Обрабатываем каждую запись // Обрабатываем каждую запись
@@ -70,6 +70,8 @@ class ImportApiDataPost implements ShouldQueue
// Обработка контента поста // Обработка контента поста
$content = strip_tags($post->DETAIL_TEXT, '<a>'); $content = strip_tags($post->DETAIL_TEXT, '<a>');
$readingTime = $this->calculateReadingTime($content);
$contentData = [ $contentData = [
[ [
'type' => 'paragraph', 'type' => 'paragraph',
@@ -83,22 +85,25 @@ class ImportApiDataPost implements ShouldQueue
$slug = $this->generateSlug($post->NAME); $slug = $this->generateSlug($post->NAME);
// Создание нового поста // Создание нового поста
Post::create([ Post::firstOrCreate(
'id' => $post->ID, ['id' => $post->ID], // Условие поиска по ID
'title' => $post->NAME, [
'slug' => $slug, 'title' => $post->NAME,
'preview_text' => strip_tags($post->PREVIEW_TEXT), 'slug' => $slug,
'authors' => ['Без автора'], 'preview_text' => strip_tags($post->PREVIEW_TEXT),
'content' => $contentData, // Преобразуем в JSON 'authors' => ['Без автора'],
'status' => 'published', 'content' => $contentData, // Преобразуем в JSON
'images' => $imagePaths, // Сохраняем пути к изображениям 'status' => 'published',
'search_data' => $content, // Преобразуем в JSON 'images' => $imagePaths, // Сохраняем пути к изображениям
'reading_time' => 2, 'preview' => $imagePaths[0],
'user_id' => 1, 'search_data' => strip_tags($content), // Преобразуем в JSON
'publish_at' => $post->DATE_CREATE, 'reading_time' => $readingTime,
'created_at' => $post->DATE_CREATE, 'user_id' => 1,
'updated_at' => $post->DATE_CREATE, 'publish_at' => $post->DATE_CREATE,
]); 'created_at' => $post->DATE_CREATE,
'updated_at' => $post->DATE_CREATE,
]
);
} catch (\Exception $e) { } catch (\Exception $e) {
Log::error('Error importing post ID: ' . $post->ID . ' - ' . $e->getMessage()); Log::error('Error importing post ID: ' . $post->ID . ' - ' . $e->getMessage());
} }
@@ -109,7 +114,7 @@ class ImportApiDataPost implements ShouldQueue
} }
} }
private function generateSlug($name) private function generateSlug($name): string
{ {
$slug = Str::slug($name); $slug = Str::slug($name);
$originalSlug = $slug; $originalSlug = $slug;
@@ -123,4 +128,11 @@ class ImportApiDataPost implements ShouldQueue
return $slug; return $slug;
} }
private function calculateReadingTime(string $text): int
{
$wordCount = str_word_count($text, 0, "АаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя");
$wordsPerMinute = 120; // Средняя скорость чтения
return max(1, round($wordCount / $wordsPerMinute));
}
} }
+5 -2
View File
@@ -14,7 +14,7 @@ class SeoPageProvider
{ {
return $model->seo?->toArray(); return $model->seo?->toArray();
} }
public function getSeoForCurrentPage(): ?array public function getSeoForCurrentPage(): array|null
{ {
$path = $this->getCurrentPath(); $path = $this->getCurrentPath();
@@ -23,8 +23,11 @@ class SeoPageProvider
now()->addHours(1), now()->addHours(1),
fn() => Page::where('path', $path)->first() fn() => Page::where('path', $path)->first()
); );
if ($page) {
return $page->seo->toArray();
}
return $page->seo?->toArray(); return null;
} }
private function getCurrentPath(): string private function getCurrentPath(): string
@@ -14,7 +14,7 @@ class PostDataProcessor
* @param array $data * @param array $data
* @return array * @return array
*/ */
public function process(array $data): array public function process(array $data, $operation): array
{ {
// Удаляем ненужные данные // Удаляем ненужные данные
unset($data['publication']); unset($data['publication']);
@@ -23,7 +23,14 @@ class PostDataProcessor
$data['preview_text'] = $this->setPreviewText($data); $data['preview_text'] = $this->setPreviewText($data);
// Устанавливаем время публикации // Устанавливаем время публикации
$data['publish_at'] = $this->setPublishDateTime($data['publish_setting'], $data['status']); if ($data['publish_setting']['publish_after'] === true) {
$data['publish_at'] = $this->setPublishDateTimeInFuture($data['publish_setting']);
}
if (($data['status'] === PostStatus::PUBLISHED->value || PostStatus::PUBLISHED) && $operation === 'create') {
$data['publish_at'] = $this->setPublishDateTime();
}
unset($data['publish_setting']); unset($data['publish_setting']);
// Генерируем данные для поиска // Генерируем данные для поиска
@@ -63,21 +70,17 @@ class PostDataProcessor
* @param string $status * @param string $status
* @return Carbon|null * @return Carbon|null
*/ */
private function setPublishDateTime(array $publishSetting, $status): ?Carbon private function setPublishDateTime(): ?Carbon
{
return Carbon::now();
}
private function setPublishDateTimeInFuture(array $publishSetting): ?Carbon
{ {
if ($publishSetting['publish_after'] === true) { if ($publishSetting['publish_after'] === true) {
return Carbon::parse($publishSetting['publish_at']); return Carbon::parse($publishSetting['publish_at']);
} }
if ($status === PostStatus::PUBLISHED->value) {
return Carbon::now();
}
if ($status === PostStatus::PUBLISHED) {
return Carbon::now();
}
return null; return null;
} }
+4 -3
View File
@@ -9,20 +9,21 @@ trait SeoGenerate
{ {
public function createSeo($record): void public function createSeo($record): void
{ {
$record->seo()->create($this->generateSeo($record)); $seo = $this->generateSeoData($record);
$record->seo()->create($seo);
} }
public function updateSeo($record): void public function updateSeo($record): void
{ {
if ($record->seo()->exists()) { if ($record->seo()->exists()) {
$record->seo()->update($this->generateSeo($record)); $record->seo()->update($this->generateSeoData($record));
} else { } else {
$this->createSeo($record); $this->createSeo($record);
} }
} }
private function generateSeo($record) { private function generateSeoData($record) {
return app(SeoGeneratorService::class)->generate([ return app(SeoGeneratorService::class)->generate([
'title' => $record->title, 'title' => $record->title,
'content' => $record instanceof SeoDescriptionInterface 'content' => $record instanceof SeoDescriptionInterface
+1 -1
View File
@@ -115,7 +115,7 @@ export default {
<PostListItem :post="post" /> <PostListItem :post="post" />
</template> </template>
</div> </div>
<BasicPagination :links="posts.links" /> <BasicPagination :links="posts.meta" />
</div> </div>
</div> </div>
</div> </div>
@@ -1,19 +1,21 @@
<template> <template>
<div class="mt-10 flex items-center justify-center"> <div class="mt-10 flex items-center justify-center">
<nav class="isolate inline-flex -space-x-px rounded-md shadow-sm" aria-label="Pagination"> <nav class="flex items-center gap-x-1" aria-label="Pagination">
<Link as="button" :href="links.prev" :disabled="links.prev === null" <Link :href="links.links[0].url" as="button" class="min-h-9.5 min-w-9.5 py-2 px-2.5 inline-flex justify-center items-center gap-x-2 text-sm rounded-lg border border-transparent text-gray-800 hover:bg-gray-100 focus:outline-hidden focus:bg-gray-100 disabled:opacity-50 disabled:pointer-events-none" aria-label="Previous">
class="relative inline-flex items-center gap-1 rounded-l-md border border-gray-300 bg-white px-3 py-2 pr-4 text-sm font-medium text-gray-500 hover:bg-gray-50 focus:z-20 disabled:pointer-events-none disabled:opacity-40"> <svg class="shrink-0 size-3.5" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" <path d="m15 18-6-6 6-6"></path>
stroke="currentColor" aria-hidden="true" data-slot="icon" class="h-3 w-3">
<path stroke-linecap="round" stroke-linejoin="round" d="M15.75 19.5 8.25 12l7.5-7.5"></path>
</svg> </svg>
<span>Предыдущая</span></Link> <span class="sr-only">Previous</span>
<Link as="button" :href="links.next" :disabled="links.next === null" </Link>
class="relative inline-flex items-center gap-1 rounded-r-md border border-gray-300 bg-white px-3 py-2 pl-4 text-sm font-medium text-gray-500 hover:bg-gray-50 focus:z-20 disabled:pointer-events-none disabled:opacity-40"> <div class="flex items-center gap-x-1">
<span>Следующая</span> <Link v-for="link in getPageLinks(links)" as="button" :href="link.url"
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" :class="link.active ? 'border-gray-200 text-gray-800' : 'border-transparent hover:bg-gray-100'"
stroke="currentColor" aria-hidden="true" data-slot="icon" class="h-3 w-3"> class="min-h-9.5 min-w-9.5 flex justify-center items-center border py-2 px-3 text-sm rounded-lg focus:outline-hidden focus:bg-gray-100 disabled:opacity-50 disabled:pointer-events-none">{{ link.label }}</Link>
<path stroke-linecap="round" stroke-linejoin="round" d="m8.25 4.5 7.5 7.5-7.5 7.5"></path> </div>
<Link :href="links.links.slice(-1).pop().url" as="button" class="min-h-9.5 min-w-9.5 py-2 px-2.5 inline-flex justify-center items-center gap-x-2 text-sm rounded-lg border border-transparent text-gray-800 hover:bg-gray-100 focus:outline-hidden focus:bg-gray-100 disabled:opacity-50 disabled:pointer-events-none" aria-label="Next">
<span class="sr-only">Next</span>
<svg class="shrink-0 size-3.5" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="m9 18 6-6-6-6"></path>
</svg> </svg>
</Link> </Link>
</nav> </nav>
@@ -31,6 +33,14 @@ export default {
required: true, required: true,
}, },
}, },
methods: {
getPageLinks(pagination) {
return pagination.links.filter(link =>
link.label !== 'pagination.previous' &&
link.label !== 'pagination.next'
);
}
}
}; };
</script> </script>