804 lines
32 KiB
PHP
804 lines
32 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\AdsDashboardService;
|
||
use app\service\LandingUrlInsightService;
|
||
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 AdsDashboardService
|
||
*/
|
||
private $adsDashboardService;
|
||
|
||
/**
|
||
* @Inject
|
||
* @var LandingUrlInsightService
|
||
*/
|
||
private $landingUrlInsightService;
|
||
|
||
/**
|
||
* @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 aggregateAd(Request $request)
|
||
{
|
||
$options = $request->all();
|
||
// $startDate = $options['conditions']['startDate'] ?? null;
|
||
// $endDate = $options['conditions']['endDate'] ?? null;
|
||
$metrics = explode(',', $options['conditions']['metrics'] ?? '');
|
||
|
||
$result = [];
|
||
// $merchantId = $options['jwtClaims']['merchant_id'];
|
||
$preloadedData = [];
|
||
|
||
//是否远程调用
|
||
$request->rpc = true;
|
||
|
||
// 预处理指标类型
|
||
$needsDashboard = false;
|
||
$analyticsEntities = [];
|
||
foreach ($metrics as $metric) {
|
||
$parts = explode('#', $metric);
|
||
if (count($parts) !== 3) continue;
|
||
|
||
[$sourceType, $platform, $field] = $parts;
|
||
$sourceType = strtolower($sourceType);
|
||
$platformKey = strtolower($platform);
|
||
|
||
if ($sourceType === 'dashboard') {
|
||
$needsDashboard = true;
|
||
} elseif ($sourceType === 'analytics') {
|
||
if (in_array($platformKey, ['accounts', 'campaigns', 'adsets', 'ads', 'creatives'])) {
|
||
$analyticsEntities[$platformKey] = true;
|
||
}
|
||
}
|
||
}
|
||
// return $this->successResponse(['data' => [$needsDashboard,$analyticsEntities]], $request);
|
||
// 批量预加载数据
|
||
if ($needsDashboard) {
|
||
$preloadedData['dashboard'] = $this->listCards($request)->original['data'] ?? [];
|
||
}
|
||
|
||
foreach (array_keys($analyticsEntities) as $entity) {
|
||
$methodName = 'list' . ucfirst($entity);
|
||
|
||
if (method_exists($this, $methodName)) {
|
||
$preloadedData[$entity] = $this->$methodName($request);
|
||
}
|
||
}
|
||
// return $this->successResponse(['data' => $preloadedData], $request);
|
||
|
||
// 处理每个指标
|
||
foreach ($metrics as $metric) {
|
||
$parts = explode('#', $metric);
|
||
if (count($parts) !== 3) continue;
|
||
|
||
[$sourceType, $platform, $field] = $parts;
|
||
$sourceType = strtolower($sourceType);
|
||
$platformKey = strtolower($platform);
|
||
|
||
try {
|
||
if ($sourceType === 'dashboard') {
|
||
if (isset($preloadedData['dashboard'][$platformKey][$field])) {
|
||
$result[$platformKey][$field] = $preloadedData['dashboard'][$platformKey][$field];
|
||
}
|
||
} elseif ($sourceType === 'analytics' && isset($preloadedData[$platformKey])) {
|
||
if (isset($preloadedData[$platformKey][$field])) {
|
||
$result[$platformKey][$field] = $preloadedData[$platformKey][$field];
|
||
}
|
||
}
|
||
} catch (\Exception $e) {
|
||
$result[$platformKey]['errors'][$field] = $e->getMessage();
|
||
}
|
||
}
|
||
|
||
return $this->successResponse(['data' => $result], $request);
|
||
}
|
||
|
||
/*
|
||
* 返回标签页角标数字接口
|
||
*
|
||
*/
|
||
public function listAdBadges(Request $request)
|
||
{
|
||
$options = $request->all();
|
||
$options['jwtClaims'] = $request->jwtClaims;
|
||
|
||
// 获取请求参数
|
||
// $startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
||
// $endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id']]);
|
||
if (empty($accounts)) {
|
||
return $this->successResponse(['data' => []], $request);
|
||
}
|
||
$accountIds = array_unique(array_column($accounts, 'account_id'));
|
||
$endDateLastWeek = (int)date('Ymd');
|
||
$startDateLastWeek = (int)date('Ymd', strtotime('-6 days'));
|
||
// dump($startDateLastWeek, $endDateLastWeek);
|
||
// if ($startDateLastWeek === $startDate && $endDateLastWeek === $endDate) {
|
||
$ad_data_count = $this->adsInsightService::getAdCountData($options['jwtClaims']['merchant_id'], $accountIds, $startDateLastWeek, $endDateLastWeek);
|
||
// } else {
|
||
// $ad_data_count = [];
|
||
// }
|
||
// 返回结果
|
||
return $this->successResponse($ad_data_count, $request);
|
||
}
|
||
|
||
public function listAccounts(Request $request)
|
||
{
|
||
$options = $request->all();
|
||
$options['jwtClaims'] = $request->jwtClaims;
|
||
$options['rpc'] = $request->rpc ?? false;
|
||
if ($options['rpc']) {
|
||
//$options['conditions']['startDate']由2025-02-14处理成纯整数
|
||
$options['conditions']['startDate'] = (int)str_replace('-', '', $options['conditions']['startDate']);
|
||
$options['conditions']['endDate'] = (int)str_replace('-', '', $options['conditions']['endDate']);
|
||
}
|
||
|
||
|
||
// 获取请求参数
|
||
$page = $options['pageNo'] ?? 1; // 页码
|
||
$pageSize = $options['pageSize'] ?? 1000; // 每页数量
|
||
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
||
$platformType = $options['conditions']['platformType'] ?? 0; // 平台类型
|
||
// $status = $options['conditions']['status'] ?? 0; // 平台类型
|
||
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
||
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
||
$requireSpend = (bool)($options['conditions']['requireSpend'] ?? false);
|
||
// $dateRange = 'Last Week'; // 默认日期范围
|
||
|
||
// 根据 platformType 获取广告账户
|
||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType]);
|
||
|
||
if (empty($accounts)) {
|
||
return $this->successResponse(['data' => []], $request);
|
||
}
|
||
// dump($accounts);
|
||
// 获取客户ID数组
|
||
$accountIds = array_unique(array_column($accounts, 'account_id'));
|
||
|
||
|
||
// 调用 Service 层查询广告列表
|
||
$result = $this->adsInsightService::getAccountList(
|
||
$platformType, // 平台类型
|
||
$options['jwtClaims']['merchant_id'], // 店铺
|
||
$accountIds, // 客户 ID 数组
|
||
$page, // 页码
|
||
$pageSize, // 每页数量
|
||
$keyword, // 关键字
|
||
$startDate, // 开始日期
|
||
$endDate, // 结束日期
|
||
$requireSpend
|
||
);
|
||
|
||
if ($options['rpc']) {
|
||
return $result['statistics'];
|
||
}
|
||
|
||
|
||
// 返回结果
|
||
return $this->successResponse($result, $request);
|
||
}
|
||
|
||
public function listAds(Request $request)
|
||
{
|
||
$options = $request->all();
|
||
$options['jwtClaims'] = $request->jwtClaims;
|
||
$options['rpc'] = $request->rpc ?? false;
|
||
if ($options['rpc']) {
|
||
//$options['conditions']['startDate']由2025-02-14处理成纯整数
|
||
$options['conditions']['startDate'] = (int)str_replace('-', '', $options['conditions']['startDate']);
|
||
$options['conditions']['endDate'] = (int)str_replace('-', '', $options['conditions']['endDate']);
|
||
}
|
||
|
||
// 获取请求参数
|
||
$page = $options['pageNo'] ?? 1; // 页码
|
||
$pageSize = $options['pageSize'] ?? 1000; // 每页数量
|
||
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
||
$platformType = $options['conditions']['platformType'] ?? 0; // 平台类型
|
||
$status = $options['conditions']['status'] ?? 0; // 平台类型
|
||
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
||
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
||
$requireSpend = (bool)($options['conditions']['requireSpend'] ?? false);
|
||
// $dateRange = 'Last Week'; // 默认日期范围
|
||
|
||
// 根据 platformType 获取广告账户
|
||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType]);
|
||
|
||
if (empty($accounts)) {
|
||
return $this->successResponse(['data' => []], $request);
|
||
}
|
||
|
||
// 获取客户ID数组
|
||
$accountIds = array_unique(array_column($accounts, 'account_id'));
|
||
|
||
// 调用 Service 层查询广告列表
|
||
$result = $this->adsInsightService::getAdList(
|
||
$platformType, // 平台类型
|
||
$accountIds, // 客户 ID 数组
|
||
$page, // 页码
|
||
$pageSize, // 每页数量
|
||
$keyword, // 关键字
|
||
$startDate, // 开始日期
|
||
$endDate, // 结束日期
|
||
$status,
|
||
$requireSpend
|
||
);
|
||
|
||
if ($options['rpc']) {
|
||
return $result['statistics'];
|
||
}
|
||
|
||
// 返回结果
|
||
return $this->successResponse($result, $request);
|
||
}
|
||
|
||
public function listCampaigns(Request $request)
|
||
{
|
||
$options = $request->all();
|
||
$options['jwtClaims'] = $request->jwtClaims;
|
||
$options['rpc'] = $request->rpc ?? false;
|
||
if ($options['rpc']) {
|
||
//$options['conditions']['startDate']由2025-02-14处理成纯整数
|
||
$options['conditions']['startDate'] = (int)str_replace('-', '', $options['conditions']['startDate']);
|
||
$options['conditions']['endDate'] = (int)str_replace('-', '', $options['conditions']['endDate']);
|
||
}
|
||
|
||
// 获取请求参数
|
||
$page = $options['pageNo'] ?? 1; // 页码
|
||
$pageSize = $options['pageSize'] ?? 1000; // 每页数量
|
||
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
||
$platformType = $options['conditions']['platformType'] ?? 0; // 关键字搜索
|
||
$status = $options['conditions']['status'] ?? 0; // 平台类型
|
||
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
||
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
||
$requireSpend = (bool)($options['conditions']['requireSpend'] ?? false);
|
||
// $dateRange = 'Last Week'; // 默认日期范围
|
||
// 根据 platformType 获取广告账户
|
||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType]);
|
||
|
||
if (empty($accounts)) {
|
||
return $this->successResponse(['data' => []], $request);
|
||
}
|
||
|
||
// 获取客户ID数组
|
||
$accountIds = array_column($accounts, 'account_id');
|
||
// 调用 Service 层查询
|
||
$result = $this->adsInsightService::getCampaignList(
|
||
$platformType,
|
||
$accountIds, // 客户 ID 数组
|
||
$page, // 页码
|
||
$pageSize, // 每页数量
|
||
$keyword, // 关键字
|
||
$startDate, // 开始日期
|
||
$endDate, // 结束日期
|
||
$status,
|
||
$requireSpend
|
||
);
|
||
|
||
if ($options['rpc']) {
|
||
return $result['statistics'];
|
||
}
|
||
|
||
return $this->successResponse($result, $request);
|
||
// return $this->errorResponse(300,'授权失败');
|
||
}
|
||
|
||
public function exportCampaignsToExcel(Request $request)
|
||
{
|
||
$options = $request->all();
|
||
$options['jwtClaims'] = $request->jwtClaims;
|
||
|
||
// 获取请求参数
|
||
$page = $options['pageNo'] ?? 1; // 页码
|
||
$pageSize = 10000; // 每页数量
|
||
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
||
$platformType = $options['conditions']['platformType'] ?? 0; // 关键字搜索
|
||
$status = $options['conditions']['status'] ?? 0; // 平台类型
|
||
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
||
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
||
$requireSpend = (bool)($options['conditions']['requireSpend'] ?? false);
|
||
// $dateRange = 'Last Week'; // 默认日期范围
|
||
// 根据 platformType 获取广告账户
|
||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType]);
|
||
|
||
if (empty($accounts)) {
|
||
$data = [
|
||
'code' => 901,
|
||
'msg' => 'No data available for export.',
|
||
'data' => []
|
||
];
|
||
// return $this->successResponse(['data' => []], $request);
|
||
}
|
||
|
||
// 获取客户ID数组
|
||
$accountIds = array_column($accounts, 'account_id');
|
||
// 调用 Service 层查询
|
||
return $this->adsInsightService::exportCampaignsToExcel(
|
||
$platformType,
|
||
$accountIds, // 客户 ID 数组
|
||
$page, // 页码
|
||
$pageSize, // 每页数量
|
||
$keyword, // 关键字
|
||
$startDate, // 开始日期
|
||
$endDate, // 结束日期
|
||
$status,
|
||
$requireSpend
|
||
);
|
||
}
|
||
|
||
public function exportAccountsToExcel(Request $request)
|
||
{
|
||
$options = $request->all();
|
||
$options['jwtClaims'] = $request->jwtClaims;
|
||
|
||
// 获取请求参数
|
||
$page = $options['pageNo'] ?? 1; // 页码
|
||
$pageSize = 10000; // 每页数量
|
||
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
||
$platformType = $options['conditions']['platformType'] ?? 0; // 关键字搜索
|
||
// $status = $options['conditions']['status'] ?? 0; // 平台类型
|
||
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
||
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
||
$requireSpend = (bool)($options['conditions']['requireSpend'] ?? false);
|
||
// $dateRange = 'Last Week'; // 默认日期范围
|
||
// 根据 platformType 获取广告账户
|
||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType]);
|
||
|
||
if (empty($accounts)) {
|
||
$data = [
|
||
'code' => 901,
|
||
'msg' => 'No data available for export.',
|
||
'data' => []
|
||
];
|
||
return new Response(400, ['Content-Type' => 'application/json'], json_encode($data, JSON_UNESCAPED_UNICODE));
|
||
// return $this->successResponse(['data' => []], $request);
|
||
}
|
||
|
||
// 获取客户ID数组
|
||
$accountIds = array_column($accounts, 'account_id');
|
||
// 调用 Service 层查询
|
||
return $this->adsInsightService::exportAccountsToExcel(
|
||
$platformType,
|
||
$accountIds, // 客户 ID 数组
|
||
$page, // 页码
|
||
$pageSize, // 每页数量
|
||
$keyword, // 关键字
|
||
$startDate, // 开始日期
|
||
$endDate, // 结束日期
|
||
$requireSpend
|
||
);
|
||
}
|
||
|
||
public function exportCreativesToExcel(Request $request)
|
||
{
|
||
$options = $request->all();
|
||
$options['jwtClaims'] = $request->jwtClaims;
|
||
|
||
// 获取请求参数
|
||
$page = $options['pageNo'] ?? 1; // 页码
|
||
$pageSize = 10000; // 每页数量
|
||
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
||
$platformType = $options['conditions']['platformType'] ?? 0; // 关键字搜索
|
||
// $status = $options['conditions']['status'] ?? 0; // 平台类型
|
||
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
||
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
||
$requireSpend = (bool)($options['conditions']['requireSpend'] ?? false);
|
||
// $dateRange = 'Last Week'; // 默认日期范围
|
||
// 根据 platformType 获取广告账户
|
||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType]);
|
||
|
||
if (empty($accounts)) {
|
||
$data = [
|
||
'code' => 901,
|
||
'msg' => 'No data available for export.',
|
||
'data' => []
|
||
];
|
||
return new Response(400, ['Content-Type' => 'application/json'], json_encode($data, JSON_UNESCAPED_UNICODE));
|
||
// return $this->successResponse(['data' => []], $request);
|
||
}
|
||
|
||
// 获取客户ID数组
|
||
$accountIds = array_column($accounts, 'account_id');
|
||
// 调用 Service 层查询
|
||
return $this->adsInsightService::exportCreativesToExcel(
|
||
$platformType,
|
||
$accountIds, // 客户 ID 数组
|
||
$page, // 页码
|
||
$pageSize, // 每页数量
|
||
$keyword, // 关键字
|
||
$startDate, // 开始日期
|
||
$endDate, // 结束日期
|
||
$requireSpend
|
||
);
|
||
}
|
||
|
||
public function exportAdsetsToExcel(Request $request)
|
||
{
|
||
$options = $request->all();
|
||
$options['jwtClaims'] = $request->jwtClaims;
|
||
|
||
// 获取请求参数
|
||
$page = $options['pageNo'] ?? 1; // 页码
|
||
$pageSize = 10000; // 每页数量
|
||
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
||
$platformType = $options['conditions']['platformType'] ?? 0; // 关键字搜索
|
||
$status = $options['conditions']['status'] ?? 0; // 平台类型
|
||
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
||
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
||
$requireSpend = (bool)($options['conditions']['requireSpend'] ?? false);
|
||
// $dateRange = 'Last Week'; // 默认日期范围
|
||
// 根据 platformType 获取广告账户
|
||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType]);
|
||
|
||
if (empty($accounts)) {
|
||
$data = [
|
||
'code' => 901,
|
||
'msg' => 'No data available for export.',
|
||
'data' => []
|
||
];
|
||
return new Response(400, ['Content-Type' => 'application/json'], json_encode($data, JSON_UNESCAPED_UNICODE));
|
||
// return $this->successResponse(['data' => []], $request);
|
||
}
|
||
|
||
// 获取客户ID数组
|
||
$accountIds = array_column($accounts, 'account_id');
|
||
// 调用 Service 层查询
|
||
return $this->adsInsightService::exportAdsetsToExcel(
|
||
$platformType,
|
||
$accountIds, // 客户 ID 数组
|
||
$page, // 页码
|
||
$pageSize, // 每页数量
|
||
$keyword, // 关键字
|
||
$startDate, // 开始日期
|
||
$endDate, // 结束日期
|
||
$status,
|
||
$requireSpend
|
||
);
|
||
}
|
||
|
||
public function exportAdsToExcel(Request $request)
|
||
{
|
||
$options = $request->all();
|
||
$options['jwtClaims'] = $request->jwtClaims;
|
||
|
||
// 获取请求参数
|
||
$page = $options['pageNo'] ?? 1; // 页码
|
||
$pageSize = 10000; // 每页数量
|
||
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
||
$platformType = $options['conditions']['platformType'] ?? 0; // 关键字搜索
|
||
$status = $options['conditions']['status'] ?? 0; // 平台类型
|
||
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
||
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
||
$requireSpend = (bool)($options['conditions']['requireSpend'] ?? false);
|
||
// $dateRange = 'Last Week'; // 默认日期范围
|
||
// 根据 platformType 获取广告账户
|
||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType]);
|
||
|
||
if (empty($accounts)) {
|
||
$data = [
|
||
'code' => 901,
|
||
'msg' => 'No data available for export.',
|
||
'data' => []
|
||
];
|
||
return new Response(400, ['Content-Type' => 'application/json'], json_encode($data, JSON_UNESCAPED_UNICODE));
|
||
// return $this->successResponse(['data' => []], $request);
|
||
}
|
||
|
||
// 获取客户ID数组
|
||
$accountIds = array_column($accounts, 'account_id');
|
||
// 调用 Service 层查询
|
||
return $this->adsInsightService::exportAdsToExcel(
|
||
$platformType,
|
||
$accountIds, // 客户 ID 数组
|
||
$page, // 页码
|
||
$pageSize, // 每页数量
|
||
$keyword, // 关键字
|
||
$startDate, // 开始日期
|
||
$endDate, // 结束日期
|
||
$status,
|
||
$requireSpend
|
||
);
|
||
}
|
||
|
||
public function listAdsets(Request $request)
|
||
{
|
||
$options = $request->all();
|
||
$options['jwtClaims'] = $request->jwtClaims;
|
||
$options['rpc'] = $request->rpc ?? false;
|
||
if ($options['rpc']) {
|
||
//$options['conditions']['startDate']由2025-02-14处理成纯整数
|
||
$options['conditions']['startDate'] = (int)str_replace('-', '', $options['conditions']['startDate']);
|
||
$options['conditions']['endDate'] = (int)str_replace('-', '', $options['conditions']['endDate']);
|
||
}
|
||
|
||
// 获取请求参数
|
||
$page = $options['pageNo'] ?? 1; // 页码
|
||
$pageSize = $options['pageSize'] ?? 1000; // 每页数量
|
||
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
||
$platformType = $options['conditions']['platformType'] ?? 0; // 平台类型
|
||
$status = $options['conditions']['status'] ?? 0; // 平台类型
|
||
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
||
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
||
$requireSpend = (bool)($options['conditions']['requireSpend'] ?? false);
|
||
// $dateRange = 'Last Week'; // 默认日期范围
|
||
|
||
// 根据 platformType 获取广告账户
|
||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType]);
|
||
|
||
if (empty($accounts)) {
|
||
return $this->successResponse(['data' => []], $request);
|
||
}
|
||
|
||
// 获取客户ID数组
|
||
$accountIds = array_unique(array_column($accounts, 'account_id'));
|
||
// dump($accountIds);
|
||
// 调用 Service 层查询广告组列表
|
||
$result = $this->adsInsightService::getAdsetList(
|
||
$platformType,
|
||
$accountIds, // 客户 ID 数组
|
||
$page, // 页码
|
||
$pageSize, // 每页数量
|
||
$keyword, // 关键字
|
||
$startDate, // 开始日期
|
||
$endDate, // 结束日期
|
||
$status,
|
||
$requireSpend
|
||
);
|
||
|
||
if ($options['rpc']) {
|
||
return $result['statistics'];
|
||
}
|
||
|
||
// 返回结果
|
||
return $this->successResponse($result, $request);
|
||
}
|
||
|
||
public function listCreatives(Request $request)
|
||
{
|
||
$options = $request->all();
|
||
$options['jwtClaims'] = $request->jwtClaims;
|
||
if ($options['rpc']) {
|
||
//$options['conditions']['startDate']由2025-02-14处理成纯整数
|
||
$options['conditions']['startDate'] = (int)str_replace('-', '', $options['conditions']['startDate']);
|
||
$options['conditions']['endDate'] = (int)str_replace('-', '', $options['conditions']['endDate']);
|
||
}
|
||
|
||
// 获取请求参数
|
||
$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; // 结束日期
|
||
$requireSpend = (bool)($options['conditions']['requireSpend'] ?? false);
|
||
|
||
// $dateRange = 'Last Week'; // 默认日期范围
|
||
|
||
// 根据 platformType 获取广告账户
|
||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType]);
|
||
|
||
if (empty($accounts)) {
|
||
return $this->successResponse(['data' => []], $request);
|
||
}
|
||
|
||
// 获取客户ID数组
|
||
$accountIds = array_unique(array_column($accounts, 'account_id'));
|
||
// dump($accountIds);
|
||
// 调用 Service 层查询广告组列表
|
||
$result = $this->adsInsightService::getCreativeInsightData(
|
||
$platformType,
|
||
$accountIds, // 客户 ID 数组
|
||
$page, // 页码
|
||
$pageSize, // 每页数量
|
||
$keyword, // 关键字
|
||
$startDate, // 开始日期
|
||
$endDate, // 结束日期
|
||
$requireSpend
|
||
);
|
||
if ($options['rpc']) {
|
||
return $result['statistics'];
|
||
}
|
||
|
||
// 返回结果
|
||
return $this->successResponse($result, $request);
|
||
}
|
||
|
||
public function listLandingUrls(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 获取广告账户
|
||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType]);
|
||
|
||
if (empty($accounts)) {
|
||
return $this->successResponse(['data' => []], $request);
|
||
}
|
||
|
||
// 获取客户ID数组
|
||
$accountIds = array_unique(array_column($accounts, 'account_id'));
|
||
// dump($accountIds);
|
||
// 调用 Service 层查询广告组列表
|
||
$result = $this->landingUrlInsightService::getLandingUrlInsightData(
|
||
$platformType,
|
||
$accountIds, // 客户 ID 数组
|
||
$page, // 页码
|
||
$pageSize, // 每页数量
|
||
$keyword, // 关键字
|
||
$startDate, // 开始日期
|
||
$endDate // 结束日期
|
||
);
|
||
|
||
// 返回结果
|
||
return $this->successResponse($result, $request);
|
||
}
|
||
|
||
public function listCharts(Request $request)
|
||
{
|
||
$options = $request->all();
|
||
$options['jwtClaims'] = $request->jwtClaims;
|
||
|
||
// 获取请求参数
|
||
$cycle = $options['conditions']['cycle'] ?? 3; // 页码
|
||
$platformType = $options['conditions']['platformType'] ?? 0; // 平台类型
|
||
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
||
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
||
// $dateRange = 'Last Week'; // 默认日期范围
|
||
|
||
// 根据 platformType 获取广告账户
|
||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType]);
|
||
|
||
if (empty($accounts)) {
|
||
return $this->successResponse(['data' => []], $request);
|
||
}
|
||
|
||
// 获取客户ID数组
|
||
$accountIds = array_unique(array_column($accounts, 'account_id'));
|
||
// dump($accountIds);
|
||
// 调用 Service 层查询广告组列表
|
||
$result = $this->adsInsightService->getAdCycleInsight(
|
||
$platformType,
|
||
$accountIds, // 客户 ID 数组
|
||
$cycle, // 页码
|
||
$startDate, // 开始日期
|
||
$endDate // 结束日期
|
||
);
|
||
|
||
// 返回结果
|
||
return $this->successResponse($result, $request);
|
||
}
|
||
|
||
public function listCards(Request $request)
|
||
{
|
||
$options = $request->all();
|
||
$options['jwtClaims'] = $request->jwtClaims;
|
||
|
||
// 获取请求参数
|
||
$cycle = $options['conditions']['dateType'] ?? 'today'; //默认today || today yesterday、month、year 、custom
|
||
// $platformType = $options['conditions']['platformType'] ?? 0; // 平台类型
|
||
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
||
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
||
// $dateRange = 'Last Week'; // 默认日期范围
|
||
|
||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id']]);
|
||
|
||
|
||
if (empty($accounts)) {
|
||
return $this->successResponse(['data' => new \stdClass()], $request);
|
||
}
|
||
|
||
// 获取客户ID数组
|
||
$accountIds = array_unique(array_column($accounts, 'account_id'));
|
||
// dump($accountIds);
|
||
// 调用 Service 层查询广告组列表
|
||
$result = $this->adsDashboardService->getAdCycleCard(
|
||
$accountIds, // 客户 ID 数组
|
||
$cycle, // 页码
|
||
$startDate, // 开始日期
|
||
$endDate // 结束日期
|
||
);
|
||
|
||
// 返回结果
|
||
return $this->successResponse($result, $request);
|
||
}
|
||
|
||
|
||
// 可以加入一些公共方法
|
||
protected function successResponse($data, Request $request): Response
|
||
{
|
||
$responseData = [
|
||
'code' => 0,
|
||
'msg' => 'ok',
|
||
'data' => $data,
|
||
];
|
||
|
||
|
||
if (is_array($data) && empty($data)) {
|
||
$responseData['data'] = new \stdClass();
|
||
}
|
||
|
||
// 如果有新 token,添加到 header
|
||
if ($request->jwtNewToken) {
|
||
return new Response(200, [
|
||
'Content-Type' => 'application/json',
|
||
'X-New-Token' => $request->jwtNewToken,
|
||
], json_encode($responseData, JSON_UNESCAPED_UNICODE));
|
||
}
|
||
|
||
return Json($responseData);
|
||
}
|
||
|
||
|
||
protected
|
||
function errorResponse($code, $message, $data = []): Response
|
||
{
|
||
return Json([
|
||
'code' => $code,
|
||
'msg' => $message ?: 'error',
|
||
'data' => $data
|
||
]);
|
||
}
|
||
|
||
}
|