From d1c91de64004656fbbd825c64c1b1807d05c9220 Mon Sep 17 00:00:00 2001 From: hgc Date: Fri, 3 Jan 2025 15:35:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=89=B4=E6=9D=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/GoogleAdsController.php | 2 +- app/controller/OAuthController.php | 2 +- app/middleware/OauthCheck.php | 38 ++++++++++++++------------ app/service/GoogleAdsReportService.php | 24 ---------------- 4 files changed, 23 insertions(+), 43 deletions(-) diff --git a/app/controller/GoogleAdsController.php b/app/controller/GoogleAdsController.php index bfeb67f..95f29f8 100644 --- a/app/controller/GoogleAdsController.php +++ b/app/controller/GoogleAdsController.php @@ -248,7 +248,7 @@ class GoogleAdsController */ public function getCampaigns($options): Response { - $resourceName = $this->googleAdsCampaignService->runListCampaigns($options['customer_id']); + $resourceName = $this->googleAdsCampaignService->runListCampaigns($options['customer_id'], $options); return $this->successResponse(['campaigns_list' => $resourceName]); } diff --git a/app/controller/OAuthController.php b/app/controller/OAuthController.php index 597bd85..ed85da4 100644 --- a/app/controller/OAuthController.php +++ b/app/controller/OAuthController.php @@ -34,7 +34,7 @@ class OAuthController public function handleCallback(Request $request) { - $state = $request->input('state'); // 从Google回调中获取state + $state = $request->input('state') ?? $request->jwtClaims['uid']; $code = $request->input('code'); // 授权码 if (!$state) { diff --git a/app/middleware/OauthCheck.php b/app/middleware/OauthCheck.php index 8a66183..ecdbe5b 100644 --- a/app/middleware/OauthCheck.php +++ b/app/middleware/OauthCheck.php @@ -2,7 +2,8 @@ namespace app\middleware; -use app\model\ThirdUserAdvertiser; +use app\model\ThirdUser; + //use ReflectionClass; use Webman\MiddlewareInterface; use Webman\Http\Response; @@ -14,33 +15,36 @@ class OauthCheck implements MiddlewareInterface { $requestData = $request->all(); // 获取请求数据 - $customerId = isset($requestData['ad_customer_id']) ? $requestData['ad_customer_id'] : getenv('GOOGLE_ADS_CUSTOMER_ID'); - // 通过 advertiser_id 查询 ThirdUserAdvertiser,联表查询 ThirdUser 数据 - $userAdvertiser = ThirdUserAdvertiser::with('googleUser') // 联表查询 user 关联 - ->where('advertiser_id', $customerId) // 根据 advertiser_id 查询 - ->find(); // 获取第一个结果 -// 如果找到广告主数据 - if ($userAdvertiser && $userAdvertiser->googleUser) { - // 获取关联用户的 access_token - $accessToken = $userAdvertiser->googleUser->access_token; - if (empty($accessToken)) { +// $customerId = isset($requestData['ad_customer_id']) ? $requestData['ad_customer_id'] : getenv('GOOGLE_ADS_CUSTOMER_ID'); + $uid = $request->jwtClaims['uid']; + // 查询指定 user_id 的 ThirdUser + $thirdUser = ThirdUser::where('user_id', $uid)->find(); + + if ($thirdUser) { + // 获取 access_token + $request->access_token = $thirdUser->access_token; + + // 获取关联的广告主数据 + $advertiser = $thirdUser->advertisers()->find(); // 获取第一个广告主记录 + + if ($advertiser) { + // 获取 google_login_customer_id + $request->login_customer_id = $advertiser->google_login_customer_id; + } else { return Json([ - 'code' => 300, - 'msg' => 'AccessToken 为空或过期', + 'code' => 0, + 'msg' => 'Ads Data is ready to collect,please wait', 'data' => [] ]); } - $request->login_customer_id = $customerId; - $request->access_token = $accessToken; } else { return Json([ 'code' => 300, - 'msg' => '未找到该广告主或关联的用户', + 'msg' => 'AccessToken 为空或过期', 'data' => [] ]); } - // if (session('user')) { // // 已经登录,请求继续向洋葱芯穿越 // return $handler($request); diff --git a/app/service/GoogleAdsReportService.php b/app/service/GoogleAdsReportService.php index 7597fce..d8b7363 100644 --- a/app/service/GoogleAdsReportService.php +++ b/app/service/GoogleAdsReportService.php @@ -32,9 +32,6 @@ class GoogleAdsReportService $query->whereIn('a.customer_id', $customerIds); // 添加 customer_id 的查询约束 } else { return [ - 'code' => 0, - 'msg' => '接口正常响应', - 'data' => [ 'pagination' => [ 'startIndex' => 0, 'maxResults' => $pageSize, @@ -53,7 +50,6 @@ class GoogleAdsReportService 'be_roas' => '-', ], 'data' => [], - ], ]; } @@ -158,13 +154,9 @@ class GoogleAdsReportService ]; return [ - 'code' => 0, - 'msg' => '接口正常响应', - 'data' => [ 'pagination' => $pagination, 'statistics' => $statistics, 'data' => $result, - ], ]; } @@ -308,9 +300,6 @@ class GoogleAdsReportService // 检查 customerIds 是否为空,直接返回空结构 if (empty($customerIds)) { return [ - 'code' => 0, - 'msg' => '接口正常响应', - 'data' => [ 'pagination' => [ 'startIndex' => 0, 'maxResults' => $pageSize, @@ -329,7 +318,6 @@ class GoogleAdsReportService 'be_roas' => '-', ], 'data' => [], - ], ]; } // 动态构建日期条件 @@ -426,13 +414,9 @@ class GoogleAdsReportService ]; return [ - 'code' => 0, - 'msg' => '接口正常响应', - 'data' => [ 'pagination' => $pagination, 'statistics' => $statistics, 'data' => $result, - ], ]; } @@ -631,9 +615,6 @@ class GoogleAdsReportService // 检查 customerIds 是否为空,直接返回空结构 if (empty($customerIds)) { return [ - 'code' => 0, - 'msg' => '接口正常响应', - 'data' => [ 'pagination' => [ 'startIndex' => 0, 'maxResults' => $pageSize, @@ -652,7 +633,6 @@ class GoogleAdsReportService 'be_roas' => '-', ], 'data' => [], - ], ]; } @@ -748,13 +728,9 @@ class GoogleAdsReportService // 返回结果 return [ - 'code' => 0, - 'msg' => '接口正常响应', - 'data' => [ 'pagination' => $pagination, 'statistics' => $statistics, 'data' => $result, - ], ]; }