TimerSsmPost.php 7.6 KB

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