webman_ad/app/controller/CustomerController.php
2025-02-15 17:21:00 +08:00

238 lines
7.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\controller;
use app\service\GoogleOAuthService;
use app\service\GoogleAdsAccountService;
use Google\ApiCore\ApiException;
use support\Log;
use support\Request;
use support\Response;
use support\Redis;
use DI\Annotation\Inject;
//use app\model\ThirdUserAdvertiser;
class CustomerController
{
// 育才定义 绑定广告账号时的该uuid的广告token Redis 键名前缀
const REDIS_KEY_PREFIX = 'bps-goui:third:token:key:';
/**
* @Inject
* @var GoogleOAuthService
*/
private $googleOAuthService;
/**
* @Inject
* @var GoogleAdsAccountService
*/
private $googleAdsAccountService;
/**
* 作废2025-2-13
*
*/
// public function getCustomerList(Request $request)
// {
// $thirdUserId = $request->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
]);
}
}