TimerSsmPost.php 6.7 KB

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