123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- namespace App\Console\Commands;
- use App\Distributor\Repositories\SmmPost;
- use App\Distributor\Repositories\SmmPostLog;
- use App\Distributor\Repositories\SmmUserAccount;
- use App\Libraries\CommonHelper;
- use App\Services\SmmService;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Symfony\Component\DomCrawler\Crawler;
- /**
- * 定时任务:发送社媒帖子
- * php artisan timer:ssmPost
- */
- class TimerSsmPost extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'timer:ssmPost';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '发送社媒帖子';
- public function handle()
- {
- //创建日志
- Log::info('开始生成发送帖子日志');
- $this->createLog();
- //刷新access_token
- Log::info('开始刷新access_token');
- $this->refreshAccessToken();
- exit;
- //发送社媒帖子开始
- Log::info('开始发送社媒帖子');
- $sendLog = SmmPostLog::getSendLog(5);
- $logIds = [];
- foreach ($sendLog as $log) {
- if ($log->media_name == 'twitter' && SmmPostLog::twitterCanSend() == false) {
- //15分钟内不重复发送
- Log::info('twitter可发送时间未到,暂时无法发送,ID'.$log->post_id);
- continue;
- }
- $logIds[] = $log->id;
- //获取帖子内容
- $post = SmmPost::getPostById($log->post_id);
- $message = $post->message;
- $imageVideoUrl = $post->image_video_url;
- $mediaName = $log->media_name;
- $postType = $post->post_type;
- $accountInfo = SmmUserAccount::getAccountById($log->account_id);
- $accessToken = $accountInfo->access_token;
- //发送帖子
- $configData = ['accountInfo' => $accountInfo->toArray()];
- $ssmService = new SmmService($mediaName,$configData);
- if ($postType == 0) {
- //图片帖子
- $imageVideoUrl = explode(',', $imageVideoUrl);
- foreach ($imageVideoUrl as $key => $url) {
- $imageVideoUrl[$key] = CommonHelper::ossUrl($url);
- }
- $response = $ssmService->postImage($message, $imageVideoUrl,$accessToken);
- Log::info('图片帖子返回'.json_encode($response));
- } else {
- $imageVideoUrl = CommonHelper::ossUrl($imageVideoUrl);
- $response = $ssmService->postVideo($message, $imageVideoUrl,$accessToken);
- Log::info('视频帖子返回'.json_encode($response));
- }
- //更新post_logs表
- if ($response['status'] == true) {
- $log->status = 1;
- $log->response_ids = json_encode($response['data']['responseIds']);
- $log->updated_at = Carbon::now();
- $log->send_time = Carbon::now();
- $log->save();
- } else {
- $log->status = 2;
- $log->remark = $response['data'];
- $log->updated_at = Carbon::now();
- $log->send_time = Carbon::now();
- $log->save();
- }
- }
- $logIds = json_encode($logIds);
- Log::info('发送社媒帖子完成'.$logIds);
- dd('发送社媒帖子完成'.$logIds);
- }
- /*
- * access_token 过期重新获取
- * 1.Facebook 有效期60天,要手动重新获取
- * 2.Instagram 与 Facebook 一样
- * YouTube 有效期为 3600 秒,提前 10 分钟更新 access_token
- * Twitter 好像是过期
- */
- public function refreshAccessToken()
- {
- $accounts = SmmUserAccount::getAllYouTubeUserAccounts();
- foreach ($accounts as $account) {
- try {
- $expiresAt = Carbon::parse($account->expires_at);
- //少于10分钟就开始刷新access_token
- if ($expiresAt->diffInSeconds(Carbon::now()) <= 6000) {
- Log::info('开始刷新access_token:'. $account->name);
- $mediaName = $account->getParent->name;
- $configData = ['accountInfo' => $account->toArray()];
- $ssmService = new SmmService($mediaName, $configData);
- $result = $ssmService->refreshAccessToken($account->refresh_token);
- if ($result['status']) {
- $account->access_token = $result['data']['access_token'];
- $account->expires_at = $result['data']['expires_at'];
- $account->refresh_token = $result['data']['refresh_token'];
- $account->save();
- Log::info('access_token 刷新成功:'. $account->name);
- } else {
- Log::info('access_token 刷新失败:'. $result['data']);
- }
- }
- } catch (\Exception $e) {
- Log::info('access_token 刷新失败:'. $account->name.$e->getMessage());
- }
- }
- }
- /*
- * 生成发送帖子日志
- */
- public function createLog()
- {
- // 发送社媒帖子
- $waitPost = SmmPost::getWaitPost();
- foreach ($waitPost as $post) {
- //插入post_logs表
- $accountIds = explode(',', $post->account_ids);
- $accounts = SmmUserAccount::getAccountsByIds($accountIds);
- foreach ($accounts as $account) {
- $send_time = $post->send_time;
- // 如果是Twitter,15分钟内只能发一个帖
- if ($account->getParent->name == 'Twitter') {
- $send_time = SmmPostLog::getTwitterSendTime($send_time);
- }
- //生成post_logs表数据
- $data = [
- 'post_id' => $post->id,
- 'account_id' => $account->id,
- 'account_name' => $account->name,
- 'status' => 0,
- 'remark' => '',
- 'created_at' => Carbon::now(),
- 'updated_at' => Carbon::now(),
- 'dist_id' => $account->dist_id,
- 'media_name' => $account->getParent->name,
- 'response_ids'=> '',
- 'send_time' => $send_time,
- ];
- SmmPostLog::createLog($data);
- Log::info('生成发送帖子日志:'. $post->id. ','. $account->name. ','. $send_time);
- }
- $post->status = 1;
- $post->save();
- }
- }
- }
|