webman_ad/app/controller/OAuthController.php

134 lines
4.4 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;
use app\model\ThirdUserAdvertiser;
use app\model\ThirdUser;
class OAuthController
{
/**
* @Inject
* @var GoogleOAuthService
*/
private $googleOAuthService;
public function getAuthCode()
{
$authUrl = $this->googleOAuthService->getAuthUrl();
return $this->successResponse([
'url' => $authUrl,
]);
}
public function handleCallback(Request $request)
{
// $state = $request->input('state'); // 从Google回调中获取state
$code = $request->input('code'); // 授权码
// 验证state值是否与保存的值一致
// if ($state !== $_SESSION['oauth_state']) {
// if ($state !== '7a7a9edad5b48c127b7c14fabe39fae0') {
// return $this->errorResponse(400, 'Invalid state parameter');
// }
// state值验证通过继续处理授权码
$googleOAuthService = new GoogleOAuthService();
$tokens = $googleOAuthService->getRefreshToken($code);
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
$googleOAuthService = new GoogleOAuthService();
$googleOAuthService->revokeToken($accessToken);
return $this->successResponse(['deleted' => 'success']);
}
public function testRefreshToken(Request $request)
{
// 使用 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
]);
}
}