|
@@ -36,38 +36,23 @@ class TimerSsmPost extends Command
|
|
|
|
|
|
public function handle()
|
|
|
{
|
|
|
- // 发送社媒帖子
|
|
|
- $waitPost = SmmPost::getWaitPost();
|
|
|
- foreach ($waitPost as $post) {
|
|
|
- //插入post_logs表
|
|
|
- $accountIds = explode(',', $post->account_ids);
|
|
|
- $accounts = SmmUserAccount::getAccountsByIds($accountIds);
|
|
|
- foreach ($accounts as $account) {
|
|
|
- //生成post_logs表数据
|
|
|
- $data = [
|
|
|
- 'post_id' => $post->id,
|
|
|
- 'account_id' => $account->id,
|
|
|
- 'account_name' => $account->name,
|
|
|
- 'status' => 0,
|
|
|
- 'remark' => '',
|
|
|
- 'created_at' => Carbon::now(),
|
|
|
- 'updated_at' => Carbon::now(),
|
|
|
- 'dist_id' => $account->dist_id,
|
|
|
- 'media_name' => $account->getParent->name,
|
|
|
- 'response_ids'=> '',
|
|
|
- 'send_time' => $post->send_time,
|
|
|
- ];
|
|
|
- SmmPostLog::createLog($data);
|
|
|
- }
|
|
|
- $post->status = 1;
|
|
|
- $post->save();
|
|
|
- }
|
|
|
+ //创建日志
|
|
|
+ $this->createLog();
|
|
|
|
|
|
- //dd('middle');
|
|
|
+ //刷新access_token
|
|
|
+ $this->refreshAccessToken();
|
|
|
+ exit;
|
|
|
|
|
|
//发送社媒帖子开始
|
|
|
- $sendLog = SmmPostLog::getSendLog();
|
|
|
+ $sendLog = SmmPostLog::getSendLog(5);
|
|
|
+ $logIds = [];
|
|
|
foreach ($sendLog as $log) {
|
|
|
+ if ($log->media_name == 'twitter' && SmmPostLog::twitterCanSend() == false) {
|
|
|
+ //15分钟内不重复发送
|
|
|
+ echo 'twitter可发送时间未到,暂时无法发送,ID'.$log->post_id;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $logIds[] = $log->id;
|
|
|
//获取帖子内容
|
|
|
$post = SmmPost::getPostById($log->post_id);
|
|
|
$message = $post->message;
|
|
@@ -89,16 +74,7 @@ class TimerSsmPost extends Command
|
|
|
} else {
|
|
|
$imageVideoUrl = CommonHelper::ossUrl($imageVideoUrl);
|
|
|
$response = $ssmService->postVideo($message, $imageVideoUrl,$accessToken);
|
|
|
-
|
|
|
-// $response = $ssmService->postVideo([
|
|
|
-// 'title' => 'Test Video',
|
|
|
-// 'description' => 'This is a test video111',
|
|
|
-// 'categoryId' => '22', // People & Blogs
|
|
|
-// 'privacyStatus' => 'private'
|
|
|
-// ], '/mnt/hgfs/wwwroot/mtb_dcatadmin_plus/1.mp4', $accessToken);
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
//更新post_logs表
|
|
|
if ($response['status'] == true) {
|
|
|
$log->status = 1;
|
|
@@ -114,7 +90,83 @@ class TimerSsmPost extends Command
|
|
|
$log->save();
|
|
|
}
|
|
|
}
|
|
|
- dd('发送社媒帖子完成');
|
|
|
+
|
|
|
+ $logIds = print_r($logIds);
|
|
|
+ dd('发送社媒帖子完成'.$logIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * access_token 过期重新获取
|
|
|
+ * 1.Facebook 有效期60天,要手动重新获取
|
|
|
+ * 2.Instagram 与 Facebook 一样
|
|
|
+ * YouTube 有效期为 3600 秒,提前 10 分钟更新 access_token
|
|
|
+ * Twitter 好像是过期
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+ public function refreshAccessToken()
|
|
|
+ {
|
|
|
+ $accounts = SmmUserAccount::getAllYouTubeUserAccounts();
|
|
|
+ foreach ($accounts as $account) {
|
|
|
+ try {
|
|
|
+ $expiresAt = Carbon::parse($account->expires_at);
|
|
|
+ if ($expiresAt->diffInSeconds(Carbon::now()) <= 3000) {
|
|
|
+ $mediaName = $account->getParent->name;
|
|
|
+ $configData = ['accountInfo' => $account->toArray()];
|
|
|
+
|
|
|
+ $ssmService = new SmmService($mediaName, $configData);
|
|
|
+ $result = $ssmService->refreshAccessToken($account->refresh_token);
|
|
|
+
|
|
|
+ $account->update([
|
|
|
+ 'access_token' => $result['data']['access_token'],
|
|
|
+ 'expires_at' => Carbon::parse($result['data']['expires_at']->format('Y-m-d H:i:s')),
|
|
|
+ 'refresh_token' => $result['data']['refresh_token'],
|
|
|
+ ]);
|
|
|
+
|
|
|
+ echo 'access_token 刷新成功:'. $account->name. "\n";
|
|
|
+ }
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 生成发送帖子日志
|
|
|
+ */
|
|
|
+ public function createLog()
|
|
|
+ {
|
|
|
+ // 发送社媒帖子
|
|
|
+ $waitPost = SmmPost::getWaitPost();
|
|
|
+ foreach ($waitPost as $post) {
|
|
|
+ //插入post_logs表
|
|
|
+ $accountIds = explode(',', $post->account_ids);
|
|
|
+ $accounts = SmmUserAccount::getAccountsByIds($accountIds);
|
|
|
+ foreach ($accounts as $account) {
|
|
|
+ $send_time = $post->send_time;
|
|
|
+ // 如果是Twitter,15分钟内只能发一个帖
|
|
|
+ if ($account->getParent->name == 'Twitter') {
|
|
|
+ $send_time = SmmPostLog::getTwitterSendTime($send_time);
|
|
|
+ }
|
|
|
+ //生成post_logs表数据
|
|
|
+ $data = [
|
|
|
+ 'post_id' => $post->id,
|
|
|
+ 'account_id' => $account->id,
|
|
|
+ 'account_name' => $account->name,
|
|
|
+ 'status' => 0,
|
|
|
+ 'remark' => '',
|
|
|
+ 'created_at' => Carbon::now(),
|
|
|
+ 'updated_at' => Carbon::now(),
|
|
|
+ 'dist_id' => $account->dist_id,
|
|
|
+ 'media_name' => $account->getParent->name,
|
|
|
+ 'response_ids'=> '',
|
|
|
+ 'send_time' => $send_time,
|
|
|
+ ];
|
|
|
+ SmmPostLog::createLog($data);
|
|
|
+ }
|
|
|
+ $post->status = 1;
|
|
|
+ $post->save();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|