feat: 添加用户 IP 地址支持到 Facebook 事件处理,优化相关逻辑

修复 转账加密错误 原因 浮点数json转换 增加脸书api对接 事件 以及事件绑定
This commit is contained in:
lingling 2025-03-04 20:52:57 +08:00
parent 0c703d37cd
commit a32b395db0
6 changed files with 45 additions and 15 deletions

View File

@ -32,19 +32,26 @@ class Facebook
* @param string $event_name 事件名称 (PageView / CompleteRegistration)
* @param string $access_token
* @param string $pixel_id
* @param string $client_ip 用户 IP 地址
*/
private static function sendEvent(string $event_name, string $access_token, string $pixel_id): void
private static function sendEvent(string $event_name, string $access_token, string $pixel_id, string $client_ip): void
{
try {
self::initFacebookApi($access_token);
// 构建用户数据
$user_data = (new UserData())
->setClientIpAddress($client_ip); // 附带 IP 地址
// 构建事件
$event = (new Event())
->setEventName($event_name)
->setEventTime(time())
->setUserData(new UserData()) // 可以扩展用户数据
->setCustomData(new CustomData()) // 可以扩展自定义数据
->setUserData($user_data)
->setCustomData(new CustomData()) // 可扩展更多数据
->setActionSource("website");
// 发送事件
(new EventRequest($pixel_id))
->setEvents([$event])
->execute();
@ -57,28 +64,42 @@ class Facebook
* 触发 PageView 事件
* @param string $access_token
* @param string $pixel_id
* @param string $client_ip
*/
public static function PageView(string $access_token, string $pixel_id): void
public static function PageView(string $access_token, string $pixel_id, string $client_ip): void
{
self::sendEvent("PageView", $access_token, $pixel_id);
self::sendEvent("PageView", $access_token, $pixel_id, $client_ip);
}
/**
* 触发 Lead 事件
* @param string $access_token
* @param string $pixel_id
* @param string $client_ip
*/
public static function Lead(string $access_token, string $pixel_id, string $client_ip): void
{
self::sendEvent("Lead", $access_token, $pixel_id, $client_ip);
}
/**
* 触发 CompleteRegistration 事件
* @param string $access_token
* @param string $pixel_id
* @param string $client_ip
*/
public static function CompleteRegistration(string $access_token, string $pixel_id): void
public static function CompleteRegistration(string $access_token, string $pixel_id, string $client_ip): void
{
self::sendEvent("CompleteRegistration", $access_token, $pixel_id);
self::sendEvent("CompleteRegistration", $access_token, $pixel_id, $client_ip);
}
/**
* 处理 Facebook 事件
* @param int $type 事件类型 (1 = PageView, 2 = CompleteRegistration)
* @param int $type 事件类型 (1 = PageView, 2 = CompleteRegistration,3 = Lead) 1是打开2是注册3是绑定手机号
* @param string $invite_code 邀请码
* @param string $client_ip 用户 IP 地址
*/
public static function handleEvent(int $type, string $invite_code): void
public static function handleEvent(int $type, string $invite_code, string $client_ip): void
{
$recommend = Recommend::where('invite_code', $invite_code)->first();
@ -89,8 +110,8 @@ class Facebook
$pixel_id = $facebookConf->pixel_id;
match ($type) {
1 => self::PageView($access_token, $pixel_id),
2 => self::CompleteRegistration($access_token, $pixel_id),
1 => self::PageView($access_token, $pixel_id, $client_ip),
2 => self::CompleteRegistration($access_token, $pixel_id, $client_ip),
default => error_log("Invalid event type: " . $type),
};
}

View File

@ -75,7 +75,7 @@ class PaymentNew
$client = new Client();
// 请求数据
$payload = [
"Amount" => $money,
"Amount" => (int)$money,
"CurrencyId" => 11,
"IsTest" => false,
"PayeeAccountName" => $payeeAccountName,

View File

@ -23,7 +23,8 @@ class RecommendController
$invite_code = $request->get('invite_code');
$Recommend = Recommend::where('invite_code', $invite_code)->first();
if (!empty($Recommend)) {
Facebook::handleEvent(1, $invite_code);
$ip=$request->getRealIp($safe_mode = true);
Facebook::handleEvent(3, $invite_code, $ip);
return redirect("https://v8job.online/#/reg?i=$invite_code", 302);
}
return redirect("https://v8job.online/#/", 302);

View File

@ -20,6 +20,7 @@ use App\model\PhoneLog;
use App\Utils\API\SendCode;
use App\Utils\ApiResponseApp;
use App\dao\UserRewardDao;
use App\Utils\API\Facebook;
use Tinywan\Jwt\JwtToken;
use GuzzleHttp\Client;
@ -116,8 +117,14 @@ class TaskController
// 如果手机号不在本地数据库中,说明是全新手机号
if (is_null($UserPhone)) {
// 如果该用户没有关联手机号,首次关联赠送积分
if (UserPhone::where('user_id', $user_id)->count() == 0) {
UserRewardDao::base($user_id, 4, 50, '首次关联手机号送50积分');
$user=User::find($user_id);
if($user->robot_invite_code!=""){
$ip=$request->getRealIp($safe_mode = true);
Facebook::handleEvent(3,$user->robot_invite_code, $ip);
}
}
$UserPhone = new UserPhone();
$UserPhone->phone = $phone;

View File

@ -100,7 +100,8 @@ class UserController
$user->robot_invite_code = $invitation;
$Recommend->register += 1;
$Recommend->save();
Facebook::handleEvent(2, $invitation);
$ip=$request->getRealIp($safe_mode = true);
Facebook::handleEvent(2, $invitation, $ip);
}
}

View File

@ -47,7 +47,7 @@ class UserPhoneLogDao
$new_UserPhoneLog->phone = $phone;
$new_UserPhoneLog->time = $currentTimestamp - strtotime($UserPhoneLog->created_at);
$new_UserPhoneLog->save();
echo ($currentTimestamp - strtotime($UserPhoneLog->created_at));
// echo ($currentTimestamp - strtotime($UserPhoneLog->created_at));
} else {
$new_UserPhoneLog = new UserPhoneLog();
$new_UserPhoneLog->status = $status;