moshaorui před 19 hodinami
rodič
revize
6b8ee8a043

+ 4 - 2
app/Console/Commands/TimerSsmPost.php

@@ -46,13 +46,13 @@ class TimerSsmPost extends Command
             Log::info('开始刷新access_token');
             $this->refreshAccessToken();
 
-
-
             //发送社媒帖子开始
             Log::info('开始发送社媒帖子');
             $sendLog = SmmPostLog::getSendLog(5);
             $logIds = [];
             foreach ($sendLog as $log) {
+                Log::info('开始发送社媒帖子,id:'. $log->id);
+                echo '开始发送社媒帖子,id:'. $log->id. "\n";
                 if ($log->media_name == 'twitter' && SmmPostLog::twitterCanSend() == false) {
                     //15分钟内不重复发送
                     Log::info('twitter可发送时间未到,暂时无法发送,ID'.$log->post_id);
@@ -90,12 +90,14 @@ class TimerSsmPost extends Command
                     $log->response_ids = json_encode($responseIds);
                     $log->updated_at = Carbon::now();
                     $log->send_time = Carbon::now();
+                    $log->request_count = $log->request_count + 1;
                     $log->save();
                 } else {
                     $log->status = 2;
                     $log->remark = $response['data'];
                     $log->updated_at = Carbon::now();
                     $log->send_time = Carbon::now();
+                    $log->request_count = $log->request_count + 1;
                     $log->save();
                 }
             }

+ 1 - 3
app/Distributor/Controllers/SmmPostLogController.php

@@ -42,7 +42,7 @@ class SmmPostLogController extends AdminController
                 1 => 'success',
                 2 => 'orange1',
             ]);
-            $grid->column('remark')->width('15%');
+            //$grid->column('remark')->width('15%');
             $grid->column('send_time', admin_trans_label('etd'))->sortable();
             $grid->disableRowSelector();
             $grid->disableCreateButton();
@@ -65,6 +65,4 @@ class SmmPostLogController extends AdminController
             $show->disableDeleteButton();
         });
     }
-
-
 }

+ 2 - 2
app/Distributor/Repositories/SmmPostLog.php

@@ -43,7 +43,7 @@ class SmmPostLog extends EloquentRepository
     public static function getSendLog($limit = 5)
     {
         $log = new Model();
-        $logs = $log->where('status', 0)->where('send_time', '<', Carbon::now())->limit($limit)->get();
+        $logs = $log->wherein('status', [0,2])->where('request_count', '<=', 2)->where('send_time', '<', Carbon::now())->limit($limit)->get();
         return $logs;
     }
 
@@ -120,7 +120,7 @@ class SmmPostLog extends EloquentRepository
     {
         $log = new Model();
         //查找最后一条发送记录
-        $lastSendLog = $log->where('status', 1)->where('media_name', 'Twitter')->orderByDesc('send_time')->first();
+        $lastSendLog = $log->wherein('status', [1,2])->where('media_name', 'Twitter')->orderByDesc('id')->first();
         //如果时间间隔小于15分钟,则不允许发送
         if ($lastSendLog) {
             $lastSendTime = Carbon::parse($lastSendLog->send_time);

+ 8 - 6
app/Services/Smm/FacebookService.php

@@ -12,7 +12,10 @@ use Illuminate\Support\Facades\Session;
 class FacebookService implements SmmPlatformInterface
 {
     protected $configData;
-    protected $graphVersion = 'v19.0';
+    protected $graphVersion = 'v22.0';
+
+    protected $timeout = 60;//总超时时间 30 秒
+    protected $connectTimeout = 15;//连接阶段超时 10 秒
 
     public function __construct($configData)
     {
@@ -119,7 +122,7 @@ class FacebookService implements SmmPlatformInterface
                 foreach ($imagePaths as $imagePath) {
                     // 上传图片
                     Log::info('开始上传图片');
-                    $uploadResponse = Http::withToken($page['pageAccessToken'])
+                    $uploadResponse = Http::timeout($this->timeout)->connectTimeout($this->connectTimeout)->withToken($page['pageAccessToken'])
                         ->attach('source', file_get_contents($imagePath), basename($imagePath))
                         ->post("https://graph.facebook.com/{$this->graphVersion}/{$page['id']}/photos", [
                             'published' => false,
@@ -133,7 +136,7 @@ class FacebookService implements SmmPlatformInterface
 
                 // 发布帖子
                 Log::info('开始发布帖子');
-                $postResponse = Http::withToken($page['pageAccessToken'])
+                $postResponse = Http::timeout($this->timeout)->connectTimeout($this->connectTimeout)->withToken($page['pageAccessToken'])
                     ->post("https://graph.facebook.com/{$this->graphVersion}/{$page['id']}/feed", [
                         'message'         => $message,
                         'attached_media'  => json_encode($mediaIds),
@@ -170,7 +173,7 @@ class FacebookService implements SmmPlatformInterface
 
             foreach ($pagesInfo['data'] as $page) {
                 Log::info('开始发布视频');
-                $response = Http::withToken($page['pageAccessToken'])
+                $response = Http::timeout($this->timeout)->connectTimeout($this->connectTimeout)->withToken($page['pageAccessToken'])
                     ->attach('source', file_get_contents($videoPath), basename($videoPath))
                     ->post("https://graph.facebook.com/{$this->graphVersion}/{$page['id']}/videos", [
                         'description' => $message,
@@ -215,10 +218,9 @@ class FacebookService implements SmmPlatformInterface
     public function getAccountsInfo($accessToken)
     {
         try {
-            $response = Http::withToken($accessToken)
+            $response = Http::timeout($this->timeout)->connectTimeout($this->connectTimeout)->withToken($accessToken)
                 ->get("https://graph.facebook.com/{$this->graphVersion}/me/accounts");
 
-
             if ($response->failed()) {
                 throw new \Exception($response->json('error.message'));
             }

+ 11 - 8
app/Services/Smm/InstagramService.php

@@ -19,6 +19,9 @@ class InstagramService implements SmmPlatformInterface
     // 构造函数,传入配置信息
     public $configData = [];
 
+    protected $timeout = 60;//总超时时间 30 秒
+    protected $connectTimeout = 15;//连接阶段超时 10 秒
+
     public function __construct($configData) {
         $this->configData = $configData;
         $this->clientId = env('SSM_INSTAGRAM_APP_ID');
@@ -125,7 +128,7 @@ class InstagramService implements SmmPlatformInterface
                     'caption' => $message,
                     'access_token' => $accessToken,
                 ];
-                $containerResponse = Http::asForm()->post("https://graph.instagram.com/v21.0/{$igUserId}/media",$postData);
+                $containerResponse = Http::timeout($this->timeout)->connectTimeout($this->connectTimeout)->asForm()->post("https://graph.instagram.com/v21.0/{$igUserId}/media",$postData);
 
                 $containerData = $containerResponse->json();
 
@@ -136,7 +139,7 @@ class InstagramService implements SmmPlatformInterface
 
                 Log::info('开始发布媒体容器');
                 // Publish the container
-                $publishResponse = Http::post("https://graph.instagram.com/v21.0/{$igUserId}/media_publish", [
+                $publishResponse = Http::timeout($this->timeout)->connectTimeout($this->connectTimeout)->post("https://graph.instagram.com/v21.0/{$igUserId}/media_publish", [
                     'creation_id' => $containerData['id'],
                     'access_token' => $accessToken,
                 ]);
@@ -168,7 +171,7 @@ class InstagramService implements SmmPlatformInterface
                     'postData' => $postData,
                 ];
                 Log::info('上传子媒体容器');
-                $childResponse = Http::asForm()->post("https://graph.instagram.com/v21.0/{$igUserId}/media", $postData);
+                $childResponse = Http::timeout($this->timeout)->connectTimeout($this->connectTimeout)->asForm()->post("https://graph.instagram.com/v21.0/{$igUserId}/media", $postData);
 
                 $childData = $childResponse->json();
                 $responseContent[] = $childData;
@@ -189,7 +192,7 @@ class InstagramService implements SmmPlatformInterface
                 'postData' => $postData,
             ];
             Log::info('Create carousel container');
-            $carouselResponse = Http::asForm()->post("https://graph.instagram.com/v21.0/{$igUserId}/media",$postData );
+            $carouselResponse = Http::timeout($this->timeout)->connectTimeout($this->connectTimeout)->asForm()->post("https://graph.instagram.com/v21.0/{$igUserId}/media",$postData );
 
             $carouselData = $carouselResponse->json();
             if (!isset($carouselData['id'])) {
@@ -205,7 +208,7 @@ class InstagramService implements SmmPlatformInterface
                 'postData' => $postData,
             ];
             Log::info('Publish carousel');
-            $publishResponse = Http::post("https://graph.instagram.com/v21.0/{$igUserId}/media_publish", $postData);
+            $publishResponse = Http::timeout($this->timeout)->connectTimeout($this->connectTimeout)->post("https://graph.instagram.com/v21.0/{$igUserId}/media_publish", $postData);
 
             $publishData = $publishResponse->json();
             if (!isset($publishData['id'])) {
@@ -241,7 +244,7 @@ class InstagramService implements SmmPlatformInterface
                 'caption' => $message,
                 'access_token' => $accessToken,
             ];
-            $containerResponse = Http::asForm()->post("https://graph.instagram.com/v21.0/{$igUserId}/media", $postData);
+            $containerResponse = Http::timeout($this->timeout)->connectTimeout($this->connectTimeout)->asForm()->post("https://graph.instagram.com/v21.0/{$igUserId}/media", $postData);
             $containerData = $containerResponse->json();
             if (!isset($containerData['id'])) {
                 return ['status' => false, 'data' => '错误:无法创建视频容器 - ' . json_encode($containerData)];
@@ -257,7 +260,7 @@ class InstagramService implements SmmPlatformInterface
                     'fields' => 'status',
                     'access_token' => $accessToken,
                 ];
-                $statusResponse = Http::get("https://graph.instagram.com/{$containerData['id']}", $postData);
+                $statusResponse = Http::timeout($this->timeout)->connectTimeout($this->connectTimeout)->get("https://graph.instagram.com/{$containerData['id']}", $postData);
 
                 $statusData = $statusResponse->json();
                 $status = $statusData['status'] ?? 'ERROR';
@@ -276,7 +279,7 @@ class InstagramService implements SmmPlatformInterface
                 'creation_id' => $containerData['id'],
                 'access_token' => $accessToken,
             ];
-            $publishResponse = Http::post("https://graph.instagram.com/v21.0/{$igUserId}/media_publish",$postData);
+            $publishResponse = Http::timeout($this->timeout)->connectTimeout($this->connectTimeout)->post("https://graph.instagram.com/v21.0/{$igUserId}/media_publish",$postData);
             Log::info("发布视频完成");
             $publishData = $publishResponse->json();
             if (!isset($publishData['id'])) {