feat: 添加 Facebook SDK 支持,优化相关逻辑和注释

This commit is contained in:
lingling 2025-03-03 20:08:29 +08:00
parent 621cbcd48d
commit 0c703d37cd
13 changed files with 197 additions and 18 deletions

View File

@ -0,0 +1,99 @@
<?php
namespace App\Utils\API;
use app\model\Facebookconf;
use app\model\Recommend;
use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
use FacebookAds\Object\ServerSide\CustomData;
use FacebookAds\Object\ServerSide\Event;
use FacebookAds\Object\ServerSide\EventRequest;
use FacebookAds\Object\ServerSide\UserData;
use Exception;
/**
* Facebook API 事件处理
*/
class Facebook
{
/**
* 初始化 Facebook API
* @param string $access_token
*/
private static function initFacebookApi(string $access_token): void
{
Api::init(null, null, $access_token);
Api::instance()->setLogger(new CurlLogger());
}
/**
* 发送 Facebook 事件
* @param string $event_name 事件名称 (PageView / CompleteRegistration)
* @param string $access_token
* @param string $pixel_id
*/
private static function sendEvent(string $event_name, string $access_token, string $pixel_id): void
{
try {
self::initFacebookApi($access_token);
$event = (new Event())
->setEventName($event_name)
->setEventTime(time())
->setUserData(new UserData()) // 可以扩展用户数据
->setCustomData(new CustomData()) // 可以扩展自定义数据
->setActionSource("website");
(new EventRequest($pixel_id))
->setEvents([$event])
->execute();
} catch (Exception $e) {
error_log("Facebook API Error: " . $e->getMessage());
}
}
/**
* 触发 PageView 事件
* @param string $access_token
* @param string $pixel_id
*/
public static function PageView(string $access_token, string $pixel_id): void
{
self::sendEvent("PageView", $access_token, $pixel_id);
}
/**
* 触发 CompleteRegistration 事件
* @param string $access_token
* @param string $pixel_id
*/
public static function CompleteRegistration(string $access_token, string $pixel_id): void
{
self::sendEvent("CompleteRegistration", $access_token, $pixel_id);
}
/**
* 处理 Facebook 事件
* @param int $type 事件类型 (1 = PageView, 2 = CompleteRegistration)
* @param string $invite_code 邀请码
*/
public static function handleEvent(int $type, string $invite_code): void
{
$recommend = Recommend::where('invite_code', $invite_code)->first();
if ($recommend) {
$facebookConf = Facebookconf::where('promotionid', $recommend->id)->first();
if ($facebookConf) {
$access_token = $facebookConf->access_token;
$pixel_id = $facebookConf->pixel_id;
match ($type) {
1 => self::PageView($access_token, $pixel_id),
2 => self::CompleteRegistration($access_token, $pixel_id),
default => error_log("Invalid event type: " . $type),
};
}
}
}
}

View File

@ -65,13 +65,13 @@ class Payment
];
$encrypt_value = self::generate_encrypt_value($payload, $secret_key);
$payload["EncryptValue"] = $encrypt_value;
var_dump(111111111111111);
var_dump($payload);
// var_dump(111111111111111);
// var_dump($payload);
$response = $client->post($url, [
'json' => $payload, // 以 JSON 格式发送数据
'headers' => $headers
]);
var_dump($response);
// var_dump($response);
// $json_payload = json_encode($payload);
// $ch = curl_init();

View File

@ -49,7 +49,7 @@ class PaymentNew
// 5. 计算 sha256 哈希
$hash_value = hash('sha256', $param_str);
Log::warning("param_str:" . $param_str." hash_value:".$hash_value);
// 6. 转换为大写返回
return strtoupper($hash_value);
}
@ -81,7 +81,6 @@ class PaymentNew
"PayeeAccountName" => $payeeAccountName,
"PayeeAccountNumber" => $payeeAccountNumber,
"PayeeBankName" => $payeeBankName,
"PayeeIFSCCode" => "",
"PaymentChannelId" => $PaymentChannelId,
"ShopInformUrl" => "http://38.54.94.131/api/withdraw/callback",
"ShopOrderId" => $orderId . "",

View File

@ -2,6 +2,7 @@
namespace app\controller;
use App\Utils\API\Fackbook;
use App\Utils\API\PaymentNew;
use support\Request;
@ -13,7 +14,6 @@ class IndexController
public function index(Request $request)
{
return response()->file(public_path() . '/index.html');
}

View File

@ -3,6 +3,7 @@
namespace app\controller;
use app\model\Recommend;
use App\Utils\API\Facebook;
use App\Utils\API\PaymentNew;
use support\Request;
@ -13,7 +14,7 @@ class RecommendController
{
protected $noNeedLogin = ['index'];
/**
* @Apidoc\Title("1.0 获取vip等级赠送积分")
* @Apidoc\Title("1.0 推广链接")
* @Apidoc\Url("Recommend/index")
* @Apidoc\Method("POST")
*/
@ -22,10 +23,9 @@ class RecommendController
$invite_code = $request->get('invite_code');
$Recommend = Recommend::where('invite_code', $invite_code)->first();
if (!empty($Recommend)) {
$Recommend->open += 1;
$Recommend->save();
return redirect("http://127.0.0.1:8787/#/reg?i=$invite_code", 302);
Facebook::handleEvent(1, $invite_code);
return redirect("https://v8job.online/#/reg?i=$invite_code", 302);
}
return 0;
return redirect("https://v8job.online/#/", 302);
}
}

View File

@ -9,6 +9,7 @@ use hg\apidoc\annotation as Apidoc;
use Intervention\Image\ImageManagerStatic as Image;
use Exception;
use support\exception\BusinessException;
use support\Log;
use Tinywan\Jwt\JwtToken;
/**
@ -57,7 +58,7 @@ class AccountController
];
// 如果密码正确,生成 JWT 令牌
$token = JwtToken::generateToken($user);
Log::info('生成的token' . json_encode($token));
// 返回成功响应和用户信息(可以将 token 添加到响应中)
return ApiResponse::success(200, [
'user' => $user, // 返回用户信息

View File

@ -84,7 +84,7 @@ class CommonController
// 执行查询并获取结果
$list = $query->get();
var_dump(1111);
// var_dump(1111);
// 准备返回的数据
$map = [

View File

@ -18,6 +18,7 @@ use app\model\Recommend;
use app\model\Reward;
use app\model\UserPhone;
use app\model\UserReward;
use App\Utils\API\Facebook;
use hg\apidoc\annotation as Apidoc;
/**
@ -99,6 +100,7 @@ class UserController
$user->robot_invite_code = $invitation;
$Recommend->register += 1;
$Recommend->save();
Facebook::handleEvent(2, $invitation);
}
}

View File

@ -79,7 +79,7 @@ class JwtAuthMiddleware implements MiddlewareInterface
// }
// var_dump($decoded);
} catch (\Exception $e) {
var_dump($e);
// var_dump($e);
// 解码失败,返回无效令牌错误
// return ApiResponse::error(401, ['error' => '无效的令牌'], '无效的令牌');
return response('',401,['error' => '无效的令牌']);

View File

@ -0,0 +1,25 @@
<?php
namespace app\model;
use support\Model;
class Facebookconf extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'facebookconf';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
}

View File

@ -37,7 +37,8 @@
"simplehtmldom/simplehtmldom": "^2.0@RC",
"guzzlehttp/guzzle": "^7.9",
"workerman/crontab": "^1.0",
"symfony/translation": "^6.0"
"symfony/translation": "^6.0",
"facebook/php-business-sdk": "^19.0"
},
"suggest": {
"ext-event": "For better performance. "

54
composer.lock generated
View File

@ -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": "6e60ac96c1f817e5e663943291142785",
"content-hash": "91c873c195356de89b6467f9af943e26",
"packages": [
{
"name": "cakephp/core",
@ -650,6 +650,58 @@
],
"time": "2024-02-05T11:35:39+00:00"
},
{
"name": "facebook/php-business-sdk",
"version": "19.0.1",
"source": {
"type": "git",
"url": "https://github.com/facebook/facebook-php-business-sdk.git",
"reference": "67c3ba5dfde6ca0a5a4bbd53eaa149cee158983a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/facebook/facebook-php-business-sdk/zipball/67c3ba5dfde6ca0a5a4bbd53eaa149cee158983a",
"reference": "67c3ba5dfde6ca0a5a4bbd53eaa149cee158983a",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"guzzlehttp/guzzle": "^6.5 || ^7.0",
"php": ">=8.0"
},
"require-dev": {
"mockery/mockery": "1.3.3",
"phpunit/phpunit": "~9",
"symfony/finder": "~2.6"
},
"type": "library",
"autoload": {
"psr-4": {
"FacebookAds\\": "src/FacebookAds/"
}
},
"notification-url": "https://packagist.org/downloads/",
"description": "PHP SDK for Facebook Business",
"homepage": "https://developers.facebook.com/",
"keywords": [
"ads",
"business",
"facebook",
"instagram",
"page",
"sdk"
],
"support": {
"issues": "https://github.com/facebook/facebook-php-business-sdk/issues",
"source": "https://github.com/facebook/facebook-php-business-sdk/tree/19.0.1"
},
"time": "2024-03-07T21:30:58+00:00"
},
{
"name": "firebase/php-jwt",
"version": "v6.4.0",

View File

@ -50,7 +50,7 @@ class Task
$GetLodeLog = GetLodeLog::where('status', 0)->get();
foreach ($GetLodeLog as $key => $value) {
$res = SendCode::get_code($value->phone);
var_dump($res);
// var_dump($res);
if ($res['status'] == 1 && $res['scanCode'] != null) {
$value->status = 1;
$value->code = $res['scanCode'];