webman_ad/app/event/GoogleAdsCustomers.php
2025-01-08 11:31:48 +08:00

116 lines
3.5 KiB
PHP

<?php
namespace app\event;
use app\model\ThirdUser;
use app\service\GoogleAdsAssetService;
use app\service\GoogleOAuthService;
use app\service\GoogleAdsAccountService;
use Google\ApiCore\ApiException;
use support\Response;
use DI\Annotation\Inject;
//use QL\QueryList;
class GoogleAdsCustomers
{
/**
* @Inject
* @var GoogleOAuthService
*/
private $googleOAuthService;
/**
* @Inject
* @var GoogleAdsAccountService
*/
private $googleAdsAccountService;
const event = 'googleads:customers:event';
const add_queue = 'googleads:customers:add:queue';
public function addCustomers()
{
// 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['refresh_token'] = $thirdUser->access_token;
// dump($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;
}
}
$allRootAccounts = $this->googleAdsAccountService->runGetAccountHierarchy($option);
// dump($allRootAccounts);
foreach ($allRootAccounts as $rootAccountId => $accounts) {
foreach ($accounts as $account) {
// $customerId = $account['customer_id'];
// dump($customerId, $thirdUser->id, $rootAccountId, $account);
// if($account['customer_id'] == 1509096882)continue;
$this->googleOAuthService->saveThirdUserAdvertiser($account['customer_id'], $thirdUser->id, $rootAccountId, $account);
}
}
$this->googleOAuthService->updateThirdUserDefault($thirdUser->id, 't');
// return $this->successResponse($allAccounts);
}
/**
* get assets
* @throws ApiException
*/
public function getAccountHierarchy($options)
{
$resourceName = $this->googleAdsAccountService->runGetAccountHierarchy($options);
return $this->successResponse(['links_resource_name' => $resourceName]);
$customers = $this->googleOAuthService->getGoogleAdCustomers([]);
foreach ($customers as $customerId) {
$googleAdsAssetService = new GoogleAdsAssetService($customerId);
$resourceName = $googleAdsAssetService->runListAssets($customerId);
}
// return $this->successResponse(['ads_list' => $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
]);
}
}