采集事件更新

This commit is contained in:
hgc 2024-12-24 09:51:32 +08:00
parent 27e62af58c
commit a37d7ae901
12 changed files with 297 additions and 208 deletions

View File

@ -86,6 +86,7 @@ class AdController
// 调用 Service 层查询
$result = $this->googleAdsReportService::getCampaignList($page, $pageSize, $keyword, $dateRange, $startDate, $endDate);
return $this->successResponse($result);
// return $this->errorResponse(300,'授权失败');
}
public function exportAdsToExcel(Request $request)

View File

@ -88,6 +88,10 @@ class OAuthController
public function testRefreshToken(Request $request)
{
$customers = $this->googleOAuthService->getGoogleAdCustomers([]);
return $this->successResponse(['data' => $customers]);
// $list = ThinkDb::table('bps.bps_third_user')->where('third_type', 'google')->select();
// return $this->successResponse($list);

View File

@ -3,6 +3,7 @@
namespace app\event;
use app\service\GoogleAdsAdService;
use app\service\GoogleOAuthService;
use Google\ApiCore\ApiException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
@ -24,6 +25,12 @@ class GoogleAdsAds
private $googleAdsAdService;
/**
* @Inject
* @var GoogleOAuthService
*/
private $googleOAuthService;
//微博热榜地址
@ -40,8 +47,6 @@ class GoogleAdsAds
const countries = ["GB", "BE"];
public function listAds(Request $request)
{
$options = $request->all();
@ -54,11 +59,14 @@ class GoogleAdsAds
* get groups
* @throws ApiException
*/
public function getAds($options): Response
public function getAds($options)
{
$resourceName = $this->googleAdsAdService->runListAds($options['customer_id']);
$customers = $this->googleOAuthService->getGoogleAdCustomers([]);
foreach ($customers as $customerId) {
$resourceName = $this->googleAdsAdService->runListAds($customerId);
}
return $this->successResponse(['ads_list' => $resourceName]);
// return $this->successResponse(['ads_list' => $resourceName]);
}
/**

View File

@ -4,6 +4,7 @@ namespace app\event;
use app\model\Campaign;
use app\service\GoogleAdsCampaignService;
use app\service\GoogleOAuthService;
use Google\ApiCore\ApiException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
@ -25,6 +26,12 @@ class GoogleAdsCampaigns
private $googleAdsCampaignService;
/**
* @Inject
* @var GoogleOAuthService
*/
private $googleOAuthService;
//微博热榜地址
@ -55,11 +62,14 @@ class GoogleAdsCampaigns
* get campaigns
* @throws ApiException
*/
public function getCampaigns($options): Response
public function getCampaigns($options)
{
$resourceName = $this->googleAdsCampaignService->runListCampaigns($options['customer_id']);
return $this->successResponse(['campaigns_list' => $resourceName]);
$customers = $this->googleOAuthService->getGoogleAdCustomers([]);
foreach ($customers as $customerId) {
$googleAdsCampaignService = new googleAdsCampaignService($customerId);
$resourceName = $googleAdsCampaignService->runListCampaigns($customerId);
}
// return $this->successResponse(['campaigns_list' => $resourceName]);
}
/**

View File

@ -4,6 +4,7 @@ namespace app\event;
use app\model\Campaign;
use app\service\GoogleAdsCampaignService;
use app\service\GoogleOAuthService;
use Google\ApiCore\ApiException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
@ -25,6 +26,13 @@ class GoogleAdsDateDatas
private $googleAdsCampaignService;
/**
* @Inject
* @var GoogleOAuthService
*/
private $googleOAuthService;
//微博热榜地址
@ -57,7 +65,12 @@ class GoogleAdsDateDatas
*/
public function getDateDatas($options)
{
$this->googleAdsCampaignService->runListDateDatas($options['customer_id'], $options['date']);
$customers = $this->googleOAuthService->getGoogleAdCustomers([]);
foreach ($customers as $customerId) {
$this->googleAdsCampaignService->runListDateDatas($customerId, $options['date']);
}
// $this->googleAdsCampaignService->runListDateDatas($options['customer_id'], $options['date']);
// return $this->successResponse(['data' => $resourceName]);
}

View File

@ -4,7 +4,9 @@ namespace app\event;
use app\model\AdGroup;
use app\service\GoogleAdsCampaignService;
use app\service\GoogleAdsGroupService;use Google\ApiCore\ApiException;
use app\service\GoogleAdsGroupService;
use app\service\GoogleOAuthService;
use Google\ApiCore\ApiException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use support\Db;
@ -25,6 +27,12 @@ class GoogleAdsGroups
private $googleAdsGroupService;
/**
* @Inject
* @var GoogleOAuthService
*/
private $googleOAuthService;
//微博热榜地址
@ -41,8 +49,6 @@ class GoogleAdsGroups
const countries = ["GB", "BE"];
public function listGroups(Request $request)
{
$options = $request->all();
@ -55,18 +61,24 @@ class GoogleAdsGroups
* get groups
* @throws ApiException
*/
public function getGroups($options): Response
public function getGroups($options)
{
$resourceName = $this->googleAdsGroupService->runListGroups($options['customer_id']);
$customers = $this->googleOAuthService->getGoogleAdCustomers([]);
return $this->successResponse(['groups_list' => $resourceName]);
foreach ($customers as $customerId) {
$googleAdsGroupService = new GoogleAdsGroupService($customerId);
$resourceName = $googleAdsGroupService->runListGroups($options['customer_id']);
// return $this->successResponse(['groups_list' => $resourceName]);
}
}
/**
* 每天爬取tiktok广告
* @return void
*/
public function update()
public
function update()
{
try {
@ -261,7 +273,8 @@ class GoogleAdsGroups
// 可以加入一些公共方法
protected function successResponse($data): Response
protected
function successResponse($data): Response
{
return Json([
'code' => 0,
@ -270,7 +283,8 @@ class GoogleAdsGroups
]);
}
protected function errorResponse($code, $message, $data = []): Response
protected
function errorResponse($code, $message, $data = []): Response
{
return Json([
'code' => $code,
@ -278,4 +292,4 @@ class GoogleAdsGroups
'data' => $data
]);
}
}
}

View File

@ -25,7 +25,7 @@ class UpdateGoogleAdsTask
// $dayBeforeYesterdayStart = date('Y-m-d', strtotime('-2 day'));
// dump($dayBeforeYesterdayStart . '更新' . GoogleAdsDateDatas::type . '开始');
// Event::emit(GoogleAdsDateDatas::type, ['customer_id' => 4060397299, 'date' => $dayBeforeYesterdayStart]);
// Event::emit(GoogleAdsDateDatas::type, ['date' => $dayBeforeYesterdayStart]);
}
);
@ -34,7 +34,7 @@ class UpdateGoogleAdsTask
new Crontab('20 */1 * * * *', function () {
// $yesterdayStart = date('Y-m-d', strtotime('-1 day'));
// dump($yesterdayStart . '更新' . GoogleAdsDateDatas::type . '开始');
// Event::emit(GoogleAdsDateDatas::type, ['customer_id' => 4060397299, 'date' => $yesterdayStart]);
// Event::emit(GoogleAdsDateDatas::type, ['date' => $yesterdayStart]);
}
);
@ -43,7 +43,7 @@ class UpdateGoogleAdsTask
//获取今天的 0 点的YYYY-MM-DD格式
// $todayStart = date('Y-m-d', strtotime('0 day'));
// dump($todayStart . '更新' . GoogleAdsDateDatas::type . '开始');
// Event::emit(GoogleAdsDateDatas::type, ['customer_id' => 4060397299, 'date' => $todayStart]);
// Event::emit(GoogleAdsDateDatas::type, ['date' => $todayStart]);
}
);
// 每15分钟执行一次

View File

@ -43,13 +43,18 @@ use Google\ApiCore\ApiException;
class GoogleAdsAdService extends BaseService
{
private $googleAdsClient;
private $customerId;
public function __construct()
public function __construct($customerId = null)
{
$advertiserId = getenv('GOOGLE_ADS_CUSTOMER_ID');
if ($customerId) {
$this->customerId = $customerId;
} else {
$this->customerId = getenv('GOOGLE_ADS_CUSTOMER_ID');
}
//
// // 从数据库获取 access_token
$refreshToken = $this->getRefreshTokenFromDatabase($advertiserId);
$refreshToken = $this->getRefreshTokenFromDatabase($this->customerId);
//
// if (!$refreshToken) {
// throw new \Exception("Access token not found for advertiserId: " . $advertiserId);
@ -268,10 +273,10 @@ class GoogleAdsAdService extends BaseService
}
// 更新数据库中的状态
// $ad->updateStatus($status);
if ($this->modifyDbAdStatus($adId, $status)){
if ($this->modifyDbAdStatus($adId, $status)) {
// 更新 Google Ads 上的状态
$googleAdsClient = $this->googleAdsClient;
$resourceName = self::updateAd($googleAdsClient,$customerId, $adGroupId, $adId, $status);
$resourceName = self::updateAd($googleAdsClient, $customerId, $adGroupId, $adId, $status);
return true;
}

View File

@ -42,12 +42,15 @@ class GoogleAdsCampaignService extends BaseService
private $customerId;
private const NUMBER_OF_CAMPAIGNS_TO_ADD = 1;
public function __construct()
public function __construct($customerId = null)
{
$advertiserId = getenv('GOOGLE_ADS_CUSTOMER_ID');
//
if ($customerId) {
$this->customerId = $customerId;
} else {
$this->customerId = getenv('GOOGLE_ADS_CUSTOMER_ID');
}
// 从数据库获取 access_token
$refreshToken = $this->getRefreshTokenFromDatabase($advertiserId);
$refreshToken = $this->getRefreshTokenFromDatabase($this->customerId);
//
// if (!$refreshToken) {
// throw new \Exception("Access token not found for advertiserId: " . $advertiserId);

View File

@ -29,13 +29,18 @@ use app\model\AdGroup as AdGroupModel;
class GoogleAdsGroupService extends BaseService
{
private $googleAdsClient;
private $customerId;
public function __construct()
public function __construct($customerId = null)
{
$advertiserId = getenv('GOOGLE_ADS_CUSTOMER_ID');
if ($customerId) {
$this->customerId = $customerId;
} else {
$this->customerId = getenv('GOOGLE_ADS_CUSTOMER_ID');
}
// 从数据库获取 access_token
$refreshToken = $this->getRefreshTokenFromDatabase($advertiserId);
$refreshToken = $this->getRefreshTokenFromDatabase($this->customerId);
//
// if (!$refreshToken) {
// throw new \Exception("Access token not found for advertiserId: " . $advertiserId);

View File

@ -4,6 +4,7 @@ namespace app\service;
use GuzzleHttp\Client;
use think\facade\Db as ThinkDb;
use app\model\ThirdUserAdvertiser;
class GoogleOAuthService
{
@ -117,4 +118,29 @@ class GoogleOAuthService
}
/**
* 批量获取全部Google广告账号数据
*
*/
public function getGoogleAdCustomers($options = [])
{
// 获取符合条件的客户ID数组
$customers = ThirdUserAdvertiser::alias('tua')
->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表
->where('tu.third_type', 'google') // 筛选 third_type 为 google 的记录
->field('tua.advertiser_id') // 获取 advertiser_id 字段
->select(); // 执行查询
// 如果没有找到符合条件的广告主,抛出异常
if ($customers->isEmpty()) {
return [];
// throw new ApiException('No customers found for google third type');
}
// 转换为简单的数组(提取 advertiser_id
return $customers->column('advertiser_id');
}
}