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); // Add a counter to limit the number of executions $executionCount = 0; 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) { // Ensure we only execute 10 times if ($executionCount >= 10) { dump('Executed 10 times, stopping further calls.'); break 3; // Break out of both inner and outer loops } // dump($account); $this->googleOAuthService->saveThirdUserAdvertiser( $account['customer_id'], $thirdUser->id, $rootId, $account, $thirdUser->merchant_id ); // Increment the counter $executionCount++; } } // 绑定的授权主体全部账号更新后将状态从 '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); } //添加某个root广告账号的全部层级账号 public function addRootCustomersNew($options) { $refresh_token = $options['refresh_token']; $merchant_id = $options['merchant_id']; $googleAdsAccounts = $this->getAccountHierarchyForBind($options); // 初始化返回结果数组 $ad_accounts = []; //广告主体的profile信息 通过请求api返回 $googleAccountInfo = $this->googleOAuthService->getGoogleAccountInfo($refresh_token); foreach ($googleAdsAccounts as $login_customer_id => $accounts) { if (!empty($accounts[$login_customer_id])) { foreach ($accounts[$login_customer_id] as $account) { $data = [ 'account_id' => $account['account_id'], 'account_name' => $account['account_name'], 'currency' => $account['currency'], 'merchant_id' => $merchant_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']]), ]; $this->googleOAuthService->saveThirdUserAdvertiserNew($data, 0); } } } // dump($ad_accounts); // return $this->successResponse(['ad_accounts' => $ad_accounts, 'total_count' => count($ad_accounts)]); //绑定的授权主体全部账号更新后 将f改成t // $this->googleOAuthService->updateThirdUserDefault($thirdUser->id, 't'); // return $this->successResponse($allAccounts); } /** * 绑定接口-广告主体下层级账号 * @throws ApiException */ public function getAccountHierarchyForBind($options) { //TODO 过滤经理账号 2025-2-13 $resourceName = []; $listAccessibleCustomers = $this->googleAdsAccountService->runListAccessibleCustomers($options); foreach ($listAccessibleCustomers as $rootAccountId) { $options['manager_customer_id'] = $rootAccountId; //开发者 $options['login_customer_id'] = $rootAccountId; try { // 获取当前 rootAccountId 的所有账户 $allAccountsByRoot = $this->googleAdsAccountService->runGetAccountHierarchy($options); $resourceName[$rootAccountId] = $allAccountsByRoot; } catch (\Exception $e) { // 记录错误日志并跳过当前循环 Log::error("Binding Account List Error processing rootAccountId {$rootAccountId}: " . $e->getMessage()); continue; } } return $resourceName; } // 可以加入一些公共方法 protected function successResponse($data): Response { return Json([ 'code' => 0, 'msg' => 'ok', 'data' => $data, ]); } protected function errorResponse($code, $message, $data = []): Response { return Json([ 'code' => $code, 'msg' => $message ?: 'error', 'data' => $data ]); } }