From 3ba0f6971a0c7f760783120454dbd51639e6fdfc Mon Sep 17 00:00:00 2001 From: huangguancheng Date: Tue, 14 Jan 2025 14:30:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0addRootCustomers=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E9=81=BF=E5=85=8D=E6=97=A0=E6=B3=95=E6=8B=89=E5=8F=96?= =?UTF-8?q?Google=E5=B9=BF=E5=91=8A=E8=B4=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/event/GoogleAdsCustomers.php | 76 +++++++++++++++++++++++++------- config/event.php | 4 +- 2 files changed, 61 insertions(+), 19 deletions(-) diff --git a/app/event/GoogleAdsCustomers.php b/app/event/GoogleAdsCustomers.php index d9cd363..b0f65df 100644 --- a/app/event/GoogleAdsCustomers.php +++ b/app/event/GoogleAdsCustomers.php @@ -8,6 +8,7 @@ use app\service\GoogleOAuthService; use app\service\GoogleAdsAccountService; use Google\ApiCore\ApiException; use support\Response; +use support\Log; use DI\Annotation\Inject; @@ -44,26 +45,23 @@ class GoogleAdsCustomers dump('not found third user'); return; } + $option = []; $option['refresh_token'] = $thirdUser->access_token; // dump($option); - $listAccessibleCustomers = $this->googleAdsAccountService->runListAccessibleCustomers($option); +// $listAccessibleCustomers = $this->googleAdsAccountService->runListAccessibleCustomers($option); // dump($listAccessibleCustomers); - if (getenv('GOOGLE_DEVELOP_TOKEN_LEVEL') === 'test') { - if (in_array(1509096882, $listAccessibleCustomers)) { -// $listAccessibleCustomers = [1509096882]; - $option['manager_customer_id'] = 1509096882; //开发者 - $option['login_customer_id'] = 1509096882; - } - if (in_array(1401879025, $listAccessibleCustomers)) { -// $listAccessibleCustomers = [1401879025]; - $option['manager_customer_id'] = 1401879025; //开发者 - $option['login_customer_id'] = 1401879025; - } - }else{ - //正式开发者令牌2个参数默认不赋值 -// $option['manager_customer_id'] = $listAccessibleCustomers[0]; //开发者 -// $option['login_customer_id'] = $listAccessibleCustomers[0]; - } +// if (getenv('GOOGLE_DEVELOP_TOKEN_LEVEL') === 'test') { +// if (in_array(1509096882, $listAccessibleCustomers)) { +// $option['manager_customer_id'] = 1509096882; //开发者 +// $option['login_customer_id'] = 1509096882; +// } +// if (in_array(1401879025, $listAccessibleCustomers)) { +// $option['manager_customer_id'] = 1401879025; //开发者 +// $option['login_customer_id'] = 1401879025; +// } +// }else{ + //正式开发者令牌2个参数默认不赋值 +// } $allRootAccounts = $this->googleAdsAccountService->runGetAccountHierarchy($option); // dump($allRootAccounts); foreach ($allRootAccounts as $rootAccountId => $accounts) { @@ -79,6 +77,50 @@ class GoogleAdsCustomers } + //添加某个root广告账号的全部层级账号 + public function addRootCustomers() + { +// dump($options); + $thirdUser = ThirdUser::where('is_default', 'f')->where('third_type', 'google') + ->find(); + if (!$thirdUser || !$thirdUser->access_token) { + dump('not found third user'); + return; + } + $option = []; + $option['refresh_token'] = $thirdUser->access_token; + $listAccessibleCustomers = $this->googleAdsAccountService->runListAccessibleCustomers($option); + foreach ($listAccessibleCustomers as $rootAccountId) { + $option['manager_customer_id'] = $rootAccountId; //开发者 + $option['login_customer_id'] = $rootAccountId; + try { + // 获取当前 rootAccountId 的所有账户 + $allAccountsByRoot = $this->googleAdsAccountService->runGetAccountHierarchy($option); + dump($allAccountsByRoot,333); + foreach ($allAccountsByRoot as $rootId => $accounts) { + foreach ($accounts as $account) { + $this->googleOAuthService->saveThirdUserAdvertiser( + $account['customer_id'], + $thirdUser->id, + $rootId, + $account + ); + } + } + // 绑定的授权主体全部账号更新后将状态从 'f' 更新为 't' + $this->googleOAuthService->updateThirdUserDefault($thirdUser->id, 't'); + } catch (\Exception $e) { + // 记录错误日志并跳过当前循环 + Log::error("Error processing rootAccountId {$rootAccountId}: " . $e->getMessage()); + continue; + } + //绑定的授权主体全部账号更新后 将f改成t + $this->googleOAuthService->updateThirdUserDefault($thirdUser->id, 't'); + } +// return $this->successResponse($allAccounts); + + } + /** * get assets diff --git a/config/event.php b/config/event.php index f100370..14af5a0 100644 --- a/config/event.php +++ b/config/event.php @@ -73,10 +73,10 @@ return [ // [GoogleAdsDateDatas::class, 'noticeDateDatas'], // ], GoogleAdsCustomers::event => [ - [GoogleAdsCustomers::class, 'addCustomers'], + [GoogleAdsCustomers::class, 'addRootCustomers'], ], GoogleAdsCustomers::add_queue => [ - [GoogleAdsCustomers::class, 'addCustomers'], + [GoogleAdsCustomers::class, 'addRootCustomers'], ],