From 700e96d8804d18a7a00a29669d5089b4c133ed85 Mon Sep 17 00:00:00 2001 From: hgc Date: Sat, 4 Jan 2025 17:15:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=89facebook=E5=85=A5=E5=8F=82=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E4=BF=AE=E6=94=B9=E6=8E=A5=E5=8F=A3=E5=85=A5=E5=8F=82?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/AdController.php | 185 ++++++++++++------------- app/service/GoogleAdsReportService.php | 12 +- 2 files changed, 95 insertions(+), 102 deletions(-) diff --git a/app/controller/AdController.php b/app/controller/AdController.php index 33bb9aa..89c4c68 100644 --- a/app/controller/AdController.php +++ b/app/controller/AdController.php @@ -2,14 +2,14 @@ namespace app\controller; +use app\service\GoogleAdsAdService; use app\service\GoogleAdsCampaignService; use app\service\GoogleAdsGroupService; -use app\service\GoogleAdsAdService; use app\service\GoogleAdsReportService; use app\service\GoogleOAuthService; +use DI\Annotation\Inject; use support\Request; use support\Response; -use DI\Annotation\Inject; class AdController @@ -47,43 +47,43 @@ class AdController public function listAds(Request $request) { + $options = $request->all(); + // 获取请求参数 - $page = $request->input('page', 1); // 页码 - $pageSize = $request->input('page_size', 20); // 每页数量 - $keyword = $request->input('keyword', ''); // 关键字搜索 - $dateRange = $request->input('date_range', 'Last Week'); // 日期范围 + $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'; // 默认日期范围 - // 获取自定义日期范围 - $startDate = $request->input('start_date', null); // 开始日期,默认为 null - $endDate = $request->input('end_date', null); // 结束日期,默认为 null - - // 你可以进一步验证日期格式(可选) -// 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); -// } -// $customerIds = [8452924576,6977154211,1401879025]; $customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]); $customerIds = array_column($customers, 'customer_id'); // dump($customers); // 调用 Service 层查询 - $result = $this->googleAdsReportService::getAdList($customerIds, $page, $pageSize, $keyword, $dateRange, $startDate, $endDate); + $result = $this->googleAdsReportService::getAdList( + $customerIds, // 客户 ID 数组 + $page, // 页码 + $pageSize, // 每页数量 + $keyword, // 关键字 + $dateRange, + $startDate, // 开始日期 + $endDate + ); // 结束日期); return $this->successResponse($result); } public function listAssets(Request $request) { - // 获取请求参数 - $page = $request->input('page', 1); // 页码 - $pageSize = $request->input('page_size', 20); // 每页数量 - $keyword = $request->input('keyword', ''); // 关键字搜索 - $dateRange = $request->input('date_range', 'Last Week'); // 日期范围 + $options = $request->all(); - // 获取自定义日期范围 - $startDate = $request->input('start_date', null); // 开始日期,默认为 null - $endDate = $request->input('end_date', null); // 结束日期,默认为 null + // 获取请求参数 + $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]); @@ -98,54 +98,54 @@ class AdController // } // 调用 Service 层查询 - $result = $this->googleAdsReportService->getAssetConversionData($customerIds, $page, $pageSize, $keyword, $dateRange, $startDate, $endDate); + $result = $this->googleAdsReportService->getAssetConversionData( + $customerIds, // 客户 ID 数组 + $page, // 页码 + $pageSize, // 每页数量 + $keyword, // 关键字 + $dateRange, + $startDate, // 开始日期 + $endDate); return $this->successResponse($result); } public function listCampaigns(Request $request) { + $options = $request->all(); + // 获取请求参数 - $page = $request->input('page', 1); // 页码 - $pageSize = $request->input('page_size', 20); // 每页数量 - $keyword = $request->input('keyword', ''); // 关键字搜索 - $dateRange = $request->input('date_range', 'Last Week'); // 日期范围 + $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'; // 默认日期范围 - // 获取自定义日期范围 - $startDate = $request->input('start_date', null); // 开始日期,默认为 null - $endDate = $request->input('end_date', null); // 结束日期,默认为 null - - // 你可以进一步验证日期格式(可选) -// 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); -// } -// $customerIds = [8452924576, 6977154211, 1401879025]; $customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]); $customerIds = array_column($customers, 'customer_id'); // 调用 Service 层查询 - $result = $this->googleAdsReportService::getCampaignList($customerIds, $page, $pageSize, $keyword, $dateRange, $startDate, $endDate); + $result = $this->googleAdsReportService::getCampaignList( + $customerIds, // 客户 ID 数组 + $page, // 页码 + $pageSize, // 每页数量 + $keyword, // 关键字 + $dateRange, + $startDate, // 开始日期 + $endDate // 结束日期 + ); return $this->successResponse($result); // return $this->errorResponse(300,'授权失败'); } public function exportAdsToExcel(Request $request) { - $keyword = $request->input('keyword', ''); // 获取关键字参数 - $dateRange = $request->input('date_range', 'Last Week'); // 获取日期范围参数 + $options = $request->all(); - // 获取自定义日期范围 - $startDate = $request->input('start_date', null); // 开始日期,默认为 null - $endDate = $request->input('end_date', null); // 结束日期,默认为 null - - // 你可以进一步验证日期格式(可选) -// 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); -// } + // 获取请求参数 + $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'); @@ -155,35 +155,28 @@ class AdController public function exportCampaignsToExcel(Request $request) { - $keyword = $request->input('keyword', ''); // 获取关键字参数 - $dateRange = $request->input('date_range', 'Last Week'); // 获取日期范围参数 + $options = $request->all(); + // 获取请求参数 + $keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索 + $startDate = $options['conditions']['startDate'] ?? null; // 开始日期 + $endDate = $options['conditions']['endDate'] ?? null; // 结束日期 + $dateRange = 'Last Week'; // 默认日期范围 - // 获取自定义日期范围 - $startDate = $request->input('start_date', null); // 开始日期,默认为 null - $endDate = $request->input('end_date', null); // 结束日期,默认为 null - - // 你可以进一步验证日期格式(可选) -// 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); -// } $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); + return $this->googleAdsReportService->exportCampaignsToExcel($customerIds, $keyword, $dateRange, $startDate, $endDate); } public function exportGroupsToExcel(Request $request) { - $keyword = $request->input('keyword', ''); // 获取关键字参数 - $dateRange = $request->input('date_range', 'Last Week'); // 获取日期范围参数 - - // 获取自定义日期范围 - $startDate = $request->input('start_date', null); // 开始日期,默认为 null - $endDate = $request->input('end_date', null); // 结束日期,默认为 null + $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'); @@ -197,33 +190,33 @@ class AdController // } // 调用 service 层导出数据 - return $this->googleAdsReportService->exportAdGroupsToExcel($customerIds,$keyword, $dateRange, $startDate, $endDate); + return $this->googleAdsReportService->exportAdGroupsToExcel($customerIds, $keyword, $dateRange, $startDate, $endDate); } public function listGroups(Request $request) { + $options = $request->all(); + // 获取请求参数 - $page = $request->input('page', 1); // 页码 - $pageSize = $request->input('page_size', 20); // 每页数量 - $keyword = $request->input('keyword', ''); // 关键字搜索 - $dateRange = $request->input('date_range', 'Last Week'); // 日期范围 + $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'; // 默认日期范围 - // 获取自定义日期范围 - $startDate = $request->input('start_date', null); // 开始日期,默认为 null - $endDate = $request->input('end_date', null); // 结束日期,默认为 null - - // 你可以进一步验证日期格式(可选) -// 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); -// } -//$customerIds = [8452924576,6977154211,1401879025]; $customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]); $customerIds = array_column($customers, 'customer_id'); // 调用 Service 层查询 - $result = $this->googleAdsReportService::getAdGroupList($customerIds, $page, $pageSize, $keyword, $dateRange, $startDate, $endDate); + $result = $this->googleAdsReportService::getAdGroupList( + $customerIds, // 客户 ID 数组 + $page, // 页码 + $pageSize, // 每页数量 + $keyword, // 关键字 + $dateRange, + $startDate, // 开始日期 + $endDate + ); return $this->successResponse($result); } diff --git a/app/service/GoogleAdsReportService.php b/app/service/GoogleAdsReportService.php index 8ea0534..494e7be 100644 --- a/app/service/GoogleAdsReportService.php +++ b/app/service/GoogleAdsReportService.php @@ -92,9 +92,9 @@ class GoogleAdsReportService a.metadata') // Include metadata field ->group('a.ad_id, a.ad_name, a.status, a.customer_id, a.ad_group_id, g.ad_group_name, a.campaign_id, c.campaign_name') ->where(function ($query) use ($keyword) { - if ($keyword) { - $query->where('a.ad_name', 'like', '%' . $keyword . '%'); - } +// if ($keyword) { +// $query->where('a.ad_name', 'like', '%' . $keyword . '%'); +// } }); // 获取所有符合条件的数据(不分页) $allAds = $query->select()->toArray(); // 使用 toArray() 将对象转化为数组 @@ -249,9 +249,9 @@ class GoogleAdsReportService ->group('a.ad_id, a.ad_name, a.status, a.customer_id, a.ad_group_id, g.ad_group_name, a.campaign_id, c.campaign_name') ->where('a.customer_id', 'in', $customerIds) ->where(function ($query) use ($keyword) { - if ($keyword) { - $query->where('a.ad_name', 'like', '%' . $keyword . '%'); - } +// if ($keyword) { +// $query->where('a.ad_name', 'like', '%' . $keyword . '%'); +// } });