TimerSsmPost.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. Log::info('开始发送社媒帖子,id:'. $log->id);
  46. echo '开始发送社媒帖子,id:'. $log->id. "\n";
  47. if ($log->media_name == 'twitter' && SmmPostLog::twitterCanSend() == false) {
  48. //15分钟内不重复发送
  49. Log::info('twitter可发送时间未到,暂时无法发送,ID'.$log->post_id);
  50. continue;
  51. }
  52. $logIds[] = $log->id;
  53. //获取帖子内容
  54. $post = SmmPost::getPostById($log->post_id);
  55. $message = $post->message;
  56. $imageVideoUrl = $post->image_video_url;
  57. $mediaName = $log->media_name;
  58. $postType = $post->post_type;
  59. $accountInfo = SmmUserAccount::getAccountById($log->account_id);
  60. $accessToken = $accountInfo->access_token;
  61. //发送帖子
  62. $configData = ['accountInfo' => $accountInfo->toArray()];
  63. $ssmService = new SmmService($mediaName,$configData);
  64. if ($postType == 0) {
  65. //图片帖子
  66. $imageVideoUrl = explode(',', $imageVideoUrl);
  67. foreach ($imageVideoUrl as $key => $url) {
  68. $imageVideoUrl[$key] = CommonHelper::ossUrl($url);
  69. }
  70. $response = $ssmService->postImage($message, $imageVideoUrl,$accessToken);
  71. Log::info('图片帖子返回'.json_encode($response));
  72. } else {
  73. $imageVideoUrl = CommonHelper::ossUrl($imageVideoUrl);
  74. $response = $ssmService->postVideo($message, $imageVideoUrl,$accessToken);
  75. Log::info('视频帖子返回'.json_encode($response));
  76. }
  77. $responseIds = isset($response['data']['responseIds'])? $response['data']['responseIds'] : [];
  78. //更新post_logs表
  79. if ($response['status'] == true) {
  80. $log->status = 1;
  81. $log->response_ids = json_encode($responseIds);
  82. $log->updated_at = Carbon::now();
  83. $log->send_time = Carbon::now();
  84. $log->request_count = $log->request_count + 1;
  85. $log->save();
  86. } else {
  87. $log->status = 2;
  88. $log->remark = $response['data'];
  89. $log->updated_at = Carbon::now();
  90. $log->send_time = Carbon::now();
  91. $log->request_count = $log->request_count + 1;
  92. $log->save();
  93. }
  94. }
  95. $logIds = json_encode($logIds);
  96. Log::info('发送社媒帖子完成'.$logIds);
  97. dd('发送社媒帖子完成'.$logIds);
  98. } catch (\Exception $e) {
  99. Log::info('发送社媒帖子失败:'.$e->getMessage());
  100. dd('发送社媒帖子失败:'.$e->getMessage());
  101. }
  102. }
  103. /*
  104. * access_token 过期重新获取
  105. * 1.Facebook 有效期60天,要手动重新获取
  106. * 2.Instagram 与 Facebook 一样
  107. * YouTube 有效期为 3600 秒,提前 10 分钟更新 access_token
  108. * Twitter 好像是过期
  109. */
  110. public function refreshAccessToken()
  111. {
  112. $accounts = SmmUserAccount::getAllYouTubeUserAccounts();
  113. foreach ($accounts as $account) {
  114. try {
  115. $expiresAt = Carbon::parse($account->expires_at);
  116. //少于10分钟就开始刷新access_token
  117. if ($expiresAt->diffInSeconds(Carbon::now()) <= 6000) {
  118. Log::info('开始刷新access_token:'. $account->name);
  119. $mediaName = $account->getParent->name;
  120. $configData = ['accountInfo' => $account->toArray()];
  121. $ssmService = new SmmService($mediaName, $configData);
  122. $result = $ssmService->refreshAccessToken($account->refresh_token);
  123. if ($result['status']) {
  124. $account->access_token = $result['data']['access_token'];
  125. $account->expires_at = $result['data']['expires_at'];
  126. $account->refresh_token = $result['data']['refresh_token'];
  127. $account->save();
  128. Log::info('access_token 刷新成功:'. $account->name);
  129. } else {
  130. Log::info('access_token 刷新失败:'. $result['data']);
  131. }
  132. }
  133. } catch (\Exception $e) {
  134. Log::info('access_token 刷新失败:'. $account->name.$e->getMessage());
  135. }
  136. }
  137. }
  138. /*
  139. * 生成发送帖子日志
  140. */
  141. public function createLog()
  142. {
  143. // 发送社媒帖子
  144. $waitPost = SmmPost::getWaitPost();
  145. foreach ($waitPost as $post) {
  146. //插入post_logs表
  147. $accountIds = explode(',', $post->account_ids);
  148. $accounts = SmmUserAccount::getAccountsByIds($accountIds);
  149. foreach ($accounts as $account) {
  150. $send_time = Carbon::now();
  151. // 如果是Twitter,15分钟内只能发一个帖
  152. if ($account->getParent->name == 'Twitter') {
  153. $send_time = SmmPostLog::getTwitterSendTime();
  154. }
  155. //生成post_logs表数据
  156. $data = [
  157. 'post_id' => $post->id,
  158. 'account_id' => $account->id,
  159. 'account_name' => $account->name,
  160. 'status' => 0,
  161. 'remark' => '',
  162. 'created_at' => Carbon::now(),
  163. 'updated_at' => Carbon::now(),
  164. 'dist_id' => $account->dist_id,
  165. 'media_name' => $account->getParent->name,
  166. 'response_ids'=> '',
  167. 'send_time' => $send_time,
  168. ];
  169. SmmPostLog::createLog($data);
  170. Log::info('生成发送帖子日志:'. $post->id. ','. $account->name. ','. $send_time);
  171. }
  172. $post->status = 1;
  173. $post->save();
  174. }
  175. }
  176. }