TimerSsmPost.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Distributor\Repositories\SmmPost;
  4. use App\Distributor\Repositories\SmmPostLog;
  5. use App\Distributor\Repositories\SmmUserAccount;
  6. use App\Libraries\CommonHelper;
  7. use App\Services\SmmService;
  8. use Carbon\Carbon;
  9. use Illuminate\Console\Command;
  10. use Illuminate\Support\Facades\DB;
  11. use Symfony\Component\DomCrawler\Crawler;
  12. /**
  13. * 定时任务:发送社媒帖子
  14. * php artisan timer:ssmPost
  15. */
  16. class TimerSsmPost extends Command
  17. {
  18. /**
  19. * The name and signature of the console command.
  20. *
  21. * @var string
  22. */
  23. protected $signature = 'timer:ssmPost';
  24. /**
  25. * The console command description.
  26. *
  27. * @var string
  28. */
  29. protected $description = '发送社媒帖子';
  30. public function handle()
  31. {
  32. //创建日志
  33. $this->createLog();
  34. //刷新access_token
  35. $this->refreshAccessToken();
  36. exit;
  37. //发送社媒帖子开始
  38. $sendLog = SmmPostLog::getSendLog(5);
  39. $logIds = [];
  40. foreach ($sendLog as $log) {
  41. if ($log->media_name == 'twitter' && SmmPostLog::twitterCanSend() == false) {
  42. //15分钟内不重复发送
  43. echo 'twitter可发送时间未到,暂时无法发送,ID'.$log->post_id;
  44. continue;
  45. }
  46. $logIds[] = $log->id;
  47. //获取帖子内容
  48. $post = SmmPost::getPostById($log->post_id);
  49. $message = $post->message;
  50. $imageVideoUrl = $post->image_video_url;
  51. $mediaName = $log->media_name;
  52. $postType = $post->post_type;
  53. $accountInfo = SmmUserAccount::getAccountById($log->account_id);
  54. $accessToken = $accountInfo->access_token;
  55. //发送帖子
  56. $configData = ['accountInfo' => $accountInfo->toArray()];
  57. $ssmService = new SmmService($mediaName,$configData);
  58. if ($postType == 0) {
  59. //图片帖子
  60. $imageVideoUrl = explode(',', $imageVideoUrl);
  61. foreach ($imageVideoUrl as $key => $url) {
  62. $imageVideoUrl[$key] = CommonHelper::ossUrl($url);
  63. }
  64. $response = $ssmService->postImage($message, $imageVideoUrl,$accessToken);
  65. } else {
  66. $imageVideoUrl = CommonHelper::ossUrl($imageVideoUrl);
  67. $response = $ssmService->postVideo($message, $imageVideoUrl,$accessToken);
  68. }
  69. //更新post_logs表
  70. if ($response['status'] == true) {
  71. $log->status = 1;
  72. $log->response_ids = json_encode($response['data']['responseIds']);
  73. $log->updated_at = Carbon::now();
  74. $log->send_time = Carbon::now();
  75. $log->save();
  76. } else {
  77. $log->status = 2;
  78. $log->remark = $response['data'];
  79. $log->updated_at = Carbon::now();
  80. $log->send_time = Carbon::now();
  81. $log->save();
  82. }
  83. }
  84. $logIds = print_r($logIds);
  85. dd('发送社媒帖子完成'.$logIds);
  86. }
  87. /*
  88. * access_token 过期重新获取
  89. * 1.Facebook 有效期60天,要手动重新获取
  90. * 2.Instagram 与 Facebook 一样
  91. * YouTube 有效期为 3600 秒,提前 10 分钟更新 access_token
  92. * Twitter 好像是过期
  93. */
  94. public function refreshAccessToken()
  95. {
  96. $accounts = SmmUserAccount::getAllYouTubeUserAccounts();
  97. foreach ($accounts as $account) {
  98. try {
  99. $expiresAt = Carbon::parse($account->expires_at);
  100. if ($expiresAt->diffInSeconds(Carbon::now()) <= 3000) {
  101. $mediaName = $account->getParent->name;
  102. $configData = ['accountInfo' => $account->toArray()];
  103. $ssmService = new SmmService($mediaName, $configData);
  104. $result = $ssmService->refreshAccessToken($account->refresh_token);
  105. $account->update([
  106. 'access_token' => $result['data']['access_token'],
  107. 'expires_at' => Carbon::parse($result['data']['expires_at']->format('Y-m-d H:i:s')),
  108. 'refresh_token' => $result['data']['refresh_token'],
  109. ]);
  110. echo 'access_token 刷新成功:'. $account->name. "\n";
  111. }
  112. } catch (\Exception $e) {
  113. continue;
  114. }
  115. }
  116. }
  117. /*
  118. * 生成发送帖子日志
  119. */
  120. public function createLog()
  121. {
  122. // 发送社媒帖子
  123. $waitPost = SmmPost::getWaitPost();
  124. foreach ($waitPost as $post) {
  125. //插入post_logs表
  126. $accountIds = explode(',', $post->account_ids);
  127. $accounts = SmmUserAccount::getAccountsByIds($accountIds);
  128. foreach ($accounts as $account) {
  129. $send_time = $post->send_time;
  130. // 如果是Twitter,15分钟内只能发一个帖
  131. if ($account->getParent->name == 'Twitter') {
  132. $send_time = SmmPostLog::getTwitterSendTime($send_time);
  133. }
  134. //生成post_logs表数据
  135. $data = [
  136. 'post_id' => $post->id,
  137. 'account_id' => $account->id,
  138. 'account_name' => $account->name,
  139. 'status' => 0,
  140. 'remark' => '',
  141. 'created_at' => Carbon::now(),
  142. 'updated_at' => Carbon::now(),
  143. 'dist_id' => $account->dist_id,
  144. 'media_name' => $account->getParent->name,
  145. 'response_ids'=> '',
  146. 'send_time' => $send_time,
  147. ];
  148. SmmPostLog::createLog($data);
  149. }
  150. $post->status = 1;
  151. $post->save();
  152. }
  153. }
  154. }