554 lines
21 KiB
PHP
554 lines
21 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\AdsInsightService;
|
|
use app\service\GoogleOAuthService;
|
|
use app\service\BpsAdAccountService;
|
|
use DI\Annotation\Inject;
|
|
use support\Request;
|
|
use support\Response;
|
|
|
|
|
|
class BpsAdController
|
|
{
|
|
|
|
/**
|
|
* @Inject
|
|
* @var AdsInsightService
|
|
*/
|
|
private $adsInsightService;
|
|
|
|
/**
|
|
* @Inject
|
|
* @var GoogleAdsGroupService
|
|
*/
|
|
private $googleAdsGroupService;
|
|
|
|
/**
|
|
* @Inject
|
|
* @var GoogleAdsAdService
|
|
*/
|
|
private $googleAdsAdService;
|
|
|
|
/**
|
|
* @Inject
|
|
* @var GoogleAdsCampaignService
|
|
*/
|
|
private $googleAdsCampaignService;
|
|
|
|
/**
|
|
* @Inject
|
|
* @var GoogleOAuthService
|
|
*/
|
|
private $googleOAuthService;
|
|
/**
|
|
* @Inject
|
|
* @var BpsAdAccountService
|
|
*/
|
|
private $bpsAdAccountService;
|
|
|
|
|
|
public function listThirdUsers(Request $request)
|
|
{
|
|
$options = $request->all();
|
|
$options['jwtClaims'] = $request->jwtClaims;
|
|
|
|
// 获取请求参数
|
|
$page = $options['pageNo'] ?? 1; // 页码
|
|
$pageSize = $options['pageSize'] ?? 1000; // 每页数量
|
|
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
|
$platformType = $options['conditions']['platformType'] ?? 0; // 平台类型
|
|
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
|
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
|
// dump($options);
|
|
// 根据 platformType 获取第三方用户数据
|
|
if ($platformType === 1) {
|
|
// 获取 Facebook 第三方用户数据
|
|
$users = $this->bpsAdAccountService->getMetaThirdUsers(['uid' => $options['jwtClaims']['uid']]);
|
|
} elseif ($platformType === 2) {
|
|
// 获取 Google 第三方用户数据
|
|
$users = $this->bpsAdAccountService->getGoogleThirdUsers(['uid' => $options['jwtClaims']['uid']]);
|
|
} elseif ($platformType === 3) {
|
|
// 获取 TikTok 第三方用户数据
|
|
$users = $this->bpsAdAccountService->getTiktokThirdUsers(['uid' => $options['jwtClaims']['uid']]);
|
|
} else {
|
|
// 根据 JWT claims 获取所有平台的第三方用户
|
|
$users = $this->bpsAdAccountService->getAllThirdUsers(['uid' => $options['jwtClaims']['uid']]);
|
|
}
|
|
|
|
if (empty($users)) {
|
|
return $this->errorResponse(1, 'no data');
|
|
}
|
|
|
|
// 获取客户 ID 数组,并去重
|
|
$userIds = array_unique(array_column($users, 'id'));
|
|
|
|
|
|
// 调用 Service 层查询第三方用户数据列表
|
|
$result = $this->adsInsightService::getThirdUserList(
|
|
$platformType, // 平台类型
|
|
$userIds, // 第三方用户 ID 数组
|
|
$page, // 页码
|
|
$pageSize, // 每页数量
|
|
$keyword, // 关键字
|
|
$startDate, // 开始日期
|
|
$endDate // 结束日期
|
|
);
|
|
|
|
// 返回结果
|
|
return $this->successResponse($result, $request);
|
|
}
|
|
|
|
|
|
public function listAds(Request $request)
|
|
{
|
|
$options = $request->all();
|
|
$options['jwtClaims'] = $request->jwtClaims;
|
|
|
|
// 获取请求参数
|
|
$page = $options['pageNo'] ?? 1; // 页码
|
|
$pageSize = $options['pageSize'] ?? 1000; // 每页数量
|
|
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
|
$platformType = $options['conditions']['platformType'] ?? 0; // 平台类型
|
|
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
|
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
|
// $dateRange = 'Last Week'; // 默认日期范围
|
|
|
|
// 根据 platformType 获取广告账户
|
|
if ($platformType === 1) {
|
|
$accounts = $this->bpsAdAccountService->getMetaAdAccounts(['refresh_token' => $request->refresh_token]);
|
|
} elseif ($platformType === 2) {
|
|
$accounts = $this->bpsAdAccountService->getGoogleAdAccounts(['refresh_token' => $request->refresh_token]);
|
|
} elseif ($platformType === 3) {
|
|
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token]);
|
|
} else {
|
|
// TODO: 匹配jwt的商户id还是登录用户id
|
|
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]);
|
|
}
|
|
|
|
if (empty($accounts)) {
|
|
return $this->errorResponse(1, 'no data');
|
|
}
|
|
|
|
// 获取客户ID数组
|
|
$accountIds = array_column($accounts, 'account_id');
|
|
|
|
// 调用 Service 层查询广告列表
|
|
$result = $this->adsInsightService::getAdList(
|
|
$platformType, // 平台类型
|
|
$accountIds, // 客户 ID 数组
|
|
$page, // 页码
|
|
$pageSize, // 每页数量
|
|
$keyword, // 关键字
|
|
$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();
|
|
$options['jwtClaims'] = $request->jwtClaims;
|
|
|
|
// 获取请求参数
|
|
$page = $options['pageNo'] ?? 1; // 页码
|
|
$pageSize = $options['pageSize'] ?? 1000; // 每页数量
|
|
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
|
$platformType = $options['conditions']['platformType'] ?? 0; // 关键字搜索
|
|
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
|
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
|
// $dateRange = 'Last Week'; // 默认日期范围
|
|
// 根据 platformType 获取广告账户
|
|
if ($platformType === 1) {
|
|
$accounts = $this->bpsAdAccountService->getMetaAdAccounts(['refresh_token' => $request->refresh_token]);
|
|
} elseif ($platformType === 2) {
|
|
$accounts = $this->bpsAdAccountService->getGoogleAdAccounts(['refresh_token' => $request->refresh_token]);
|
|
} elseif ($platformType === 3) {
|
|
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token]);
|
|
} else {
|
|
// TODO: 匹配jwt的商户id还是登录用户id
|
|
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]);
|
|
}
|
|
|
|
if (empty($accounts)) {
|
|
return $this->errorResponse(1, 'no data');
|
|
}
|
|
|
|
// 获取客户ID数组
|
|
$accountIds = array_column($accounts, 'account_id');
|
|
// 调用 Service 层查询
|
|
$result = $this->adsInsightService::getCampaignList(
|
|
$platformType,
|
|
$accountIds, // 客户 ID 数组
|
|
$page, // 页码
|
|
$pageSize, // 每页数量
|
|
$keyword, // 关键字
|
|
$startDate, // 开始日期
|
|
$endDate // 结束日期
|
|
);
|
|
return $this->successResponse($result, $request);
|
|
// return $this->errorResponse(300,'授权失败');
|
|
}
|
|
|
|
public function listAdsets(Request $request)
|
|
{
|
|
$options = $request->all();
|
|
$options['jwtClaims'] = $request->jwtClaims;
|
|
|
|
// 获取请求参数
|
|
$page = $options['pageNo'] ?? 1; // 页码
|
|
$pageSize = $options['pageSize'] ?? 1000; // 每页数量
|
|
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
|
$platformType = $options['conditions']['platformType'] ?? 0; // 平台类型
|
|
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
|
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
|
// $dateRange = 'Last Week'; // 默认日期范围
|
|
|
|
// 根据 platformType 获取广告账户
|
|
if ($platformType === 1) {
|
|
$accounts = $this->bpsAdAccountService->getMetaAdAccounts(['refresh_token' => $request->refresh_token]);
|
|
} elseif ($platformType === 2) {
|
|
$accounts = $this->bpsAdAccountService->getGoogleAdAccounts(['refresh_token' => $request->refresh_token]);
|
|
} elseif ($platformType === 3) {
|
|
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token]);
|
|
} else {
|
|
// TODO: 匹配jwt的商户id还是登录用户id
|
|
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]);
|
|
}
|
|
|
|
if (empty($accounts)) {
|
|
return $this->errorResponse(1, 'no data');
|
|
}
|
|
|
|
// 获取客户ID数组
|
|
$accountIds = array_column($accounts, 'account_id');
|
|
|
|
// 调用 Service 层查询广告组列表
|
|
$result = $this->adsInsightService::getAdsetList(
|
|
$platformType,
|
|
$accountIds, // 客户 ID 数组
|
|
$page, // 页码
|
|
$pageSize, // 每页数量
|
|
$keyword, // 关键字
|
|
$startDate, // 开始日期
|
|
$endDate // 结束日期
|
|
);
|
|
|
|
// 返回结果
|
|
return $this->successResponse($result, $request);
|
|
}
|
|
|
|
|
|
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
|
|
]);
|
|
}
|
|
|
|
}
|