TimerSsmPost.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 Dcat\Admin\Traits\HasUploadedFile;
  10. use Illuminate\Console\Command;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Log;
  13. use Symfony\Component\DomCrawler\Crawler;
  14. /**
  15. * 定时任务:发送社媒帖子
  16. * php artisan timer:ssmPost
  17. */
  18. class TimerSsmPost extends Command
  19. {
  20. /**
  21. * The name and signature of the console command.
  22. *
  23. * @var string
  24. */
  25. protected $signature = 'timer:ssmPost';
  26. /**
  27. * The console command description.
  28. *
  29. * @var string
  30. */
  31. protected $description = '发送社媒帖子';
  32. use HasUploadedFile;
  33. public function handle()
  34. {
  35. try {
  36. //创建日志
  37. Log::info('开始生成发送帖子日志');
  38. $this->createLog();
  39. //刷新access_token
  40. Log::info('开始刷新access_token');
  41. $this->refreshAccessToken();
  42. //上传图片到oss
  43. Log::info('开始上传图片到oss');
  44. $ossUpload = $this->ossUpload();
  45. if (!$ossUpload) {
  46. Log::info('有正在上传的图片,请稍后再试,结束流程');
  47. echo '有正在上传的图片,请稍后再试';
  48. exit;
  49. }
  50. //开始发送社媒帖子
  51. Log::info('开始发送社媒帖子');
  52. $sendLog = SmmPostLog::getSendLog(5);
  53. $logIds = [];
  54. foreach ($sendLog as $log) {
  55. Log::info('开始发送社媒帖子,id:'. $log->id);
  56. echo '开始发送社媒帖子,id:'. $log->id. "\n";
  57. if ($log->media_name == 'Twitter' && SmmPostLog::twitterCanSend() == false) {
  58. //15分钟内不重复发送
  59. Log::info('twitter可发送时间未到,暂时无法发送,ID'.$log->post_id);
  60. continue;
  61. }
  62. Log::info('开始发送社媒帖子,把状态改为发送中');
  63. //发送社媒帖子中
  64. $log->status = 1;//发送中
  65. $log->updated_at = Carbon::now();
  66. $log->save();
  67. //发送社媒帖子中 end
  68. $logIds[] = $log->id;
  69. //获取帖子内容
  70. $post = SmmPost::getPostById($log->post_id);
  71. $message = $post->message;
  72. $imageVideoUrl = $post->image_video_url;
  73. $mediaName = $log->media_name;
  74. $postType = $post->post_type;
  75. $accountInfo = SmmUserAccount::getAccountById($log->account_id);
  76. $accessToken = $accountInfo->access_token;
  77. //发送帖子
  78. $configData = ['accountInfo' => $accountInfo->toArray(),'postData'=>$post->toArray()];
  79. $ssmService = new SmmService($mediaName,$configData);
  80. if ($postType == 0) {
  81. //图片帖子
  82. $imageVideoUrl = explode(',', $imageVideoUrl);
  83. foreach ($imageVideoUrl as $key => $url) {
  84. $imageVideoUrl[$key] = CommonHelper::ossUrl($url);
  85. }
  86. $response = $ssmService->postImage($message, $imageVideoUrl,$accessToken);
  87. Log::info('图片帖子返回'.json_encode($response));
  88. } else {
  89. $imageVideoUrl = CommonHelper::ossUrl($imageVideoUrl);
  90. $response = $ssmService->postVideo($message, $imageVideoUrl,$accessToken);
  91. Log::info('视频帖子返回'.json_encode($response));
  92. }
  93. $responseIds = isset($response['data']['responseIds'])? $response['data']['responseIds'] : [];
  94. //更新post_logs表
  95. if ($response['status'] == true) {
  96. $log->status = 2;//发送成功
  97. $log->response_ids = json_encode($responseIds);
  98. $log->updated_at = Carbon::now();
  99. $log->send_time = Carbon::now();
  100. $log->request_count = $log->request_count + 1;
  101. $log->save();
  102. } else {
  103. $log->status = 3;//发送失败
  104. $log->remark = $response['data'];
  105. $log->updated_at = Carbon::now();
  106. $log->send_time = Carbon::now();
  107. $log->request_count = $log->request_count + 1;
  108. $log->save();
  109. }
  110. }
  111. $logIds = json_encode($logIds);
  112. Log::info('发送社媒帖子完成'.$logIds);
  113. dd('发送社媒帖子完成'.$logIds);
  114. } catch (\Exception $e) {
  115. Log::info('发送社媒帖子失败:'.$e->getMessage());
  116. dd('发送社媒帖子失败:'.$e->getMessage());
  117. }
  118. }
  119. /*
  120. * access_token 过期重新获取
  121. * 1.Facebook 有效期60天,要手动重新获取
  122. * 2.Instagram 与 Facebook 一样
  123. * YouTube 有效期为 3600 秒,提前 10 分钟更新 access_token
  124. * Twitter 好像是过期
  125. */
  126. public function refreshAccessToken()
  127. {
  128. $accounts = SmmUserAccount::getAllYouTubeUserAccounts();
  129. foreach ($accounts as $account) {
  130. try {
  131. $expiresAt = Carbon::parse($account->expires_at);
  132. //少于15分钟就开始刷新access_token
  133. if ($expiresAt->diffInSeconds(Carbon::now()) <= 900) {
  134. Log::info('开始刷新access_token:'. $account->name);
  135. $mediaName = $account->getParent->name;
  136. $configData = ['accountInfo' => $account->toArray()];
  137. $ssmService = new SmmService($mediaName, $configData);
  138. $result = $ssmService->refreshAccessToken($account->refresh_token);
  139. if ($result['status']) {
  140. $account->access_token = $result['data']['access_token'];
  141. $account->expires_at = $result['data']['expires_at'];
  142. $account->refresh_token = $result['data']['refresh_token'];
  143. $account->save();
  144. Log::info('access_token 刷新成功:'. $account->name);
  145. } else {
  146. Log::info('access_token 刷新失败:'. $result['data']);
  147. }
  148. }
  149. } catch (\Exception $e) {
  150. Log::info('access_token 刷新失败:'. $account->name.$e->getMessage());
  151. }
  152. }
  153. }
  154. /*
  155. * 生成发送帖子日志
  156. */
  157. public function createLog()
  158. {
  159. // 发送社媒帖子
  160. $waitPost = SmmPost::getWaitPost();
  161. foreach ($waitPost as $post) {
  162. //插入post_logs表
  163. $accountIds = explode(',', $post->account_ids);
  164. $accounts = SmmUserAccount::getAccountsByIds($accountIds);
  165. foreach ($accounts as $account) {
  166. $send_time = Carbon::now();
  167. // 如果是Twitter,15分钟内只能发一个帖
  168. if ($account->getParent->name == 'Twitter') {
  169. $send_time = SmmPostLog::getTwitterSendTime();
  170. }
  171. //生成post_logs表数据
  172. $data = [
  173. 'post_id' => $post->id,
  174. 'account_id' => $account->id,
  175. 'account_name' => $account->name,
  176. 'status' => 0,
  177. 'remark' => '',
  178. 'created_at' => Carbon::now(),
  179. 'updated_at' => Carbon::now(),
  180. 'dist_id' => $account->dist_id,
  181. 'media_name' => $account->getParent->name,
  182. 'response_ids'=> '',
  183. 'send_time' => $send_time,
  184. ];
  185. SmmPostLog::createLog($data);
  186. Log::info('生成发送帖子日志:'. $post->id. ','. $account->name. ','. $send_time);
  187. }
  188. $post->status = 1;
  189. $post->save();
  190. }
  191. }
  192. public function ossUpload()
  193. {
  194. $count = SmmPost::getOssUploadingPostCount();
  195. if ($count > 0) {
  196. return false;
  197. }
  198. // 发送社媒帖子
  199. $ossUploadPost = SmmPost::getOssUploadPost();
  200. $disk = $this->disk('oss');
  201. foreach ($ossUploadPost as $post) {
  202. $post->oss_upload = 1;//上传中
  203. $post->save();
  204. //上传图片到oss
  205. $imageVideoUrl = explode(',', $post->image_video_url);
  206. foreach ($imageVideoUrl as $key => $url) {
  207. $localPath = toStoragePath($url);
  208. $filename = basename($url);
  209. $dir = dirname($url);
  210. $disk->putFileAs($dir, $localPath, $filename);
  211. }
  212. $post->oss_upload = 2; //上传完成
  213. $post->save();
  214. }
  215. return true;
  216. }
  217. }