webman_ad/app/controller/NoticeController.php
2025-02-15 17:21:00 +08:00

98 lines
2.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\controller;
use app\service\GoogleOAuthService;
use support\Request;
use support\Response;
use DI\Annotation\Inject;
class NoticeController
{
/**
* @Inject
* @var GoogleOAuthService
*/
private $googleOAuthService;
public function handleGoogleBindingAccounts(Request $request)
{
// $state = $request->input('state') ?? $request->jwtClaims['uid'];
$state = $request->input('state') ?? $request->jwtClaims['merchant_id'];
$code = $request->input('code'); // 授权码
if (!$state) {
return $this->errorResponse(300, 'Invalid state parameter');
}
// state值验证通过继续处理授权码
$googleOAuthService = new GoogleOAuthService();
$tokens = $googleOAuthService->getRefreshToken($code);
if (!isset($tokens['refresh_token'])) {
if (isset($tokens['access_token'])) {
return $this->errorResponse(300, 'Google Ads Account is Authorized');
}
return $this->errorResponse(300, 'Refresh Token getting failed');
} else {
$googleOAuthService->saveRefreshToken($tokens['refresh_token'], $state);
}
return $this->successResponse($tokens, $request);
}
public function initNewGoogleAdsAccountData(Request $request)
{
$state = $request->input('state') ??'';
if ($state != 'sdf^&^*7hkjhjk') {
return $this->errorResponse(300, 'Invalid state parameter');
}
$refresh_token = $request->input('refresh_token'); // 授权码
// state值验证通过继续处理授权码
$googleOAuthService = new GoogleOAuthService();
if (!isset($refresh_token)) {
return $this->errorResponse(300, 'Refresh Token getting failed');
} else {
$googleOAuthService->initNewGoogleAdsAccountData($refresh_token);
}
return $this->successResponse('', $request);
}
// 可以加入一些公共方法
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
]);
}
}