moshaorui vor 1 Tag
Ursprung
Commit
66250b69b8

+ 15 - 5
app/Console/Commands/TimerSsmPost.php

@@ -47,7 +47,12 @@ class TimerSsmPost extends Command
 
             //上传图片到oss
             Log::info('开始上传图片到oss');
-            $this->ossUpload();
+            $ossUpload = $this->ossUpload();
+            if (!$ossUpload) {
+                Log::info('有正在上传的图片,请稍后再试');
+                echo '有正在上传的图片,请稍后再试';
+                exit;
+            }
 
             //刷新access_token
             Log::info('开始刷新access_token');
@@ -58,7 +63,6 @@ class TimerSsmPost extends Command
             $sendLog = SmmPostLog::getSendLog(5);
 
 
-
             $logIds = [];
             foreach ($sendLog as $log) {
                 Log::info('开始发送社媒帖子,id:'. $log->id);
@@ -208,10 +212,16 @@ class TimerSsmPost extends Command
 
     public function ossUpload()
     {
+        $count = SmmPost::getOssUploadingPostCount();
+        if ($count > 0) {
+            return false;
+        }
         // 发送社媒帖子
-        $disk = $this->disk('oss');
         $ossUploadPost = SmmPost::getOssUploadPost();
+        $disk = $this->disk('oss');
         foreach ($ossUploadPost as $post) {
+            $post->oss_upload = 1;//上传中
+            $post->save();
             //上传图片到oss
             $imageVideoUrl = explode(',', $post->image_video_url);
             foreach ($imageVideoUrl as $key => $url) {
@@ -220,10 +230,10 @@ class TimerSsmPost extends Command
                 $dir = dirname($url);
                 $disk->putFileAs($dir, $localPath, $filename);
             }
-            $post->oss_upload = 1;
+            $post->oss_upload = 2; //上传完成
             $post->save();
         }
+        return true;
     }
 
-
 }

+ 8 - 0
app/Distributor/Repositories/SmmPost.php

@@ -53,6 +53,14 @@ class SmmPost extends EloquentRepository
         return $model;
     }
 
+    public static function getOssUploadingPostCount()
+    {
+        $model = new Model();
+        $count = $model->where('oss_upload',1)->count();
+        return $count;
+    }
+
+
     public static function getPostById($id)
     {
         $model = new Model();

+ 3 - 2
app/Distributor/Repositories/SmmUserAccount.php

@@ -88,8 +88,9 @@ class SmmUserAccount extends EloquentRepository
     {
         $model = new Model();
         $youTube = $model->where('name', 'YouTube')->first();
-        $accounts = $model->where('parent_id','=',$youTube->id)->where('expires_at','>', Carbon::now())->orderBy('id', 'asc')->get();
-        //$accounts = $model->where('parent_id','=',$youTube->id)->orderBy('id', 'asc')->get();
+        $twitter = $model->where('name', 'Twitter')->first();
+        $parentIds = [$youTube->id,$twitter->id];
+        $accounts = $model->whereIn('parent_id',$parentIds)->where('expires_at','>', Carbon::now())->orderBy('id', 'asc')->get();
         return $accounts;
     }
 

+ 62 - 1
app/Services/Smm/TwitterService.php

@@ -99,10 +99,11 @@ class TwitterService implements SmmPlatformInterface
                     'status' => true,
                     'data' => [
                         'accessToken' => $auth['access_token'],
-                        'backupField1' => json_encode(['refresh_token'=>$refresh_token]),
+                        'backupField1' => '',
                         'userId' => $userId,
                         'userName' => $username,
                         'accessToken_expiresAt' => $expiresAt,
+                        'refresh_token' => $refresh_token,
                     ]
                 ];
             } else {
@@ -381,4 +382,64 @@ class TwitterService implements SmmPlatformInterface
             return ['status'=>false,'message' => "Upload failed: ".$e->getMessage()];
         }
     }
+
+    public function refreshAccessToken($refreshToken)
+    {
+        try {
+            $tokenUrl = 'https://api.twitter.com/2/oauth2/token';
+            $params = [
+                'refresh_token' => $refreshToken,
+                'grant_type' => 'refresh_token',
+                'client_id' => $this->clientId,
+            ];
+
+            $ch = curl_init();
+            curl_setopt($ch, CURLOPT_URL, $tokenUrl);
+            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+            curl_setopt($ch, CURLOPT_POST, true);
+            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
+            curl_setopt($ch, CURLOPT_HTTPHEADER, [
+                'Authorization: Basic ' . base64_encode($this->clientId . ':' . $this->clientSecret),
+                'Content-Type: application/x-www-form-urlencoded'
+            ]);
+
+            $response = curl_exec($ch);
+            $error = curl_error($ch);
+            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+            curl_close($ch);
+
+            if ($error) {
+                throw new \Exception("cURL Error: " . $error);
+            }
+
+            $authData = json_decode($response, true);
+            if (!$authData || isset($authData['error'])) {
+                throw new \Exception("Token refresh failed: " . ($authData['error_description'] ?? 'Unknown error'));
+            }
+
+            if (!isset($authData['access_token'])) {
+                throw new \Exception("Access token not found in response");
+            }
+
+            $newAccessToken = $authData['access_token'];
+            $newRefreshToken = $authData['refresh_token'] ?? $refreshToken; // 使用新的refresh_token,若未返回则保留原值
+            $expiresIn = $authData['expires_in'] ?? 0;
+            $expiresAt = Carbon::now()->addSeconds($expiresIn)->format('Y-m-d H:i:s');
+
+            return [
+                'status' => true,
+                'data' => [
+                    'access_token' => $newAccessToken,
+                    'refresh_token' => $newRefreshToken,
+                    'expires_at' => $expiresAt,
+                ],
+            ];
+        } catch (\Exception $e) {
+            Log::error("Twitter刷新Token失败: " . $e->getMessage());
+            return [
+                'status' => false,
+                'data' => $e->getMessage(),
+            ];
+        }
+    }
 }

+ 55 - 0
public/index.php

@@ -0,0 +1,55 @@
+<?php
+
+use Illuminate\Contracts\Http\Kernel;
+use Illuminate\Http\Request;
+
+define('LARAVEL_START', microtime(true));
+
+/*
+|--------------------------------------------------------------------------
+| Check If The Application Is Under Maintenance
+|--------------------------------------------------------------------------
+|
+| If the application is in maintenance / demo mode via the "down" command
+| we will load this file so that any pre-rendered content can be shown
+| instead of starting the framework, which could cause an exception.
+|
+*/
+
+if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {
+    require $maintenance;
+}
+
+/*
+|--------------------------------------------------------------------------
+| Register The Auto Loader
+|--------------------------------------------------------------------------
+|
+| Composer provides a convenient, automatically generated class loader for
+| this application. We just need to utilize it! We'll simply require it
+| into the script here so we don't need to manually load our classes.
+|
+*/
+
+require __DIR__.'/../vendor/autoload.php';
+
+/*
+|--------------------------------------------------------------------------
+| Run The Application
+|--------------------------------------------------------------------------
+|
+| Once we have the application, we can handle the incoming request using
+| the application's HTTP kernel. Then, we will send the response back
+| to this client's browser, allowing them to enjoy our application.
+|
+*/
+
+$app = require_once __DIR__.'/../bootstrap/app.php';
+
+$kernel = $app->make(Kernel::class);
+
+$response = $kernel->handle(
+    $request = Request::capture()
+)->send();
+
+$kernel->terminate($request, $response);