TimerSsmPost.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. $waitPost = SmmPost::getWaitPost();
  34. foreach ($waitPost as $post) {
  35. //插入post_logs表
  36. $accountIds = explode(',', $post->account_ids);
  37. $accounts = SmmUserAccount::getAccountsByIds($accountIds);
  38. foreach ($accounts as $account) {
  39. //生成post_logs表数据
  40. $data = [
  41. 'post_id' => $post->id,
  42. 'account_id' => $account->id,
  43. 'account_name' => $account->name,
  44. 'status' => 0,
  45. 'remark' => '',
  46. 'created_at' => Carbon::now(),
  47. 'updated_at' => Carbon::now(),
  48. 'dist_id' => $account->dist_id,
  49. 'media_name' => $account->getParent->name,
  50. 'response_ids'=> '',
  51. 'send_time' => $post->send_time,
  52. ];
  53. SmmPostLog::createLog($data);
  54. }
  55. $post->status = 1;
  56. $post->save();
  57. }
  58. //dd('middle');
  59. //发送社媒帖子开始
  60. $sendLog = SmmPostLog::getSendLog();
  61. foreach ($sendLog as $log) {
  62. //获取帖子内容
  63. $post = SmmPost::getPostById($log->post_id);
  64. $message = $post->message;
  65. $imageVideoUrl = $post->image_video_url;
  66. // $imageVideoUrl = storage_path($imageVideoUrl);
  67. $mediaName = $log->media_name;
  68. $postType = $post->post_type;
  69. $accountInfo = SmmUserAccount::getAccountById($log->account_id);
  70. $accessToken = $accountInfo->access_token;
  71. //发送帖子
  72. $configData = ['accountInfo' => $accountInfo->toArray()];
  73. $ssmService = new SmmService($mediaName,$configData);
  74. if ($postType == 0) {
  75. //图片帖子
  76. $imageVideoUrl = explode(',', $imageVideoUrl);
  77. foreach ($imageVideoUrl as $key => $url) {
  78. $imageVideoUrl[$key] = CommonHelper::ossUrl($url);
  79. }
  80. $response = $ssmService->postImage($message, $imageVideoUrl,$accessToken);
  81. } else {
  82. $imageVideoUrl = CommonHelper::ossUrl($imageVideoUrl);
  83. $response = $ssmService->postVideo($message, $imageVideoUrl,$accessToken);
  84. }
  85. // dd($response);
  86. //更新post_logs表
  87. if ($response['status'] == true) {
  88. $log->status = 1;
  89. $log->response_ids = json_encode($response['data']['responseIds']);
  90. $log->request_content = json_encode($response['data']['requestContent']);
  91. $log->response_content = json_encode($response['data']['responseContent']);
  92. $log->updated_at = Carbon::now();
  93. $log->send_time = Carbon::now();
  94. $log->save();
  95. } else {
  96. $log->status = 2;
  97. $log->remark = $response['data'];
  98. $log->updated_at = Carbon::now();
  99. $log->send_time = Carbon::now();
  100. $log->save();
  101. }
  102. }
  103. dd('发送社媒帖子完成');
  104. }
  105. }