add_vk_module
This commit is contained in:
@@ -4,6 +4,10 @@ namespace App\Services\VK;
|
||||
|
||||
use App\Services\VK\Album\VkAlbumService;
|
||||
use App\Services\VK\Wall\VkWallService;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use VK\Client\VKApiClient;
|
||||
|
||||
class VkService
|
||||
@@ -16,6 +20,7 @@ class VkService
|
||||
$this->wallService = new VkWallService($vk);
|
||||
$this->albumService = new VkAlbumService($vk);
|
||||
$this->public_id = env('PUBLIC_ID');
|
||||
$this->vkAuthService = new VkAuthService();
|
||||
}
|
||||
|
||||
public function getPosts(int $count = 10)
|
||||
@@ -28,16 +33,50 @@ class VkService
|
||||
return $this->wallService->getPostById($id);
|
||||
}
|
||||
|
||||
public function createPost(string $title, string $message, array $images = [], int|null $publish_date = null)
|
||||
// public function createPost(string $title, string $message, array $images = [], array $videos = [], int|null $publish_date = null)
|
||||
// {
|
||||
// $from_group = 1;
|
||||
// $attachments = [];
|
||||
//
|
||||
// if (!empty($images)) {
|
||||
// if (count($images) <= 10) {
|
||||
// $attachments[] = $this->prepareWallPhotos($images);
|
||||
// }
|
||||
// else{
|
||||
// $album = $this->createAlbum($title, $images);
|
||||
// $attachments[] = $this->createAlbumAttachmentParam($album['id']);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// $attachmentString = implode(',', $attachments);
|
||||
//
|
||||
// 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)
|
||||
{
|
||||
$from_group = 1;
|
||||
$album_attachment = '';
|
||||
if ($images !== []) {
|
||||
$album = $this->createAlbum($title, $images);
|
||||
$album_attachment = $this->createAlbumAttachmentParam($album['id']);
|
||||
$attachments = [];
|
||||
|
||||
if (!empty($images)) {
|
||||
if (count($images) <= 10) {
|
||||
$attachments[] = $this->prepareWallPhotos($images);
|
||||
} else {
|
||||
$attachments[] = $this->prepareWallPhotos(array_slice($images, 0, 10));
|
||||
|
||||
$album = $this->createAlbum($title, $images);
|
||||
if (isset($album['id'])) {
|
||||
$albumLink = "https://vk.com/album-{$this->public_id}_{$album['id']}";
|
||||
$message .= "\n[{$albumLink}|Ссылка на все фотографии]";
|
||||
} else {
|
||||
Log::error('Не удалось создать альбом');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->wallService->createPost($message, $from_group, $album_attachment, $publish_date);
|
||||
$attachmentString = implode(',', $attachments);
|
||||
|
||||
return $this->wallService->createPost($message, $from_group, $attachmentString, $publish_date);
|
||||
}
|
||||
|
||||
public function updatePost(int $id, string $title, string $message, array $images = [], int|null $publish_date = null)
|
||||
@@ -59,6 +98,13 @@ class VkService
|
||||
return $this->wallService->updatePost($id, $message, $from_group, $album_attachment, $publish_date);
|
||||
}
|
||||
|
||||
public function deletePost(int $post_id){
|
||||
$postRelation = DB::table('posts_vk_posts')->select()->where('post_id', $post_id)->first();
|
||||
$post_id = $postRelation->vk_post_id;
|
||||
$vk_post = $this->getPostById($post_id);
|
||||
$this->wallService->deletePost($vk_post['id']);
|
||||
}
|
||||
|
||||
public function createAlbum(string $title, $images)
|
||||
{
|
||||
$album = $this->albumService->createAlbum($title);
|
||||
@@ -89,4 +135,129 @@ class VkService
|
||||
return (isset($filteredAttachment[0]) ? $filteredAttachment[0] : null);
|
||||
}
|
||||
|
||||
private function prepareWallPhotos(array $images){
|
||||
$baseUrl = config('app.url');
|
||||
try {
|
||||
$imagePaths = array_map(function($img) use ($baseUrl) {
|
||||
return $baseUrl . $img;
|
||||
}, $images);
|
||||
|
||||
$photos = $this->uploadWallPhotos($imagePaths, env('PUBLIC_ID'));
|
||||
|
||||
return implode(',', array_map(function($photo) {
|
||||
return "photo{$photo['owner_id']}_{$photo['id']}";
|
||||
}, $photos));
|
||||
} catch (Exception $e) {
|
||||
Log::error("Ошибка: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function prepareVideos(array $videos)
|
||||
{
|
||||
$baseUrl = config('app.url');
|
||||
$videoPaths = array_map(function($vid) use ($baseUrl) {
|
||||
return $baseUrl . $vid;
|
||||
}, $videos);
|
||||
return $this->uploadVideos($videoPaths, env('PUBLIC_ID')); // Возвращает массив, например: ['video123_456', 'video789_012']
|
||||
}
|
||||
|
||||
private function uploadVideos(array $videoPaths, ?int $groupId = null): array
|
||||
{
|
||||
$uploadedVideos = [];
|
||||
|
||||
foreach ($videoPaths as $videoPath) {
|
||||
try {
|
||||
// Шаг 1: Получение сервера для загрузки
|
||||
$saveResponse = Http::get('https://api.vk.com/method/video.save', [
|
||||
'group_id' => $groupId,
|
||||
'access_token' => $this->vkAuthService->getToken()->access_token,
|
||||
'v' => '5.131',
|
||||
]);
|
||||
|
||||
$saveData = $saveResponse->json();
|
||||
if (!isset($saveData['response']['upload_url'])) {
|
||||
throw new Exception('Не удалось получить сервер для загрузки видео: ' . $videoPath);
|
||||
}
|
||||
$uploadUrl = $saveData['response']['upload_url'];
|
||||
|
||||
// Шаг 2: Загрузка видео на сервер
|
||||
$uploadResponse = Http::attach(
|
||||
'video_file',
|
||||
file_get_contents($videoPath),
|
||||
basename($videoPath)
|
||||
)->post($uploadUrl);
|
||||
|
||||
$uploadData = $uploadResponse->json();
|
||||
if (!isset($uploadData['video_id']) || !isset($uploadData['owner_id'])) {
|
||||
throw new Exception('Не удалось загрузить видео: ' . $videoPath);
|
||||
}
|
||||
|
||||
|
||||
$attachment = "video{$uploadData['owner_id']}_{$uploadData['video_id']}";
|
||||
$uploadedVideos[] = $attachment;
|
||||
} catch (Exception $e) {
|
||||
Log::error("Ошибка при загрузке видео {$videoPath}: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return $uploadedVideos;
|
||||
}
|
||||
|
||||
private function uploadWallPhotos(array $imagePaths, ?int $groupId = null): array
|
||||
{
|
||||
$uploadedPhotos = [];
|
||||
|
||||
foreach ($imagePaths as $imagePath) {
|
||||
try {
|
||||
$uploadServerResponse = Http::get('https://api.vk.com/method/photos.getWallUploadServer', [
|
||||
'group_id' => $groupId,
|
||||
'access_token' => $this->vkAuthService->getToken()->access_token,
|
||||
'v' => '5.131',
|
||||
]);
|
||||
|
||||
$uploadServerData = $uploadServerResponse->json();
|
||||
if (!isset($uploadServerData['response']['upload_url'])) {
|
||||
throw new Exception('Не удалось получить сервер для загрузки для изображения: ' . $imagePath);
|
||||
}
|
||||
$uploadUrl = $uploadServerData['response']['upload_url'];
|
||||
|
||||
// Шаг 2: Загрузка фотографии на сервер
|
||||
$uploadResponse = Http::attach(
|
||||
'photo',
|
||||
file_get_contents($imagePath),
|
||||
basename($imagePath)
|
||||
)->post($uploadUrl);
|
||||
|
||||
$uploadData = $uploadResponse->json();
|
||||
if (!isset($uploadData['photo']) || !isset($uploadData['server']) || !isset($uploadData['hash'])) {
|
||||
throw new Exception('Не удалось загрузить фотографию: ' . $imagePath);
|
||||
}
|
||||
|
||||
// Шаг 3: Сохранение фотографии
|
||||
$saveResponse = Http::get('https://api.vk.com/method/photos.saveWallPhoto', [
|
||||
'group_id' => $groupId,
|
||||
'photo' => $uploadData['photo'],
|
||||
'server' => $uploadData['server'],
|
||||
'hash' => $uploadData['hash'],
|
||||
'access_token' => $this->vkAuthService->getToken()->access_token,
|
||||
'v' => '5.131',
|
||||
]);
|
||||
|
||||
$saveData = $saveResponse->json();
|
||||
if (!isset($saveData['response'][0])) {
|
||||
throw new Exception('Не удалось сохранить фотографию: ' . $imagePath);
|
||||
}
|
||||
|
||||
// Добавляем данные о загруженной фотографии в массив
|
||||
$uploadedPhotos[] = $saveData['response'][0];
|
||||
Log::info("Фотография успешно загружена. ID: " . $saveData['response'][0]['id']);
|
||||
} catch (Exception $e) {
|
||||
// Логируем ошибку, но продолжаем загрузку остальных изображений
|
||||
Log::error("Ошибка при загрузке изображения {$imagePath}: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return $uploadedPhotos; // Возвращаем массив с данными о загруженных фотографиях
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user