input('third_user_id'); // bps_third_user的id // // $googleOAuthService = new GoogleOAuthService(); // $hasThirdUser = $googleOAuthService->getCustomerList($thirdUserId); // if (!$hasThirdUser) { // return $this->errorResponse(300, 'Invalid state parameter'); // } // return $this->successResponse($hasThirdUser); // // } //调试用 public function getCustomerList(Request $request) { $merchantId = $request->input('merchant_id'); // bps_third_user的id $googleOAuthService = new GoogleOAuthService(); $hasThirdUser = $googleOAuthService->getCustomerListNew($merchantId); if (!$hasThirdUser) { return $this->errorResponse(300, 'Invalid state parameter'); } return $this->successResponse($hasThirdUser); } public function accessibleCustomers(Request $request) { $options = $request->all(); $options['refresh_token'] = $request->refresh_token; $options['login_customer_id'] = $request->login_customer_id; // 继续处理 Google Ads API 操作 return $this->listAccessibleCustomers($options); } public function accountHierarchy(Request $request) { $options = $request->all(); $options['refresh_token'] = $request->refresh_token; // $options['login_customer_id'] = $request->login_customer_id; $options['login_customer_id'] = $request->input('login_customer_id'); $options['manager_customer_id'] = $request->input('manager_customer_id') !== null ? $request->input('manager_customer_id') : null; // 继续处理 Google Ads API 操作 return $this->getAccountHierarchy($options); } //绑定接口-广告主体下层级账号 public function handleBinding(Request $request) { $options = $request->all(); $options['jwtClaims'] = $request->jwtClaims; // dump($options,22222222); // token即uuid 例如:f47ac10b-58cc-4372-a567-0e02b2c3d479 $redisKey = self::REDIS_KEY_PREFIX . $options['token']; // 尝试从 Redis 中获取缓存值 $refresh_token = Redis::get($redisKey); // dump($redisKey, $refresh_token, 444444); // return $this->successResponse($refresh_token); if ($refresh_token) { // $options['refresh_token'] = $refresh_token; // // 继续处理 Google Ads API 操作 // $googleAdsAccounts = $this->getAccountHierarchyForBind($options); // // dump($googleAdsAccounts); $googleOAuthService = new GoogleOAuthService(); // dump($options);return 0; $hasThirdUser = $googleOAuthService->getCustomerListNew($options['jwtClaims']['merchant_id']); dump($hasThirdUser,777); // if(!empty($hasThirdUser)) { // //$googleAdsAccounts和$hasThirdUser 合并 // foreach ($hasThirdUser as $key => $value) { // // } // // return $this->successResponse($hasThirdUser);} // } } else { return $this->errorResponse(300, 'token expired'); } } // 没调用 管理用户访问权限 public function accountAccess(Request $request) { $options = $request->all(); $options['refresh_token'] = $request->refresh_token; $options['login_customer_id'] = $request->login_customer_id; // 继续处理 Google Ads API 操作 return $this->getAccountAccess($options); } /** * 关联广告客户ID * @throws ApiException */ public function listAccessibleCustomers($options): Response { $resourceName = $this->googleAdsAccountService->runListAccessibleCustomers($options); return $this->successResponse(['links_resource_name' => $resourceName]); } /** * 广告主体下层级账号 * @throws ApiException */ public function getAccountHierarchy($options): Response { if (!isset($options['manager_customer_id'])) { $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("Error processing rootAccountId {$rootAccountId}: " . $e->getMessage()); continue; } } } else { $resourceName = $this->googleAdsAccountService->runListAccessibleCustomers($options); } return $this->successResponse(['links_resource_name' => $resourceName]); } /** * 绑定接口-广告主体下层级账号 * @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; } /** * 管理用户访问权限 * @throws ApiException */ public function getAccountAccess($options): Response { // dump($options); $resourceName = $this->googleAdsAccountService->runGetAccountAccess($options); return $this->successResponse(['links_resource_name' => $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 ]); } }