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,11 +186,11 @@ class CustomerController
'account_id' => $account['account_id'], 'account_id' => $account['account_id'],
'name' => $account['account_name'], 'name' => $account['account_name'],
'currency' => $account['currency'], 'currency' => $account['currency'],
'bind' => $account['is_unbind'] == 't'? 0 : 1, 'bind' => $account['is_unbind'],
]; ];
} }
} else { } else {
return $this->successResponse(['ad_accounts' => [], 'total_count' => 0]); return $this->successResponse(['ad_accounts' => [], 'total_count' => 0]);
} }
// dump($ad_accounts); // dump($ad_accounts);
return $this->successResponse(['ad_accounts' => $ad_accounts, 'total_count' => count($ad_accounts)]); return $this->successResponse(['ad_accounts' => $ad_accounts, 'total_count' => count($ad_accounts)]);
@ -229,10 +229,11 @@ class CustomerController
// 遍历 bind_accounts保存或更新到 bps_ads_merchant_relation 表 // 遍历 bind_accounts保存或更新到 bps_ads_merchant_relation 表
foreach ($bindAccounts as $account) { foreach ($bindAccounts as $account) {
$data = [ $data = [
'merchant_id' => $options['jwtClaims']['merchant_id'], // 假设merchant_id从jwtClaims中获取
'user_id' => $options['jwtClaims']['uid'],
'account_id' => $account['account_id'], 'account_id' => $account['account_id'],
'account_name' => $account['name'], 'account_name' => $account['name'],
'currency' => $account['currency'], 'currency' => $account['currency'],
'merchant_id' => $options['jwtClaims']['merchant_id'], // 假设merchant_id从jwtClaims中获取
'access_token' => $refresh_token, 'access_token' => $refresh_token,
'refresh_token' => $refresh_token, 'refresh_token' => $refresh_token,
'is_unbind' => 'f' // 假设默认绑定状态为 'f' 'is_unbind' => 'f' // 假设默认绑定状态为 'f'
@ -241,7 +242,7 @@ class CustomerController
//ext_info是jsonb 存放 $account['login_customer_id']的键值对 //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' => $account['login_customer_id']]),
]; ];
$this->googleOAuthService->saveThirdUserAdvertiserNew($data,1); $this->googleOAuthService->saveThirdUserAdvertiserNew($data, 1); //授权回来先不初始化广告数据
// 查找是否已经存在该账号的绑定记录 // 查找是否已经存在该账号的绑定记录
// $existingRelation = \App\Models\AdsMerchantRelation::where('account_id', $account['account_id']) // $existingRelation = \App\Models\AdsMerchantRelation::where('account_id', $account['account_id'])
// ->where('merchant_id', $options['jwtClaims']['merchant_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['uid'];
$state = $request->input('state') ?? $request->jwtClaims['merchant_id']; $state = $request->input('state') ?? $request->jwtClaims['merchant_id'];
$merchantId = $request->jwtClaims['merchant_id']; $merchantId = $request->jwtClaims['merchant_id'];
$userId = $request->jwtClaims['uid'];
$code = $request->input('code'); // 授权码 $code = $request->input('code'); // 授权码
if (!$state) { if (!$state) {
@ -83,6 +84,13 @@ class OAuthController
$tokens = $googleOAuthService->getRefreshToken($code); $tokens = $googleOAuthService->getRefreshToken($code);
// $tokens = [
// 'access_token' => 'ya29.a0AXeO80QFiuV13I4ZCB4I8NQBVI2rnJsHlyBluSbbe0AUtqK3GDTeg50ktAXsqbG6YiY4paIqdlkTJgzzQZ0qZj1Nwz4wcx91XALOAtjJQC28xIruDc2ngt5a64IETQ_-ItbWdHW75tIUVcfKv7uaYYMSYs6juTJZ8zzLcy_2aCgYKAVgSARMSFQHGX2Mi2SLHpao46UhAg0Gy6LXLkg0175',
// 'refresh_token' => '1//0gZUuWDXHT45KCgYIARAAGBASNwF-L9IrRsKsk2dWR0jVvwji_pmDBtyvjbl1KCJ-btzLcfy4q4cRYvFebFmtqMDZAv1vdtbY470',
// 'id_token' => 'eyJhbGciOiJSUzI1NiIsImtpZCI6IjVkMTJhYjc4MmNiNjA5NjI4NWY2OWU0OGFlYTk5MDc5Ym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tokens['access_token'])) { if (isset($tokens['access_token'])) {
return $this->errorResponse(300, 'Google Ads Account is Authorized'); return $this->errorResponse(300, 'Google Ads Account is Authorized');
@ -90,7 +98,7 @@ class OAuthController
return $this->errorResponse(300, 'Refresh Token getting failed'); return $this->errorResponse(300, 'Refresh Token getting failed');
} else { } else {
// $googleOAuthService->saveRefreshToken($tokens['refresh_token'], $state); //作废2025-2-13 // $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
$uuid = $this->generateUuid(); $uuid = $this->generateUuid();
@ -103,7 +111,7 @@ class OAuthController
// // 触发事件 // // 触发事件
return $this->successResponse(['token' => $uuid], $request); 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) public function addRootCustomersNew($options)
{ {
$refresh_token = $options['refresh_token']; $refresh_token = $options['refresh_token'];
$access_token = $options['access_token'];
$merchant_id = $options['merchant_id']; $merchant_id = $options['merchant_id'];
$user_id = $options['user_id'];
//dump($refresh_token,$merchant_id,$access_token,9999999);
$googleAdsAccounts = $this->getAccountHierarchyForBind($options); $googleAdsAccounts = $this->getAccountHierarchyForBind($options);
// dump($googleAdsAccounts, 555);
// 初始化返回结果数组 // 初始化返回结果数组
$ad_accounts = []; $ad_accounts = [];
//广告主体的profile信息 通过请求api返回 //广告主体的profile信息 通过请求api返回
$googleAccountInfo = $this->googleOAuthService->getGoogleAccountInfo($refresh_token); $googleAccountInfo = $this->googleOAuthService->getGoogleAccountInfo($access_token);
// dump($googleAccountInfo,333);
foreach ($googleAdsAccounts as $login_customer_id => $accounts) { foreach ($googleAdsAccounts as $login_customer_id => $accounts) {
// dump($login_customer_id, $accounts,66);
if (!empty($accounts[$login_customer_id])) { if (!empty($accounts[$login_customer_id])) {
foreach ($accounts[$login_customer_id] as $account) { foreach ($accounts[$login_customer_id] as $account) {
$data = [ $data = [
@ -113,15 +119,17 @@ class GoogleAdsCustomers
'account_name' => $account['account_name'], 'account_name' => $account['account_name'],
'currency' => $account['currency'], 'currency' => $account['currency'],
'merchant_id' => $merchant_id, 'merchant_id' => $merchant_id,
'user_id' => $user_id,
'access_token' => $refresh_token, 'access_token' => $refresh_token,
'refresh_token' => $refresh_token, 'refresh_token' => $refresh_token,
'platform_user_id' => $googleAccountInfo['id'], 'platform_user_id' => $googleAccountInfo['id'],
'platform_user_name' => $googleAccountInfo['name'], 'platform_user_name' => $googleAccountInfo['name'],
'is_unbind' => 't', 'is_unbind' => 't',
//ext_info是jsonb 存放 $account['login_customer_id']的键值对 //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; continue;
} }
} }
// dump($resourceName,222);
return $resourceName; return $resourceName;
} }

View File

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