googleAds广告账号-授权-绑定-激活流程更新 2

This commit is contained in:
huangguancheng 2025-02-17 21:24:14 +08:00
parent ca457d5539
commit 907be771ab
4 changed files with 59 additions and 31 deletions

View File

@ -186,7 +186,7 @@ class CustomerController
'account_id' => $account['account_id'],
'name' => $account['account_name'],
'currency' => $account['currency'],
'bind' => $account['is_unbind'] == 't'? 0 : 1,
'bind' => $account['is_unbind'],
];
}
} else {
@ -229,10 +229,11 @@ class CustomerController
// 遍历 bind_accounts保存或更新到 bps_ads_merchant_relation 表
foreach ($bindAccounts as $account) {
$data = [
'merchant_id' => $options['jwtClaims']['merchant_id'], // 假设merchant_id从jwtClaims中获取
'user_id' => $options['jwtClaims']['uid'],
'account_id' => $account['account_id'],
'account_name' => $account['name'],
'currency' => $account['currency'],
'merchant_id' => $options['jwtClaims']['merchant_id'], // 假设merchant_id从jwtClaims中获取
'access_token' => $refresh_token,
'refresh_token' => $refresh_token,
'is_unbind' => 'f' // 假设默认绑定状态为 'f'
@ -241,7 +242,7 @@ class CustomerController
//ext_info是jsonb 存放 $account['login_customer_id']的键值对
// 'ext_info' => json_encode(['login_customer_id' => $account['login_customer_id']]),
];
$this->googleOAuthService->saveThirdUserAdvertiserNew($data,1);
$this->googleOAuthService->saveThirdUserAdvertiserNew($data, 1); //授权回来先不初始化广告数据
// 查找是否已经存在该账号的绑定记录
// $existingRelation = \App\Models\AdsMerchantRelation::where('account_id', $account['account_id'])
// ->where('merchant_id', $options['jwtClaims']['merchant_id'])

View File

@ -73,6 +73,7 @@ class OAuthController
// $state = $request->input('state') ?? $request->jwtClaims['uid'];
$state = $request->input('state') ?? $request->jwtClaims['merchant_id'];
$merchantId = $request->jwtClaims['merchant_id'];
$userId = $request->jwtClaims['uid'];
$code = $request->input('code'); // 授权码
if (!$state) {
@ -83,6 +84,13 @@ class OAuthController
$tokens = $googleOAuthService->getRefreshToken($code);
// $tokens = [
// 'access_token' => 'ya29.a0AXeO80QFiuV13I4ZCB4I8NQBVI2rnJsHlyBluS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65PcGnK3X5nnRKzshcHgwGT5RCC9lcCnXmAaXMjjQWOtXOFcrRMCPG-HM3SmaCsrC-ThzvUoCzXl6ArQkZxpS3JA5b-KdmWK8KxnK9Lr63G9xvbr56ys_RIPXNyNuzlmY1Xzc3Pr920fJl43haJDp3HOx9DlRYQctC53mw6t6DoqfhCKItZ7jON65JWG4CJhqxZiFJNW7RaKAKzUncVoen_D1SphLlOMda0m6YBgj8UDcyo5U0X7dZM7f_oMMjHkuS252XG9mJ2QH74K-7tlO0GPUEWuUkprvFSay5LU5TzJIJCM9EkoD4hvyYB24oCHgqUiHA',
//
// ];
if (!isset($tokens['refresh_token'])) {
if (isset($tokens['access_token'])) {
return $this->errorResponse(300, 'Google Ads Account is Authorized');
@ -90,7 +98,7 @@ class OAuthController
return $this->errorResponse(300, 'Refresh Token getting failed');
} else {
// $googleOAuthService->saveRefreshToken($tokens['refresh_token'], $state); //作废2025-2-13
$googleOAuthService->saveRefreshTokenNew($tokens['refresh_token'], $merchantId);
$googleOAuthService->saveRefreshTokenNew($tokens['refresh_token'],$tokens['access_token'], $merchantId,$userId);
// 生成 UUID
$uuid = $this->generateUuid();
@ -103,7 +111,7 @@ class OAuthController
// // 触发事件
return $this->successResponse(['token' => $uuid], $request);
}
return $this->successResponse($tokens, $request);
// return $this->successResponse($tokens, $request);
}

View File

@ -96,16 +96,22 @@ class GoogleAdsCustomers
public function addRootCustomersNew($options)
{
$refresh_token = $options['refresh_token'];
$access_token = $options['access_token'];
$merchant_id = $options['merchant_id'];
$user_id = $options['user_id'];
//dump($refresh_token,$merchant_id,$access_token,9999999);
$googleAdsAccounts = $this->getAccountHierarchyForBind($options);
// dump($googleAdsAccounts, 555);
// 初始化返回结果数组
$ad_accounts = [];
//广告主体的profile信息 通过请求api返回
$googleAccountInfo = $this->googleOAuthService->getGoogleAccountInfo($refresh_token);
$googleAccountInfo = $this->googleOAuthService->getGoogleAccountInfo($access_token);
// dump($googleAccountInfo,333);
foreach ($googleAdsAccounts as $login_customer_id => $accounts) {
// dump($login_customer_id, $accounts,66);
if (!empty($accounts[$login_customer_id])) {
foreach ($accounts[$login_customer_id] as $account) {
$data = [
@ -113,15 +119,17 @@ class GoogleAdsCustomers
'account_name' => $account['account_name'],
'currency' => $account['currency'],
'merchant_id' => $merchant_id,
'user_id' => $user_id,
'access_token' => $refresh_token,
'refresh_token' => $refresh_token,
'platform_user_id' => $googleAccountInfo['id'],
'platform_user_name' => $googleAccountInfo['name'],
'is_unbind' => 't',
//ext_info是jsonb 存放 $account['login_customer_id']的键值对
'ext_info' => json_encode(['login_customer_id' => $account['login_customer_id']]),
'ext_info' => json_encode(['login_customer_id' => $login_customer_id]),
];
$this->googleOAuthService->saveThirdUserAdvertiserNew($data, 0);
// dump($data);
$this->googleOAuthService->saveThirdUserAdvertiserNew($data, 1);
}
}
}
@ -155,6 +163,7 @@ class GoogleAdsCustomers
continue;
}
}
// dump($resourceName,222);
return $resourceName;
}

View File

@ -29,26 +29,26 @@ class GoogleOAuthService
// 'family_name' => '用户的姓氏',
// 'picture' => '用户头像的URL',
// 'locale' => '用户的语言偏好',
$client = new Client();
try {
// 调用Google的userinfo API
$response = $client->get('https://www.googleapis.com/oauth2/v2/userinfo', [
'headers' => [
$options = [
"headers" => [
'Authorization' => 'Bearer ' . $accessToken,
],
]);
// 解析API返回的JSON数据
$userInfo = json_decode($response->getBody(), true);
// 返回用户信息
];
$userInfo = json_decode($client->request('Get', 'https://www.googleapis.com/oauth2/v2/userinfo', $options)->getBody()->getContents(), true);
// // Process user info...
// dump($userInfo,666);
return $userInfo;
} catch (\GuzzleHttp\Exception\ClientException $e) {
error_log('Client error: ' . $e->getMessage());
if ($e->hasResponse()) {
error_log('Response: ' . $e->getResponse()->getBody());
}
} catch (\Exception $e) {
// 处理异常情况
throw new \Exception("Failed to fetch Google account info: " . $e->getMessage());
// Handle other exceptions
error_log('Error: ' . $e->getMessage());
}
}
@ -129,7 +129,7 @@ class GoogleOAuthService
// ->where('bamr.is_unbind', 'f')
->where('bamr.is_del', 'f')
->where('bamr.platform', 2)
->field('bamr.account_id, bamr.account_name,bamr.currency')
->field('bamr.account_id, bamr.account_name,bamr.is_unbind,bamr.currency')
// ->field('')
->select(); // 执行查询
// dump($customers,555);
@ -193,6 +193,7 @@ class GoogleOAuthService
// 确保 account_id 和 merchant_id 是字符串
$data['account_id'] = (string)$data['account_id'];
$data['merchant_id'] = (string)$data['merchant_id'];
$data['user_id'] = (string)$data['user_id'];
$tableName = 'bps_ads_merchant_relation';
$tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'bps.' . $tableName;
@ -201,6 +202,7 @@ class GoogleOAuthService
$insertData = [
'platform' => 2, // 假设google平台值为2根据实际情况调整
'merchant_id' => $data['merchant_id'],
'user_id' => $data['user_id'],
'account_id' => $data['account_id'],
'access_token' => $data['access_token'],
'refresh_token' => $data['refresh_token'],
@ -211,14 +213,15 @@ class GoogleOAuthService
'is_unbind' => $data['is_unbind']??'f',
'ext_info' => $data['ext_info']??'{}',
];
// dump($insertData,2525);
// 构造SQL语句
$sql = "
INSERT INTO {$tableName}
(platform, merchant_id, access_token, refresh_token,account_id, account_name, currency, ext_info,platform_user_id, platform_user_name)
VALUES (:platform, :merchant_id, :access_token, :refresh_token, :account_id, :account_name, :currency, :ext_info,:platform_user_id, :platform_user_name)
ON CONFLICT (account_id, merchant_id)
(platform, merchant_id, user_id,access_token, refresh_token,account_id, account_name, currency, ext_info,platform_user_id, platform_user_name,is_unbind)
VALUES (:platform, :merchant_id,:user_id, :access_token, :refresh_token, :account_id, :account_name, :currency, :ext_info,:platform_user_id, :platform_user_name,:is_unbind)
ON CONFLICT (platform,merchant_id,account_id)
DO UPDATE SET
user_id = EXCLUDED.user_id,
account_name = EXCLUDED.account_name,
access_token = EXCLUDED.access_token,
refresh_token = EXCLUDED.refresh_token,
@ -228,6 +231,7 @@ class GoogleOAuthService
// 执行SQL语句
ThinkDb::execute($sql, $insertData);
// dump(ThinkDb::getLastSql(),2525);
//如果要异步更新改广告账号数据的话。
if($sendMq == 1){
@ -235,6 +239,8 @@ class GoogleOAuthService
$options = [];
$options['merchant_id'] = $data['merchant_id'];
$options['account_id'] = $data['account_id'];
// $options['refresh_token'] = $data['refresh_token'];
// $options['login_customer_id'] = json_decode($data['ext_info'],true)['login_customer_id']??'';
$this->queueNew($options);
}
}
@ -309,18 +315,22 @@ class GoogleOAuthService
$this->queue($options);
}
public function saveRefreshTokenNew($refreshToken, $merchantId)
public function saveRefreshTokenNew($refreshToken,$accessToken, $merchantId,$userId)
{
$data = [
'refresh_token' => $refreshToken,
'access_token' => $accessToken,
'merchant_id' => $merchantId,
'user_id' => $userId,
// 'is_default' => 'f',
// 'third_type' => 'google',
];
$options = [];
$options['refresh_token'] = $refreshToken;
// dump($data,$options);
Event::emit(GoogleAdsCustomers::add_queue, $data);
$this->queue($options);
// $this->queue($options);
}
public function initNewGoogleAdsAccountData($refreshToken)