250 lines
9.2 KiB
PHP
250 lines
9.2 KiB
PHP
<?php
|
||
|
||
namespace app\controller;
|
||
|
||
use app\service\GoogleOAuthService;
|
||
use support\Request;
|
||
use support\Response;
|
||
use DI\Annotation\Inject;
|
||
use app\model\ThirdUserAdvertiser;
|
||
use app\model\ThirdUser;
|
||
|
||
use app\event\GoogleAdsCustomers;
|
||
use Webman\Event\Event;
|
||
|
||
class OAuthController
|
||
{
|
||
|
||
/**
|
||
* @Inject
|
||
* @var GoogleOAuthService
|
||
*/
|
||
private $googleOAuthService;
|
||
|
||
|
||
public function getAuthCode(Request $request)
|
||
{
|
||
$state = $request->get('state') ?? $request->jwtClaims['uid'];
|
||
$authUrl = $this->googleOAuthService->getAuthUrl($state);
|
||
return $this->successResponse([
|
||
'url' => $authUrl,
|
||
]);
|
||
}
|
||
|
||
|
||
public function handleCallback(Request $request)
|
||
{
|
||
$state = $request->input('state'); // 从Google回调中获取state
|
||
$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'])) {
|
||
return $this->errorResponse(300, 'Invalid state parameter');
|
||
} else {
|
||
$googleOAuthService->saveRefreshToken($tokens['refresh_token'], $state);
|
||
}
|
||
// if (getenv('GOOGLE_DEVELOP_TOKEN_LEVEL') === 'test') {
|
||
// $option['manager_customer_id'] = '1401879025'; //开发者
|
||
// $option['login_customer_id'] = '1401879025';
|
||
// }
|
||
// $option['refresh_token'] = $tokens['refresh_token'];
|
||
//
|
||
// $thirdUser = ThirdUser::where('access_token', $tokens['refresh_token'])->find(); // 获取第一个结果
|
||
// if ($thirdUser) {
|
||
// $option['third_user_id'] = $thirdUser->id;
|
||
// Event::emit(GoogleAdsCustomers::CUSTOMERADD, $option);
|
||
// } else {
|
||
// return $this->errorResponse(300, 'Invalid state parameter');
|
||
// }
|
||
return $this->successResponse($tokens);
|
||
|
||
}
|
||
|
||
public function getRefreshToken(Request $request)
|
||
{
|
||
$authCode = $request->input('code');
|
||
// $state = $request->input('state'); // 从Google回调中获取state
|
||
// 验证state值是否与保存的值一致
|
||
// if ($state !== $_SESSION['oauth_state']) {
|
||
// return $this->errorResponse(400, 'Invalid state parameter');
|
||
// }
|
||
$googleOAuthService = new GoogleOAuthService();
|
||
|
||
$tokens = $googleOAuthService->getRefreshToken($authCode);
|
||
if (!isset($tokens['refresh_token'])) {
|
||
return $this->successResponse($tokens);
|
||
}
|
||
// 保存refresh token到数据库
|
||
// $googleOAuthService->saveRefreshToken($tokens['refresh_token'], $tokens['access_token'], $request->user_id);
|
||
$googleOAuthService->saveRefreshToken($tokens['refresh_token'], $tokens['access_token']);
|
||
return $this->successResponse($tokens);
|
||
}
|
||
|
||
public function useRefreshToken(Request $request)
|
||
{
|
||
$refreshToken = $request->input('refresh_token');
|
||
$googleOAuthService = new GoogleOAuthService();
|
||
|
||
$newAccessToken = $googleOAuthService->useRefreshToken($refreshToken);
|
||
return $this->successResponse(['access_token' => $newAccessToken]);
|
||
}
|
||
|
||
public function revokeRefreshToken(Request $request)
|
||
{
|
||
// $accessToken = $request->input('token'); //access token
|
||
// $customerId = isset($requestData['customer_id']) ? $requestData['customer_id'] : getenv('GOOGLE_ADS_CUSTOMER_ID');
|
||
// $customerId = getenv('GOOGLE_ADS_CUSTOMER_ID'); //临时指定
|
||
|
||
$uid = $request->input('user_id');
|
||
|
||
// 通过 advertiser_id 查询 ThirdUserAdvertiser,联表查询 ThirdUser 数据
|
||
$thirdUser = ThirdUser::where('user_id', $uid)->find(); // 获取第一个结果
|
||
// dump($thirdUser); return ($uid);
|
||
if (!$thirdUser) {
|
||
return $this->errorResponse(300, '未授权');
|
||
}
|
||
// dump($userAdvertiser->googleUser->access_token);
|
||
$accessToken = $thirdUser->access_token;
|
||
|
||
$googleOAuthService = new GoogleOAuthService();
|
||
$googleOAuthService->revokeToken($accessToken, $thirdUser->id);
|
||
return $this->successResponse(['deleted' => 'success']);
|
||
|
||
}
|
||
|
||
|
||
//保存某个主体的全部access广告账号
|
||
function saveAdvertisers(Request $request)
|
||
{
|
||
$customerIds = $request->input('third_user_list_customers'); // customer_id_list每个元素包含advertiser_id
|
||
|
||
|
||
// $thirdUserId = $request->input('third_user_id'); // bps_third_user的id
|
||
$googleOAuthService = new GoogleOAuthService();
|
||
$hasThirdUser = $googleOAuthService->bindThirdUserAdvertiser($customerId, $thirdUserId);
|
||
if (!$hasThirdUser) {
|
||
return $this->errorResponse(300, 'Invalid state parameter');
|
||
}
|
||
return $this->successResponse($hasThirdUser);
|
||
}
|
||
|
||
|
||
public function testRefreshToken(Request $request)
|
||
{
|
||
|
||
// 接建立socket连到内部推送端口
|
||
$client = stream_socket_client('tcp://192.168.21.27:22101', $errorCode, $errorMessage);
|
||
if (false === $client) {
|
||
throw new \Exception('rpc failed to connect: ' . $errorMessage);
|
||
}
|
||
$rpc_request = [
|
||
'class' => 'Auth',
|
||
'method' => 'ValidateJwtToken',
|
||
'args' => [
|
||
[
|
||
'uid' => 2023,
|
||
'username' => 'Tinywan',
|
||
]
|
||
]
|
||
];
|
||
// 发送数据,注意5678端口是Text协议的端口,Text协议需要在数据末尾加上换行符
|
||
fwrite($client, json_encode($rpc_request) . "\n");
|
||
// 读取推送结果
|
||
$result = fgets($client, 10240000);
|
||
// 解析JSON字符串
|
||
$result = json_decode($result, true);
|
||
dump($result);
|
||
return $this->successResponse($result);
|
||
|
||
//发布事件
|
||
// $dayBeforeYesterdayStart = date('Y-m-d', strtotime('-2 day'));
|
||
// dump($dayBeforeYesterdayStart . '更新' . GoogleAdsDateDatas::type . '开始');
|
||
// Event::emit(GoogleAdsDateDatas::type, ['date' => $dayBeforeYesterdayStart]);
|
||
|
||
// dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsCampaigns::type . '开始');
|
||
// Event::emit(GoogleAdsCampaigns::type, []);
|
||
// dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsGroups::type . '开始');
|
||
// Event::emit(GoogleAdsGroups::type, []);
|
||
// dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsAds::type . '开始');
|
||
// Event::emit(GoogleAdsAdMaterials::type, []);
|
||
dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsMaterials::type . '开始');
|
||
Event::emit(GoogleAdsMaterials::type, []);
|
||
|
||
return $this->successResponse(['data' => []]);
|
||
|
||
|
||
// $customers = $this->googleOAuthService->getGoogleAdCustomers([]);
|
||
// return $this->successResponse(['data' => $customers]);
|
||
|
||
|
||
// $list = ThinkDb::table('bps.bps_third_user')->where('third_type', 'google')->select();
|
||
// return $this->successResponse($list);
|
||
|
||
// $user = new ThirdUserModel;
|
||
// $data = [
|
||
// ['access_token' => 'bar', 'third_type' => 'google'],
|
||
// ['access_token' => 'bar1', 'third_type' => 'google'],
|
||
// ['access_token' => 'bar2', 'third_type' => 'google']
|
||
// ];
|
||
// $user->saveAll($data);
|
||
|
||
//
|
||
//
|
||
// ThinkDb::table('bps.bps_third_user')->insertAll($data);
|
||
|
||
// ThinkDb::name('bps_third_user')
|
||
// ->update(['id' => 10, 'user_id' => 'bbb']);
|
||
//
|
||
// return $this->successResponse(['added' =>'success']);
|
||
|
||
// 使用 ThinkDb 进行联表查询
|
||
// $advertiserId = 'your-advertiser-id'; // 假设你已经获得了广告商ID
|
||
$requestData = $request->all(); // 获取请求数据
|
||
$customerId = $requestData['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;
|
||
// dump($accessToken); // 打印 access_token
|
||
return $this->successResponse($accessToken); // 返回 access_token
|
||
} else {
|
||
// 如果没有找到广告主或关联的用户,返回错误信息
|
||
// dump('未找到该广告主或关联的用户');
|
||
return $this->errorResponse('101', '未找到该广告主或关联的用户');
|
||
}
|
||
|
||
}
|
||
|
||
|
||
// 可以加入一些公共方法
|
||
protected function successResponse($data): Response
|
||
{
|
||
return Json([
|
||
'code' => 0,
|
||
'msg' => 'ok',
|
||
'data' => $data,
|
||
]);
|
||
}
|
||
|
||
protected function errorResponse($code, $message, $data = []): Response
|
||
{
|
||
return Json([
|
||
'code' => $code,
|
||
'msg' => $message ?: 'error',
|
||
'data' => $data
|
||
]);
|
||
}
|
||
|
||
} |