From 836452add63fda9975266e3ae89201cd8dc518aa Mon Sep 17 00:00:00 2001 From: F4ilji Date: Fri, 20 Mar 2026 23:35:09 +0500 Subject: [PATCH] Changes --- .../Dashboard/Actions/PublishPostAction.php | 9 ++-- .../Tasks/CreatePostFromAiDataTask.php | 21 ++++++++- .../WEB/Controllers/PublishPostController.php | 2 +- app/Jobs/CreateVkPost.php | 10 +++- app/Jobs/CreateVkPostJob.php | 6 ++- app/Jobs/UpdateVkPostJob.php | 4 +- app/Services/VK/VkService.php | 4 +- app/Services/VK/Wall/VkWallService.php | 3 +- resources/js/Pages/Dashboard/Main.vue | 46 +++++++++++++++++-- 9 files changed, 88 insertions(+), 17 deletions(-) diff --git a/app/Containers/Dashboard/Actions/PublishPostAction.php b/app/Containers/Dashboard/Actions/PublishPostAction.php index 9a6ea11..aa77521 100644 --- a/app/Containers/Dashboard/Actions/PublishPostAction.php +++ b/app/Containers/Dashboard/Actions/PublishPostAction.php @@ -21,9 +21,10 @@ class PublishPostAction * Публикует черновик поста * * @param Post $post Пост для публикации + * @param bool $publishToVk Публиковать ли в VK * @return Post Опубликованный пост */ - public function run(Post $post): Post + public function run(Post $post, bool $publishToVk = true): Post { // Обновляем данные поста $updatedData = $this->postDataProcessor->processUpdate([ @@ -40,8 +41,10 @@ class PublishPostAction // Отправляем уведомления $this->sendNotifications($post); - // Публикуем в VK - $this->publishToVk($post); + // Публикуем в VK, если указано + if ($publishToVk) { + $this->publishToVk($post); + } // Обновляем слайд, если есть $this->updateSlide($post); diff --git a/app/Containers/Dashboard/Tasks/CreatePostFromAiDataTask.php b/app/Containers/Dashboard/Tasks/CreatePostFromAiDataTask.php index 366e406..f7e4b33 100644 --- a/app/Containers/Dashboard/Tasks/CreatePostFromAiDataTask.php +++ b/app/Containers/Dashboard/Tasks/CreatePostFromAiDataTask.php @@ -95,9 +95,12 @@ class CreatePostFromAiDataTask */ private function preparePostData(array $newsData, array $content, ?string $previewPath, array $imagesPaths): array { + $baseSlug = Str::slug($newsData['title'] ?? ''); + $slug = $this->generateUniqueSlug($baseSlug); + return [ 'title' => $newsData['title'] ?? 'Без названия', - 'slug' => Str::slug($newsData['title'] ?? '') . '-' . time(), + 'slug' => $slug, 'content' => $content, 'authors' => $newsData['authors'] ?? [], 'category_id' => $newsData['category_id'] ?? null, @@ -113,6 +116,22 @@ class CreatePostFromAiDataTask ]; } + /** + * Генерирует уникальный slug + */ + private function generateUniqueSlug(string $baseSlug): string + { + $slug = $baseSlug; + $count = 1; + + while (Post::where('slug', $slug)->exists()) { + $slug = $baseSlug . '-' . $count; + $count++; + } + + return $slug; + } + /** * Сохраняет теги поста */ diff --git a/app/Containers/Dashboard/UI/WEB/Controllers/PublishPostController.php b/app/Containers/Dashboard/UI/WEB/Controllers/PublishPostController.php index b2ec689..4508098 100644 --- a/app/Containers/Dashboard/UI/WEB/Controllers/PublishPostController.php +++ b/app/Containers/Dashboard/UI/WEB/Controllers/PublishPostController.php @@ -27,7 +27,7 @@ class PublishPostController extends Controller } try { - $publishedPost = $this->publishPostAction->run($post); + $publishedPost = $this->publishPostAction->run($post, $request->input('publish_to_vk', true)); return back()->with([ 'success' => 'Новость успешно опубликована: ' . $publishedPost->title, diff --git a/app/Jobs/CreateVkPost.php b/app/Jobs/CreateVkPost.php index 1e9c0be..74e49e1 100755 --- a/app/Jobs/CreateVkPost.php +++ b/app/Jobs/CreateVkPost.php @@ -26,7 +26,7 @@ class CreateVkPost implements ShouldQueue readonly private array $images = [], readonly private int $post_id, readonly private int|null $publish_date = null, - + readonly private string $primary_attachments_mode = 'carousel', ) {} @@ -34,7 +34,13 @@ class CreateVkPost implements ShouldQueue { try { $vkService = new VkService(); - $vk_post = $vkService->createPost($this->title, $this->text, $this->images, $this->publish_date); + $vk_post = $vkService->createPost( + $this->title, + $this->text, + $this->images, + $this->publish_date, + $this->primary_attachments_mode + ); DB::table('posts_vk_posts')->insert( [ 'post_id' => $this->post_id, diff --git a/app/Jobs/CreateVkPostJob.php b/app/Jobs/CreateVkPostJob.php index 1ff4117..14231c7 100755 --- a/app/Jobs/CreateVkPostJob.php +++ b/app/Jobs/CreateVkPostJob.php @@ -28,9 +28,10 @@ class CreateVkPostJob implements ShouldQueue protected $videos; protected $publish_date; protected $public_id; + protected $primary_attachments_mode; public function __construct( - int $post_id, string $title, string $message, array $images = [], array $videos = [], ?int $publish_date = null + int $post_id, string $title, string $message, array $images = [], array $videos = [], ?int $publish_date = null, string $primary_attachments_mode = 'carousel' ) { $this->post_id = $post_id; @@ -40,6 +41,7 @@ class CreateVkPostJob implements ShouldQueue $this->videos = $videos; $this->publish_date = $publish_date; $this->public_id = config('services.vk.public_id'); + $this->primary_attachments_mode = $primary_attachments_mode; } public function handle() @@ -96,7 +98,7 @@ class CreateVkPostJob implements ShouldQueue $attachmentString = implode(',', $selected_attachments); Log::info("{$logPrefix} Финальная строка вложений: '{$attachmentString}'"); - $vk_post = $wallService->createPost($this->message, $from_group, $attachmentString, $this->publish_date); + $vk_post = $wallService->createPost($this->message, $from_group, $attachmentString, $this->publish_date, $this->primary_attachments_mode); if (isset($vk_post['post_id'])) { Log::info("{$logPrefix} Пост успешно создан ID: {$vk_post['post_id']}"); diff --git a/app/Jobs/UpdateVkPostJob.php b/app/Jobs/UpdateVkPostJob.php index 986f49d..390ff8f 100755 --- a/app/Jobs/UpdateVkPostJob.php +++ b/app/Jobs/UpdateVkPostJob.php @@ -28,9 +28,10 @@ class UpdateVkPostJob implements ShouldQueue protected $videos; protected $publish_date; protected $public_id; + protected $primary_attachments_mode; public function __construct( - int $post_id, string $title, string $message, array $images = [], array $videos = [], ?int $publish_date = null + int $post_id, string $title, string $message, array $images = [], array $videos = [], ?int $publish_date = null, string $primary_attachments_mode = 'carousel' ) { $this->post_id = $post_id; @@ -40,6 +41,7 @@ class UpdateVkPostJob implements ShouldQueue $this->videos = $videos; $this->publish_date = $publish_date; $this->public_id = config('services.vk.public_id'); + $this->primary_attachments_mode = $primary_attachments_mode; } public function handle() diff --git a/app/Services/VK/VkService.php b/app/Services/VK/VkService.php index 33938c5..75d6b9d 100755 --- a/app/Services/VK/VkService.php +++ b/app/Services/VK/VkService.php @@ -53,7 +53,7 @@ class VkService // return $this->wallService->createPost($message, $from_group, $attachmentString, $publish_date); // } - public function createPost(string $title, string $message, array $images = [], array $videos = [], int|null $publish_date = null) + public function createPost(string $title, string $message, array $images = [], array $videos = [], int|null $publish_date = null, string $primary_attachments_mode = 'carousel') { $from_group = 1; $attachments = []; @@ -76,7 +76,7 @@ class VkService $attachmentString = implode(',', $attachments); - return $this->wallService->createPost($message, $from_group, $attachmentString, $publish_date); + return $this->wallService->createPost($message, $from_group, $attachmentString, $publish_date, $primary_attachments_mode); } public function updatePost(int $id, string $title, string $message, array $images = [], int|null $publish_date = null) diff --git a/app/Services/VK/Wall/VkWallService.php b/app/Services/VK/Wall/VkWallService.php index f1bc4d8..42dafcf 100755 --- a/app/Services/VK/Wall/VkWallService.php +++ b/app/Services/VK/Wall/VkWallService.php @@ -48,7 +48,7 @@ class VkWallService } } - public function createPost(string $message, int $from_group, string $attachments = '', int|null $publish_date = null) + public function createPost(string $message, int $from_group, string $attachments = '', int|null $publish_date = null, string $primary_attachments_mode = 'carousel') { $token = $this->vkAuthService->getToken()->access_token; $params = [ @@ -58,6 +58,7 @@ class VkWallService 'attachments' => $attachments, 'access_token' => $token, 'publish_date' => $publish_date, + 'primary_attachments_mode' => $primary_attachments_mode, 'v' => '5.131', ]; diff --git a/resources/js/Pages/Dashboard/Main.vue b/resources/js/Pages/Dashboard/Main.vue index 6ea203c..4d53951 100755 --- a/resources/js/Pages/Dashboard/Main.vue +++ b/resources/js/Pages/Dashboard/Main.vue @@ -182,7 +182,18 @@ Обновлено: {{ new Date(post.updated_at).toLocaleDateString('ru-RU') }} -
+
+ + + +