Changes
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\VK\Album;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use VK\Client\VKApiClient;
|
||||
|
||||
class VkAlbumService
|
||||
{
|
||||
private VKApiClient $vk;
|
||||
private string $wallToken;
|
||||
private string $serviceToken;
|
||||
private string $publicId;
|
||||
private string $publicDomain;
|
||||
|
||||
public function __construct(VKApiClient $vk) {
|
||||
$this->vk = $vk;
|
||||
$this->wallToken = env('WALL_ACCESS_VK_TOKEN');
|
||||
$this->serviceToken = env('SERVICE_ACCESS_VK_KEY');
|
||||
$this->publicId = env('PUBLIC_ID');
|
||||
$this->publicDomain = env('PUBLIC_DOMAIN');
|
||||
}
|
||||
|
||||
public function getServerForUploadImages()
|
||||
{
|
||||
return $this->vk->photos()->getUploadServer($this->wallToken, array(
|
||||
''
|
||||
));
|
||||
}
|
||||
|
||||
public function createAlbum(string $title)
|
||||
{
|
||||
try {
|
||||
return $this->vk->photos()->createAlbum($this->wallToken, array(
|
||||
'title' => $title,
|
||||
'group_id' => $this->publicId,
|
||||
'privacy' => 0,
|
||||
|
||||
));
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Ошибка при создании альбома: ' . $e->getMessage());
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'Не удалось создать альбом: ' . $e->getMessage(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\VK;
|
||||
|
||||
use App\Services\VK\Album\VkAlbumService;
|
||||
use App\Services\VK\Wall\VkWallService;
|
||||
use VK\Client\VKApiClient;
|
||||
|
||||
class VkService
|
||||
{
|
||||
protected VkWallService $wallService;
|
||||
protected VkAlbumService $albumService;
|
||||
public function __construct(VKApiClient $vk) {
|
||||
$this->wallService = new VkWallService($vk);
|
||||
$this->albumService = new VkAlbumService($vk);
|
||||
}
|
||||
|
||||
public function getPosts(int $count = 10)
|
||||
{
|
||||
return $this->wallService->getPosts($count);
|
||||
}
|
||||
|
||||
public function createPost(string $message, int $from_group = 1)
|
||||
{
|
||||
return $this->wallService->createPost($message, $from_group);
|
||||
}
|
||||
|
||||
public function createAlbum(string $title)
|
||||
{
|
||||
return $this->albumService->createAlbum($title);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\VK\Wall;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use VK\Client\VKApiClient;
|
||||
|
||||
class VkWallService
|
||||
{
|
||||
private VKApiClient $vk;
|
||||
private string $wallToken;
|
||||
private string $serviceToken;
|
||||
private string $publicId;
|
||||
private string $publicDomain;
|
||||
|
||||
public function __construct(VKApiClient $vk) {
|
||||
$this->vk = $vk;
|
||||
$this->wallToken = env('WALL_ACCESS_VK_TOKEN');
|
||||
$this->serviceToken = env('SERVICE_ACCESS_VK_KEY');
|
||||
$this->publicId = env('PUBLIC_ID');
|
||||
$this->publicDomain = env('PUBLIC_DOMAIN');
|
||||
}
|
||||
|
||||
public function getPosts(int $count)
|
||||
{
|
||||
return $this->vk->wall()->get($this->serviceToken, array(
|
||||
'owner_id' => '-'. $this->publicId,
|
||||
'domain' => $this->publicDomain,
|
||||
'count' => $count
|
||||
));
|
||||
}
|
||||
|
||||
public function createPost(string $message, int $from_group, string $attachments = '')
|
||||
{
|
||||
try {
|
||||
return $this->vk->wall()->post($this->wallToken, array(
|
||||
'owner_id' => '-' . $this->publicId,
|
||||
'from_group' => $from_group,
|
||||
'message' => $message,
|
||||
'attachments' => $attachments
|
||||
));
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Ошибка при создании поста: ' . $e->getMessage());
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'Не удалось создать пост: ' . $e->getMessage(),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user