每分钟定时轮询缓存是否有新的广告账号需要绑定

This commit is contained in:
hgc 2025-02-22 15:23:42 +08:00
parent 2178e81d89
commit 066c04a6cd
4 changed files with 116 additions and 26 deletions

View File

@ -33,6 +33,7 @@ class GoogleAdsCustomers
private $googleAdsAccountService; private $googleAdsAccountService;
const event = 'googleads:customers:event'; const event = 'googleads:customers:event';
const newbind_event = 'googleads:customers:newbind:event';
const add_queue = 'googleads:customers:add:queue'; const add_queue = 'googleads:customers:add:queue';
const init_queue = 'googleads:customers:init:queue'; const init_queue = 'googleads:customers:init:queue';
@ -100,11 +101,11 @@ class GoogleAdsCustomers
return; return;
} }
$refresh_token = $options['refresh_token']; $refresh_token = $options['refresh_token'];
$access_token = $options['access_token']; $access_token = $options['access_token'];
$merchant_id = $options['merchant_id']; $merchant_id = $options['merchant_id'];
$user_id = $options['user_id']; $user_id = $options['user_id'];
//dump($refresh_token,$merchant_id,$access_token,9999999); //dump($refresh_token,$merchant_id,$access_token,9999999);
$googleAdsAccounts = $this->getAccountHierarchyForBind($options); $googleAdsAccounts = $this->getAccountHierarchyForBind($options);
// dump($googleAdsAccounts, 555); // dump($googleAdsAccounts, 555);
// 初始化返回结果数组 // 初始化返回结果数组
$ad_accounts = []; $ad_accounts = [];
@ -145,6 +146,50 @@ class GoogleAdsCustomers
} }
//每分钟定时轮询缓存是否有新的广告账号需要绑定-荣洁set缓存
public function newBindAccount()
{
$redisKey = 'bps:ads:new_bind_account'; //$options['token'] 即uuid 例如f47ac10b-58cc-4372-a567-0e02b2c3d479
// 获取哈希表所有字段和值
$hashData = Redis::hGetAll($redisKey);
if (empty($hashData)) {
dump('no pending bindings accounts');
return;
}
foreach ($hashData as $uuid => $bindingStr) {
// 解析商户ID和账户列表 格式:"merchantId_platform_account1,account2"
[$merchantId, $platform, $accountsStr] = explode('_', $bindingStr, 3);
if((int)$platform !== 2){
continue;
}
// 需要实际数据来源
$bindAccounts = explode(',', $accountsStr);
foreach ($bindAccounts as $account) {
$data = [
'merchant_id' => $merchantId, // 分离商户ID
'account_id' => $account
];
$this->googleOAuthService->queueNew($data);
}
// 处理完成后删除哈希字段
Redis::hDel($redisKey, $uuid);
}
return $this->successResponse(['success' => 'new binded accounts queued for initialization.']);
}
// 没调用 管理用户访问权限
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);
}
/** /**
* 绑定接口-广告主体下层级账号 * 绑定接口-广告主体下层级账号
* @throws ApiException * @throws ApiException

View File

@ -24,6 +24,8 @@ class UpdateGoogleAdsTask
public function onWorkerStart() public function onWorkerStart()
{ {
define('CURRENT_TIME', date('Y-m-d H:i:s'));
// 每15分钟执行一次 // 每15分钟执行一次
new Crontab('2 2 0 * * *', function () { new Crontab('2 2 0 * * *', function () {
@ -74,17 +76,17 @@ class UpdateGoogleAdsTask
// 每15分钟执行一次 // 每15分钟执行一次
new Crontab('30 2 */12 * * *', function () { new Crontab('30 2 */12 * * *', function () {
dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsGroups::event . '开始'); dump(CURRENT_TIME . '更新' . GoogleAdsGroups::event . '开始');
Event::emit(GoogleAdsGroups::event, []); Event::emit(GoogleAdsGroups::event, []);
dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsCampaigns::event . '开始'); dump(CURRENT_TIME . '更新' . GoogleAdsCampaigns::event . '开始');
Event::emit(GoogleAdsCampaigns::event, []); Event::emit(GoogleAdsCampaigns::event, []);
} }
); );
// 每15分钟执行一次 // 每15分钟执行一次
new Crontab('5 2 */12 * * *', function () { new Crontab('5 2 */12 * * *', function () {
dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsAds::event . '开始'); dump(CURRENT_TIME . '更新' . GoogleAdsAds::event . '开始');
Event::emit(GoogleAdsAds::event, []); Event::emit(GoogleAdsAds::event, []);
} }
@ -92,25 +94,31 @@ class UpdateGoogleAdsTask
// 每15分钟执行一次 // 每15分钟执行一次
new Crontab('1 2 */12 * * *', function () { new Crontab('1 2 */12 * * *', function () {
dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsCreatives::event . '开始'); dump(CURRENT_TIME . '更新' . GoogleAdsCreatives::event . '开始');
Event::emit(GoogleAdsCreatives::event, []); Event::emit(GoogleAdsCreatives::event, []);
} }
); );
new Crontab('25 11 */12 * * *', function () { new Crontab('25 11 */12 * * *', function () {
dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsAssets::event . '开始'); dump(CURRENT_TIME . '更新' . GoogleAdsAssets::event . '开始');
Event::emit(GoogleAdsAssets::event, []); Event::emit(GoogleAdsAssets::event, []);
} }
); );
new Crontab('16 */1 * * * *', function () { new Crontab('16 */1 * * * *', function () {
dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsCustomers::event . '开始'); dump(CURRENT_TIME . '更新' . GoogleAdsCustomers::event . '开始');
Event::emit(GoogleAdsCustomers::event, []); Event::emit(GoogleAdsCustomers::event, []);
} }
); );
new Crontab('3 */1 * * * *', function () {
dump(CURRENT_TIME . '更新' . GoogleAdsCustomers::newbind_event . '开始');
Event::emit(GoogleAdsCustomers::newbind_event, []);
});
//临时调试shopify店铺定时任务 //临时调试shopify店铺定时任务
// new Crontab('*/20 * * * * *', function () { // new Crontab('*/20 * * * * *', function () {
// dump(date('Y-m-d H:i:s') . '更新' . ShopifyOrders::event . '开始'); // dump(CURRENT_TIME . '更新' . ShopifyOrders::event . '开始');
// Event::emit(ShopifyOrders::event, ['storeName' => 'fefc94-67', 'token' => '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']); // PSA shop // Event::emit(ShopifyOrders::event, ['storeName' => 'fefc94-67', 'token' => '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']); // PSA shop
// Event::emit(ShopifyOrders::event, ['storeName' => 'aeb0e7-b3', 'token' => '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']);// VIVI shop // Event::emit(ShopifyOrders::event, ['storeName' => 'aeb0e7-b3', 'token' => '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']);// VIVI shop
// } // }
@ -119,20 +127,20 @@ class UpdateGoogleAdsTask
// 每15分钟执行一次 // 每15分钟执行一次
// new Crontab('58 */15 * * * *', function () { // new Crontab('58 */15 * * * *', function () {
// dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsCampaigns::type . '开始'); // dump(CURRENT_TIME . '更新' . GoogleAdsCampaigns::type . '开始');
// Event::emit(GoogleAdsCampaigns::type, ['customer_id'=>4060397299]); // Event::emit(GoogleAdsCampaigns::type, ['customer_id'=>4060397299]);
// //
// }); // });
// 每30分钟执行一次 // 每30分钟执行一次
// new Crontab('0 */30 * * * *', function () { // new Crontab('0 */30 * * * *', function () {
// dump(date('Y-m-d H:i:s') . '更新' . V2ex::type . '开始'); // dump(CURRENT_TIME . '更新' . V2ex::type . '开始');
// Event::emit(V2ex::type, null); // Event::emit(V2ex::type, null);
// //
// dump(date('Y-m-d H:i:s') . '更新' . GitHub::type . '开始'); // dump(CURRENT_TIME . '更新' . GitHub::type . '开始');
// Event::emit(GitHub::type, null); // Event::emit(GitHub::type, null);
// //
// dump(date('Y-m-d H:i:s') . '更新' . JueJin::type . '开始'); // dump(CURRENT_TIME . '更新' . JueJin::type . '开始');
// Event::emit(JueJin::type, null); // Event::emit(JueJin::type, null);
// }); // });

View File

@ -32,7 +32,7 @@ class GoogleOAuthService
$client = new Client(); $client = new Client();
try { try {
$options = [ $options = [
"headers" => [ "headers" => [
'Authorization' => 'Bearer ' . $accessToken, 'Authorization' => 'Bearer ' . $accessToken,
], ],
@ -186,14 +186,48 @@ class GoogleOAuthService
// dump($sql,$data); // dump($sql,$data);
ThinkDb::execute($sql, $data); ThinkDb::execute($sql, $data);
} }
//没调用
// public function updateAdsMerchantRelation($data,$sendMq = 0)
// {
// $tableName = 'bps_ads_merchant_relation';
// $tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'bps.' . $tableName;
//
// $sql = "
// UPDATE {$tableName}
// SET
// user_id = :user_id,
// is_unbind = :is_unbind,
// updated_at = CURRENT_TIMESTAMP
// WHERE
// platform = 2
// AND merchant_id = :merchant_id
// AND account_id = :account_id
// ";
//
// ThinkDb::execute($sql, [
// 'user_id' => (string)$data['user_id'],
// 'is_unbind' => $data['is_unbind'] ?? 'f',
// 'merchant_id' => (string)$data['merchant_id'],
// 'account_id' => (string)$data['account_id'],
// 'user_id' => (string)$data['user_id'],
// ]);
//
// // 如果需要发送队列
// if ($sendMq == 1) {
// $this->queueNew([
// 'merchant_id' => $data['merchant_id'],
// 'account_id' => $data['account_id']
// ]);
// }
// }
//保存或更新某个主体部广告账号 //保存或更新某个主体部广告账号
public function saveThirdUserAdvertiserNew($data,$sendMq = 0) public function saveThirdUserAdvertiserNew($data, $sendMq = 0)
{ {
// 确保 account_id 和 merchant_id 是字符串 // 确保 account_id 和 merchant_id 是字符串
$data['account_id'] = (string)$data['account_id']; $data['account_id'] = (string)$data['account_id'];
$data['merchant_id'] = (string)$data['merchant_id']; $data['merchant_id'] = (string)$data['merchant_id'];
$data['user_id'] = (string)$data['user_id']; $data['user_id'] = (string)$data['user_id'];
$tableName = 'bps_ads_merchant_relation'; $tableName = 'bps_ads_merchant_relation';
$tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'bps.' . $tableName; $tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'bps.' . $tableName;
@ -208,10 +242,10 @@ class GoogleOAuthService
'refresh_token' => $data['refresh_token'], 'refresh_token' => $data['refresh_token'],
'account_name' => $data['account_name'], 'account_name' => $data['account_name'],
'currency' => $data['currency'], 'currency' => $data['currency'],
'platform_user_id' => $data['platform_user_id']??'', 'platform_user_id' => $data['platform_user_id'] ?? '',
'platform_user_name' => $data['platform_user_name']??'', 'platform_user_name' => $data['platform_user_name'] ?? '',
'is_unbind' => $data['is_unbind']??'f', 'is_unbind' => $data['is_unbind'] ?? 'f',
'ext_info' => $data['ext_info']??'{}', 'ext_info' => $data['ext_info'] ?? '{}',
]; ];
// dump($insertData,2525); // dump($insertData,2525);
// 构造SQL语句 // 构造SQL语句
@ -238,14 +272,14 @@ class GoogleOAuthService
// dump(ThinkDb::getLastSql(),2525); // dump(ThinkDb::getLastSql(),2525);
//如果要异步更新改广告账号数据的话。 //如果要异步更新改广告账号数据的话。
if($sendMq == 1){ if ($sendMq == 1) {
// Event::emit(GoogleAdsCustomers::add_queue, $data); // Event::emit(GoogleAdsCustomers::add_queue, $data);
$options = []; $options = [];
$options['merchant_id'] = $data['merchant_id']; $options['merchant_id'] = $data['merchant_id'];
$options['account_id'] = $data['account_id']; $options['account_id'] = $data['account_id'];
// $options['refresh_token'] = $data['refresh_token']; // $options['refresh_token'] = $data['refresh_token'];
// $options['login_customer_id'] = json_decode($data['ext_info'],true)['login_customer_id']??''; // $options['login_customer_id'] = json_decode($data['ext_info'],true)['login_customer_id']??'';
$this->queueNew($options); $this->queueNew($options);
} }
} }
@ -319,7 +353,7 @@ class GoogleOAuthService
$this->queue($options); $this->queue($options);
} }
public function saveRefreshTokenNew($refreshToken,$accessToken, $merchantId,$userId) public function saveRefreshTokenNew($refreshToken, $accessToken, $merchantId, $userId)
{ {
$data = [ $data = [
'refresh_token' => $refreshToken, 'refresh_token' => $refreshToken,
@ -356,7 +390,7 @@ class GoogleOAuthService
]); ]);
// 在数据库中删除或标记该`access_token(其实是refresh_token)`为无效 // 在数据库中删除或标记该`access_token(其实是refresh_token)`为无效
BpsAdsMerchantRelation::where('merchant_id', $merchantId)->where('platform',2)->delete(); BpsAdsMerchantRelation::where('merchant_id', $merchantId)->where('platform', 2)->delete();
// //
// $tableName = 'bps_third_user'; // $tableName = 'bps_third_user';
// $tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'bps' . $tableName; // $tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'bps' . $tableName;

View File

@ -78,6 +78,9 @@ return [
GoogleAdsCustomers::event => [ GoogleAdsCustomers::event => [
[GoogleAdsCustomers::class, 'addRootCustomersNew'], [GoogleAdsCustomers::class, 'addRootCustomersNew'],
], ],
GoogleAdsCustomers::newbind_event => [
[GoogleAdsCustomers::class, 'newBindAccount'],
],
GoogleAdsCustomers::add_queue => [ GoogleAdsCustomers::add_queue => [
[GoogleAdsCustomers::class, 'addRootCustomersNew'], [GoogleAdsCustomers::class, 'addRootCustomersNew'],
], ],