TimerSsmPost.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. try {
  34. //创建日志
  35. Log::info('开始生成发送帖子日志');
  36. $this->createLog();
  37. //刷新access_token
  38. Log::info('开始刷新access_token');
  39. $this->refreshAccessToken();
  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. $responseIds = isset($response['data']['responseIds'])? $response['data']['responseIds'] : [];
  76. //更新post_logs表
  77. if ($response['status'] == true) {
  78. $log->status = 1;
  79. $log->response_ids = json_encode($responseIds);
  80. $log->updated_at = Carbon::now();
  81. $log->send_time = Carbon::now();
  82. $log->save();
  83. } else {
  84. $log->status = 2;
  85. $log->remark = $response['data'];
  86. $log->updated_at = Carbon::now();
  87. $log->send_time = Carbon::now();
  88. $log->save();
  89. }
  90. }
  91. $logIds = json_encode($logIds);
  92. Log::info('发送社媒帖子完成'.$logIds);
  93. dd('发送社媒帖子完成'.$logIds);
  94. } catch (\Exception $e) {
  95. Log::info('发送社媒帖子失败:'.$e->getMessage());
  96. dd('发送社媒帖子失败:'.$e->getMessage());
  97. }
  98. }
  99. /*
  100. * access_token 过期重新获取
  101. * 1.Facebook 有效期60天,要手动重新获取
  102. * 2.Instagram 与 Facebook 一样
  103. * YouTube 有效期为 3600 秒,提前 10 分钟更新 access_token
  104. * Twitter 好像是过期
  105. */
  106. public function refreshAccessToken()
  107. {
  108. $accounts = SmmUserAccount::getAllYouTubeUserAccounts();
  109. foreach ($accounts as $account) {
  110. try {
  111. $expiresAt = Carbon::parse($account->expires_at);
  112. //少于10分钟就开始刷新access_token
  113. if ($expiresAt->diffInSeconds(Carbon::now()) <= 6000) {
  114. Log::info('开始刷新access_token:'. $account->name);
  115. $mediaName = $account->getParent->name;
  116. $configData = ['accountInfo' => $account->toArray()];
  117. $ssmService = new SmmService($mediaName, $configData);
  118. $result = $ssmService->refreshAccessToken($account->refresh_token);
  119. if ($result['status']) {
  120. $account->access_token = $result['data']['access_token'];
  121. $account->expires_at = $result['data']['expires_at'];
  122. $account->refresh_token = $result['data']['refresh_token'];
  123. $account->save();
  124. Log::info('access_token 刷新成功:'. $account->name);
  125. } else {
  126. Log::info('access_token 刷新失败:'. $result['data']);
  127. }
  128. }
  129. } catch (\Exception $e) {
  130. Log::info('access_token 刷新失败:'. $account->name.$e->getMessage());
  131. }
  132. }
  133. }
  134. /*
  135. * 生成发送帖子日志
  136. */
  137. public function createLog()
  138. {
  139. // 发送社媒帖子
  140. $waitPost = SmmPost::getWaitPost();
  141. foreach ($waitPost as $post) {
  142. //插入post_logs表
  143. $accountIds = explode(',', $post->account_ids);
  144. $accounts = SmmUserAccount::getAccountsByIds($accountIds);
  145. foreach ($accounts as $account) {
  146. $send_time = $post->send_time;
  147. // 如果是Twitter,15分钟内只能发一个帖
  148. if ($account->getParent->name == 'Twitter') {
  149. $send_time = SmmPostLog::getTwitterSendTime($send_time);
  150. }
  151. //生成post_logs表数据
  152. $data = [
  153. 'post_id' => $post->id,
  154. 'account_id' => $account->id,
  155. 'account_name' => $account->name,
  156. 'status' => 0,
  157. 'remark' => '',
  158. 'created_at' => Carbon::now(),
  159. 'updated_at' => Carbon::now(),
  160. 'dist_id' => $account->dist_id,
  161. 'media_name' => $account->getParent->name,
  162. 'response_ids'=> '',
  163. 'send_time' => $send_time,
  164. ];
  165. SmmPostLog::createLog($data);
  166. Log::info('生成发送帖子日志:'. $post->id. ','. $account->name. ','. $send_time);
  167. }
  168. $post->status = 1;
  169. $post->save();
  170. }
  171. }
  172. }