86 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			86 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace app\event;
 | |
| 
 | |
| 
 | |
| use app\service\BpsAdAccountService;
 | |
| use app\service\GoogleAdsGroupService;
 | |
| use app\service\GoogleOAuthService;
 | |
| use Google\ApiCore\ApiException;
 | |
| 
 | |
| use support\Response;
 | |
| use DI\Annotation\Inject;
 | |
| use Webman\RedisQueue\Redis;
 | |
| 
 | |
| //use QL\QueryList;
 | |
| 
 | |
| 
 | |
| class GoogleAdsGroups
 | |
| {
 | |
|     /**
 | |
|      * @Inject
 | |
|      * @var GoogleOAuthService
 | |
|      */
 | |
| 
 | |
|     private $googleOAuthService;
 | |
| 
 | |
|     /**
 | |
|      * @Inject
 | |
|      * @var BpsAdAccountService
 | |
|      */
 | |
| 
 | |
|     private $bpsAdAccountService;
 | |
| 
 | |
|     const event = 'googleads:groups:event';
 | |
|     const queue = 'googleads:groups:queue';
 | |
| 
 | |
| 
 | |
|     public function syncGroups($options = [])
 | |
|     {
 | |
|         $queue     = self::queue;
 | |
| //        $customers = $this->googleOAuthService->getGoogleAdCustomers($options);
 | |
|         $customers = $this->bpsAdAccountService->getGoogleAdAccounts($options);
 | |
|         foreach ($customers as $customer) {
 | |
|             if ($customer['login_customer_id'] > 0) {
 | |
|                 Redis::send($queue,$customer,rand(1,10));
 | |
|             }
 | |
|         }
 | |
|         return self::event.' send queue ok';
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * get groups
 | |
|      * @throws ApiException
 | |
|      */
 | |
|     public function getGroups($customer)
 | |
|     {
 | |
| 
 | |
|         $googleAdsGroupService = new GoogleAdsGroupService($customer['account_id']);
 | |
|         if ($customer['login_customer_id'] > 0) {
 | |
|             $resourceName = $googleAdsGroupService->runListGroups($customer['account_id'], $customer);
 | |
|         }
 | |
| //        return $this->successResponse(['groups_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
 | |
|         ]);
 | |
|     }
 | |
| }
 | 
