Browse Source

社媒对facebook

moshaorui 5 days ago
parent
commit
cb2d576c0e

+ 9 - 2
.env.dev

@@ -1,11 +1,11 @@
 APP_NAME=Laravel
 #上线时需要改成prod
-APP_ENV=local
+APP_ENV=local.dev
 APP_KEY=base64:bdomFa+r6S774rRyFNeb4pCBxVe4WbuzAzk+LyP86+k=
 #上线时需要改成false
 APP_DEBUG=true
 #后台网站根域名,暂时未用到
-APP_URL=http://localhost
+APP_URL=https://localhost:8011
 #是否https
 ADMIN_HTTPS=false
 
@@ -90,3 +90,10 @@ MAIL_CC_ADDRESS=""
 ALBUM_RPC_URL=https://album-prime-control.mietubl.com.cn/rpc
 ALBUM_RPC_SECRET=MtbSecretVBUC
 ALBUM_OSS_URL=https://mietublcom.oss-cn-hongkong.aliyuncs.com
+
+#分销商后台地址 (社媒对接用到)
+DIST_SITE_URL=http://127.0.0.1:8011
+#facebook社媒对接配置
+SSM_FACEBOOK_APP_ID=1351316886010052
+SSM_FACEBOOK_APP_SECRET=2cb7b0669ee1da3107bf800b687c35d5
+

+ 14 - 5
app/Distributor/Forms/SmmAddAccount.php

@@ -6,8 +6,10 @@ use App\Distributor\Repositories\RpcAlbum;
 use App\Libraries\CommonHelper;
 use App\Models\DistProduct;
 use App\Models\DistProductImage;
+use App\Services\SmmService;
 use Dcat\Admin\Widgets\Form;
 
+
 class SmmAddAccount extends Form
 {
     public function handle(array $input)
@@ -16,11 +18,18 @@ class SmmAddAccount extends Form
         $mediaName = $input['media_name'];
         if ($mediaName) {
             //跳转到媒体受权页面
-            $url = CommonHelper::albumUrl($mediaName);
-            // 返回 JS 代码触发新窗口打开
-            return $this->response()->script(
-                "window.open('{$url}', '_blank')"
-            );
+            $ssmService = new SmmService($mediaName);
+            $result = $ssmService->login();
+            if ($result['status']) {
+                // 返回 JS 代码触发新窗口打开
+                $url = $result['data']['url'];
+                return $this->response()->script(
+                    "window.open('{$url}', '_blank')"
+                );
+            }
+            return $this
+                ->response()
+                ->error('获取授权失败,请检查媒体名称是否正确');
         } else {
             return $this
                 ->response()

+ 2 - 0
app/Libraries/RpcClient.php

@@ -25,6 +25,7 @@ class RpcClient
         $payload = $payload.$apiKey;
         $signature = hash_hmac('sha256', $payload, $apiSecret);
         $clientHost = env('ALBUM_RPC_URL');
+
         $httpClient = new HttpClient($clientHost);
         $httpClient = $httpClient->withTimeout(self::$timeout);
         $client = new Client($clientHost,false,$httpClient);
@@ -35,6 +36,7 @@ class RpcClient
             ]);
             return $result;
         } catch (\Exception $e) {
+            dd($e->getMessage());
             return ['status'=>false,'msg'=>$e->getMessage(),'data'=>[]];
         }
     }

+ 2 - 2
app/Services/Contracts/SmmPlatformInterface.php

@@ -51,7 +51,7 @@ interface SmmPlatformInterface
      *  'data' => '发布失败'
      * ]
      */
-    public function postImage($message,$imagePath,$accountIds);
+    public function postImage($message,$imagePath);
 
     /*
      * 发布视频帖子
@@ -64,7 +64,7 @@ interface SmmPlatformInterface
      *  'image_video_url' => 'https://example.com/video.mp4', //帖子图片URL
      * ]
      */
-    public function postVideo($message,$videoPath,$accountIds);
+    public function postVideo($message,$videoPath);
 
     /*
      * 获取帖评论列表

+ 89 - 2
app/Services/Smm/FacebookService.php

@@ -1,11 +1,25 @@
 <?php
 
 namespace App\Services\Smm;
-use App\Services\Smm\Contracts\SmmPlatformInterface;
+use App\Services\Contracts\SmmPlatformInterface;
 use Illuminate\Http\Request;
+use Facebook\Facebook;
+
 
 class FacebookService implements SmmPlatformInterface
 {
+    public $fb = null;
+    public $pageAccessToken = null;
+
+    public function __construct() {
+        $this->fb = new Facebook([
+            'app_id' => env('SSM_FACEBOOK_APP_ID'), // 替换为您的App ID
+            'app_secret' => env('SSM_FACEBOOK_APP_SECRET'), // 替换为您的App Secret
+            'default_graph_version' => 'v12.0', // 使用当前版本
+        ]);
+        //$this->pageAccessToken = $pageAccessToken;
+    }
+
     /*
      * OAuth 2.0 授权登录
      * 返回授权地址:https://example.com/fb-callback.php
@@ -13,7 +27,11 @@ class FacebookService implements SmmPlatformInterface
     public function login()
     {
         // 实现Facebook登录逻辑
-
+        $helper = $this->fb->getRedirectLoginHelper();
+        $permissions = ['publish_pages'];
+        $distSiteUrl = env('DIST_SITE_URL');
+        $loginUrl = $helper->getLoginUrl($distSiteUrl.'/callback/facebook', $permissions);
+        return ['status'=>true, 'data' => ['login_url'=>$loginUrl]];
     }
 
     /*
@@ -24,8 +42,77 @@ class FacebookService implements SmmPlatformInterface
     public function loginCallback(Request $request)
     {
         // 实现Facebook回调处理
+        $helper = $this->fb->getRedirectLoginHelper();
+        try {
+            $accessToken = $helper->getAccessToken();
+        } catch (Facebook\Exceptions\FacebookResponseException $e) {
+            return ['status' => false, 'data' => 'Graph API错误:' . $e->getMessage()];
+        } catch (Facebook\Exceptions\FacebookSDKException $e) {
+            return ['status' => false, 'data' => 'SDK错误:' . $e->getMessage()];
+        }
+        if (isset($accessToken)) {
+            // 可选:将短期令牌转换为长期令牌(有效期约60天)
+            $oAuth2Client = $this->fb->getOAuth2Client();
+            $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
+            return ['status' => true, 'data' => ['access_token' => $longLivedAccessToken]];
+        } else {
+            return ['status' => false, 'data' => '无法获取访问令牌'];
+        }
+    }
+
+    public function postImage($message,$imagePath) {
+        try {
+            $response = $this->fb->post(
+                '/me/photos',
+                [
+                    'source' => $this->fb->fileToUpload($imagePath), // 上传图片文件
+                    'message' => $message,                    // 添加描述
+                ],
+                $this->userAccessToken
+            );
+            $graphNode = $response->getGraphNode();
+            return ['status' => true, 'data' => ['id' => $graphNode['id']]];
+        } catch (Facebook\Exceptions\FacebookResponseException $e) {
+            $errorMsg  = 'Graph API错误:' . $e->getMessage();
+        } catch (Facebook\Exceptions\FacebookSDKException $e) {
+            $errorMsg = 'SDK错误:' . $e->getMessage();
+        }
+        return ['status'=>false,'data' => $errorMsg];
+    }
+
+
+    public function postVideo($message,$videoPath) {
+        try {
+            $response = $this->fb->post(
+                '/me/videos',
+                [
+                    'source' => $this->fb->fileToUpload($videoPath), // 上传视频文件
+                    'title' => $message,                        // 添加标题
+                    'description' => '',            // 添加描述
+                ],
+                $this->userAccessToken
+            );
+            $graphNode = $response->getGraphNode();
+            return ['status' => true, 'data' => ['id' => $graphNode['id']]];
+        } catch (Facebook\Exceptions\FacebookResponseException $e) {
+            $errorMsg = 'Graph API错误:' . $e->getMessage();
+        } catch (Facebook\Exceptions\FacebookSDKException $e) {
+            $errorMsg = 'SDK错误:' . $e->getMessage();
+        }
+        return ['status'=>false,'data' => $errorMsg];
     }
 
+    public function getComments($postId) {
+
+    }
+
+    public function replyToComment($commentId) {
+
+    }
+
+    public function deleteComment($commentId) {
+
+    }
 
 
 }

+ 2 - 1
app/Services/SmmService.php

@@ -2,7 +2,7 @@
 
 namespace App\Services;
 
-use App\Services\Smm\Contracts\SmmPlatformInterface;
+use App\Services\Contracts\SmmPlatformInterface;
 use InvalidArgumentException;
 
 class SmmService
@@ -18,6 +18,7 @@ class SmmService
     {
         $className = $this->getPlatformClassName($platform);
 
+
         if (!class_exists($className)) {
             throw new InvalidArgumentException("Platform [{$platform}] not supported");
         }

+ 1 - 0
composer.json

@@ -8,6 +8,7 @@
         "php": "^8.0.2",
         "alphasnow/aliyun-oss-laravel": "^4.7",
         "dcat-plus/laravel-admin": "^1.2",
+        "facebook/graph-sdk": "^5.1",
         "fguillot/json-rpc": "^1.3",
         "guzzlehttp/guzzle": "^7.2",
         "laravel/framework": "^9.19",

+ 67 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "d5c6237309a497437a44985e037d1c19",
+    "content-hash": "23de1a3cb4c1fc0b34eaccbfde64ac12",
     "packages": [
         {
             "name": "aliyuncs/oss-sdk-php",
@@ -1047,6 +1047,72 @@
             ],
             "time": "2023-10-06T06:47:41+00:00"
         },
+        {
+            "name": "facebook/graph-sdk",
+            "version": "5.1.4",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/facebook/php-graph-sdk.git",
+                "reference": "38fd7187a6704d3ab14ded2f3a534ac4ee6f3481"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/facebook/php-graph-sdk/zipball/38fd7187a6704d3ab14ded2f3a534ac4ee6f3481",
+                "reference": "38fd7187a6704d3ab14ded2f3a534ac4ee6f3481",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "ext-mbstring": "*",
+                "php": ">=5.4.0"
+            },
+            "require-dev": {
+                "guzzlehttp/guzzle": "~5.0",
+                "mockery/mockery": "~0.8",
+                "phpunit/phpunit": "~4.0"
+            },
+            "suggest": {
+                "guzzlehttp/guzzle": "Allows for implementation of the Guzzle HTTP client"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "5.x-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Facebook\\": "src/Facebook/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "Facebook Platform"
+            ],
+            "authors": [
+                {
+                    "name": "Facebook",
+                    "homepage": "https://github.com/facebook/facebook-php-sdk-v4/contributors"
+                }
+            ],
+            "description": "Facebook SDK for PHP",
+            "homepage": "https://github.com/facebook/facebook-php-sdk-v4",
+            "keywords": [
+                "facebook",
+                "sdk"
+            ],
+            "support": {
+                "issues": "https://github.com/facebook/php-graph-sdk/issues",
+                "source": "https://github.com/facebook/php-graph-sdk/tree/5.1.4"
+            },
+            "abandoned": true,
+            "time": "2016-05-13T17:28:30+00:00"
+        },
         {
             "name": "fguillot/json-rpc",
             "version": "v1.3.0",