406 lines
14 KiB
PHP
406 lines
14 KiB
PHP
<?php
|
|
|
|
namespace app\controller;
|
|
|
|
use app\service\GoogleAdsAdService;
|
|
use app\service\GoogleAdsCampaignService;
|
|
use app\service\GoogleAdsGroupService;
|
|
use app\service\GoogleAdsReportService;
|
|
use app\service\GoogleOAuthService;
|
|
use DI\Annotation\Inject;
|
|
use support\Request;
|
|
use support\Response;
|
|
|
|
|
|
class AdController
|
|
{
|
|
|
|
/**
|
|
* @Inject
|
|
* @var GoogleAdsReportService
|
|
*/
|
|
private $googleAdsReportService;
|
|
|
|
/**
|
|
* @Inject
|
|
* @var GoogleAdsGroupService
|
|
*/
|
|
private $googleAdsGroupService;
|
|
|
|
/**
|
|
* @Inject
|
|
* @var GoogleAdsAdService
|
|
*/
|
|
private $googleAdsAdService;
|
|
|
|
/**
|
|
* @Inject
|
|
* @var GoogleAdsCampaignService
|
|
*/
|
|
private $googleAdsCampaignService;
|
|
|
|
/**
|
|
* @Inject
|
|
* @var GoogleOAuthService
|
|
*/
|
|
private $googleOAuthService;
|
|
|
|
public function listAds(Request $request)
|
|
{
|
|
$options = $request->all();
|
|
|
|
// 获取请求参数
|
|
$page = $options['pageNo'] ?? 1; // 页码
|
|
$pageSize = $options['pageSize'] ?? 10; // 每页数量
|
|
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
|
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
|
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
|
$dateRange = 'Last Week'; // 默认日期范围
|
|
|
|
$customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]);
|
|
$customerIds = array_column($customers, 'customer_id');
|
|
// dump($customers);
|
|
// 调用 Service 层查询
|
|
$result = $this->googleAdsReportService::getAdList(
|
|
$customerIds, // 客户 ID 数组
|
|
$page, // 页码
|
|
$pageSize, // 每页数量
|
|
$keyword, // 关键字
|
|
$dateRange,
|
|
$startDate, // 开始日期
|
|
$endDate
|
|
); // 结束日期);
|
|
return $this->successResponse($result, $request);
|
|
}
|
|
|
|
public function listAssets(Request $request)
|
|
{
|
|
$options = $request->all();
|
|
|
|
// 获取请求参数
|
|
$page = $options['pageNo'] ?? 1; // 页码
|
|
$pageSize = $options['pageSize'] ?? 10; // 每页数量
|
|
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
|
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
|
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
|
$dateRange = 'Last Week'; // 默认日期范围
|
|
|
|
// $customerId = 4060397299;
|
|
$customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]);
|
|
$customerIds = array_column($customers, 'customer_id');
|
|
//dump( $customerIds, // 客户 ID 数组
|
|
// $page, // 页码
|
|
// $pageSize, // 每页数量
|
|
// $keyword, // 关键字
|
|
// $dateRange,
|
|
// $startDate, // 开始日期
|
|
// $endDate);
|
|
// 调用 Service 层查询
|
|
$result = $this->googleAdsReportService->getAssetConversionData(
|
|
$customerIds, // 客户 ID 数组
|
|
$page, // 页码
|
|
$pageSize, // 每页数量
|
|
$keyword, // 关键字
|
|
$dateRange,
|
|
$startDate, // 开始日期
|
|
$endDate);
|
|
return $this->successResponse($result, $request);
|
|
}
|
|
|
|
public function listCampaigns(Request $request)
|
|
{
|
|
$options = $request->all();
|
|
|
|
// 获取请求参数
|
|
$page = $options['pageNo'] ?? 1; // 页码
|
|
$pageSize = $options['pageSize'] ?? 10; // 每页数量
|
|
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
|
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
|
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
|
$dateRange = 'Last Week'; // 默认日期范围
|
|
|
|
$customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]);
|
|
$customerIds = array_column($customers, 'customer_id');
|
|
// 调用 Service 层查询
|
|
$result = $this->googleAdsReportService::getCampaignList(
|
|
$customerIds, // 客户 ID 数组
|
|
$page, // 页码
|
|
$pageSize, // 每页数量
|
|
$keyword, // 关键字
|
|
$dateRange,
|
|
$startDate, // 开始日期
|
|
$endDate // 结束日期
|
|
);
|
|
return $this->successResponse($result, $request);
|
|
// return $this->errorResponse(300,'授权失败');
|
|
}
|
|
|
|
public function exportAdsToExcel(Request $request)
|
|
{
|
|
$options = $request->all();
|
|
|
|
// 获取请求参数
|
|
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
|
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
|
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
|
$dateRange = 'Last Week'; // 默认日期范围
|
|
|
|
$customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]);
|
|
$customerIds = array_column($customers, 'customer_id');
|
|
// 调用 service 层导出数据
|
|
return $this->googleAdsReportService::exportAdListToExcel($customerIds, $keyword, $dateRange, $startDate, $endDate);
|
|
}
|
|
|
|
public function exportCampaignsToExcel(Request $request)
|
|
{
|
|
$options = $request->all();
|
|
// 获取请求参数
|
|
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
|
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
|
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
|
$dateRange = 'Last Week'; // 默认日期范围
|
|
|
|
$customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]);
|
|
$customerIds = array_column($customers, 'customer_id');
|
|
// dump($customerIds);
|
|
// 调用 service 层导出数据
|
|
return $this->googleAdsReportService->exportCampaignsToExcel($customerIds, $keyword, $dateRange, $startDate, $endDate);
|
|
}
|
|
|
|
public function exportGroupsToExcel(Request $request)
|
|
{
|
|
$options = $request->all();
|
|
// 获取请求参数
|
|
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
|
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
|
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
|
$dateRange = 'Last Week'; // 默认日期范围
|
|
|
|
$customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]);
|
|
$customerIds = array_column($customers, 'customer_id');
|
|
|
|
// 你可以进一步验证日期格式(可选)
|
|
// if ($startDate && !strtotime($startDate)) {
|
|
// return response()->json(['error' => 'Invalid start date format'], 400);
|
|
// }
|
|
// if ($endDate && !strtotime($endDate)) {
|
|
// return response()->json(['error' => 'Invalid end date format'], 400);
|
|
// }
|
|
|
|
// 调用 service 层导出数据
|
|
return $this->googleAdsReportService->exportAdGroupsToExcel($customerIds, $keyword, $dateRange, $startDate, $endDate);
|
|
}
|
|
|
|
public function listGroups(Request $request)
|
|
{
|
|
$options = $request->all();
|
|
|
|
// 获取请求参数
|
|
$page = $options['pageNo'] ?? 1; // 页码
|
|
$pageSize = $options['pageSize'] ?? 10; // 每页数量
|
|
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
|
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
|
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
|
$dateRange = 'Last Week'; // 默认日期范围
|
|
|
|
$customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]);
|
|
$customerIds = array_column($customers, 'customer_id');
|
|
// 调用 Service 层查询
|
|
$result = $this->googleAdsReportService::getAdGroupList(
|
|
$customerIds, // 客户 ID 数组
|
|
$page, // 页码
|
|
$pageSize, // 每页数量
|
|
$keyword, // 关键字
|
|
$dateRange,
|
|
$startDate, // 开始日期
|
|
$endDate
|
|
);
|
|
return $this->successResponse($result, $request);
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取广告系列的状态 备用
|
|
*/
|
|
// public function getCampaignStatus(int $campaignId): Response
|
|
// {
|
|
// try {
|
|
// $status = $this->googleAdsCampaignService->getCampaignStatus($campaignId);
|
|
// return $this->successResponse(['status' => $status]);
|
|
// } catch (ValidateException $e) {
|
|
// return $this->errorResponse(400, $e->getMessage());
|
|
// }
|
|
// }
|
|
|
|
/**
|
|
* 更新广告系列的状态
|
|
*/
|
|
public function updateCampaignStatus(Request $request): Response
|
|
{
|
|
$campaignStatus = [
|
|
0, // UNSPECIFIED
|
|
1, // UNKNOWN
|
|
2, // ENABLED
|
|
3, // PAUSED
|
|
4, // REMOVED
|
|
];
|
|
$requestData = $request->all(); // 获取请求数据
|
|
$requestData['refresh_token'] = $request->refresh_token;
|
|
$requestData['login_customer_id'] = $request->login_customer_id;
|
|
// dump($requestData);
|
|
|
|
$status = $requestData['status'];
|
|
if (!in_array($status, $campaignStatus)) {
|
|
return $this->errorResponse(101, 'status参数错误');
|
|
}
|
|
// try {
|
|
$reslut = $this->googleAdsCampaignService->updateCampaignStatus($requestData);
|
|
if (!$reslut) {
|
|
return $this->errorResponse(101, 'Status update failed');
|
|
}
|
|
return $this->successResponse(['message' => 'Status updated successfully'], $request);
|
|
// } catch (ValidateException $e) {
|
|
// return $this->errorResponse(400, $e->getMessage());
|
|
// }
|
|
}
|
|
|
|
/**
|
|
* 判断广告系列是否启用
|
|
*/
|
|
// public function isEnabled(int $campaignId): Json
|
|
// {
|
|
// try {
|
|
// $isEnabled = $this->campaignService->isCampaignEnabled($campaignId);
|
|
// return json(['enabled' => $isEnabled], 200);
|
|
// } catch (ValidateException $e) {
|
|
// return json(['error' => $e->getMessage()], 400);
|
|
// }
|
|
// }
|
|
|
|
/**
|
|
* 判断广告系列是否暂停
|
|
*/
|
|
// public function isPaused(int $campaignId): Json
|
|
// {
|
|
// try {
|
|
// $isPaused = $this->campaignService->isCampaignPaused($campaignId);
|
|
// return json(['paused' => $isPaused], 200);
|
|
// } catch (ValidateException $e) {
|
|
// return json(['error' => $e->getMessage()], 400);
|
|
// }
|
|
// }
|
|
|
|
/**
|
|
* 判断广告系列是否停止
|
|
*/
|
|
// public function isStopped(int $campaignId): Json
|
|
// {
|
|
// try {
|
|
// $isStopped = $this->campaignService->isCampaignStopped($campaignId);
|
|
// return json(['stopped' => $isStopped], 200);
|
|
// } catch (ValidateException $e) {
|
|
// return json(['error' => $e->getMessage()], 400);
|
|
// }
|
|
// }
|
|
//
|
|
|
|
/**
|
|
* 更新广告组的状态
|
|
*/
|
|
public function updateGroupStatus(Request $request): Response
|
|
{
|
|
$adGroupStatus = [
|
|
0, // UNSPECIFIED
|
|
1, // UNKNOWN
|
|
2, // ENABLED
|
|
3, // PAUSED
|
|
4 // REMOVED
|
|
];
|
|
|
|
$requestData = $request->all(); // 获取请求数据
|
|
$requestData['refresh_token'] = $request->refresh_token;
|
|
$requestData['login_customer_id'] = $request->login_customer_id;
|
|
// dump($requestData);
|
|
|
|
$status = $requestData['status'];
|
|
// $options['bid_micro_amount'] = $options['amount'] * 1000000 < 0 ? 0 : $options['amount'] * 1000000;
|
|
if (!in_array($status, $adGroupStatus)) {
|
|
return $this->errorResponse(101, 'status参数错误');
|
|
}
|
|
// try {
|
|
$result = $this->googleAdsGroupService->updateGroupStatus($requestData);
|
|
if (!$result) {
|
|
return $this->errorResponse(101, 'Status update failed');
|
|
}
|
|
return $this->successResponse(['message' => 'Status updated successfully'], $request);
|
|
// } catch (ValidateException $e) {
|
|
// return $this->errorResponse(400, $e->getMessage());
|
|
// }
|
|
}
|
|
|
|
/**
|
|
* 更新广告的状态
|
|
*/
|
|
public function updateAdStatus(Request $request): Response
|
|
{
|
|
$adStatus = [
|
|
0, // UNSPECIFIED
|
|
1, // UNKNOWN
|
|
2, // ENABLED
|
|
3, // PAUSED
|
|
4 // REMOVED
|
|
];
|
|
|
|
$requestData = $request->all(); // 获取请求数据
|
|
$requestData['refresh_token'] = $request->refresh_token;
|
|
$requestData['login_customer_id'] = $request->login_customer_id;
|
|
// dump($requestData);
|
|
|
|
$status = $requestData['status'];
|
|
if (!in_array($status, $adStatus)) {
|
|
return $this->errorResponse(101, 'status参数错误');
|
|
}
|
|
// try {
|
|
$result = $this->googleAdsAdService->updateAdStatus($requestData);
|
|
if (!$result) {
|
|
return $this->errorResponse(101, 'Status update failed');
|
|
}
|
|
return $this->successResponse(['message' => 'Status updated successfully'], $request);
|
|
// } catch (ValidateException $e) {
|
|
// return $this->errorResponse(400, $e->getMessage());
|
|
// }
|
|
}
|
|
|
|
|
|
// 可以加入一些公共方法
|
|
protected function successResponse($data, Request $request): Response
|
|
{
|
|
if ($request->jwtNewToken) {
|
|
return new Response(200,
|
|
[
|
|
'Content-Type' => 'application/json',
|
|
'X-New-Token' => $request->jwtNewToken
|
|
],
|
|
json_encode($data, JSON_UNESCAPED_UNICODE));
|
|
} else {
|
|
return Json([
|
|
'code' => 0,
|
|
'msg' => 'ok',
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
protected function errorResponse($code, $message, $data = []): Response
|
|
{
|
|
return Json([
|
|
'code' => $code,
|
|
'msg' => $message ?: 'error',
|
|
'data' => $data
|
|
]);
|
|
}
|
|
|
|
}
|