三方广告授权绑定跟由原来的登录uid改成merchant_id绑定

This commit is contained in:
huangguancheng 2025-01-21 15:01:06 +08:00
parent 0ac4027bc9
commit 78d23dd376
9 changed files with 83 additions and 110 deletions

View File

@ -80,7 +80,7 @@ class BpsAdController
// 获取请求参数 // 获取请求参数
// $startDate = $options['conditions']['startDate'] ?? null; // 开始日期 // $startDate = $options['conditions']['startDate'] ?? null; // 开始日期
// $endDate = $options['conditions']['endDate'] ?? null; // 结束日期 // $endDate = $options['conditions']['endDate'] ?? null; // 结束日期
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]); $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id']]);
if (empty($accounts)) { if (empty($accounts)) {
return $this->successResponse(['data' => []], $request); return $this->successResponse(['data' => []], $request);
} }
@ -130,7 +130,7 @@ class BpsAdController
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]); $accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]);
} else { } else {
// TODO: 匹配jwt的商户id还是登录用户id // TODO: 匹配jwt的商户id还是登录用户id
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]); $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id']]);
} }
if (empty($accounts)) { if (empty($accounts)) {
@ -174,16 +174,16 @@ class BpsAdController
// 根据 platformType 获取第三方用户数据 // 根据 platformType 获取第三方用户数据
if ($platformType === 1) { if ($platformType === 1) {
// 获取 Facebook 第三方用户数据 // 获取 Facebook 第三方用户数据
$users = $this->bpsAdAccountService->getMetaThirdUsers(['uid' => $options['jwtClaims']['uid']]); $users = $this->bpsAdAccountService->getMetaThirdUsers(['merchant_id' => $options['jwtClaims']['merchant_id']]);
} elseif ($platformType === 2) { } elseif ($platformType === 2) {
// 获取 Google 第三方用户数据 // 获取 Google 第三方用户数据
$users = $this->bpsAdAccountService->getGoogleThirdUsers(['uid' => $options['jwtClaims']['uid']]); $users = $this->bpsAdAccountService->getGoogleThirdUsers(['merchant_id' => $options['jwtClaims']['merchant_id']]);
} elseif ($platformType === 3) { } elseif ($platformType === 3) {
// 获取 TikTok 第三方用户数据 // 获取 TikTok 第三方用户数据
$users = $this->bpsAdAccountService->getTiktokThirdUsers(['uid' => $options['jwtClaims']['uid']]); $users = $this->bpsAdAccountService->getTiktokThirdUsers(['merchant_id' => $options['jwtClaims']['merchant_id']]);
} else { } else {
// 根据 JWT claims 获取所有平台的第三方用户 // 根据 JWT claims 获取所有平台的第三方用户
$users = $this->bpsAdAccountService->getAllThirdUsers(['uid' => $options['jwtClaims']['uid']]); $users = $this->bpsAdAccountService->getAllThirdUsers(['merchant_id' => $options['jwtClaims']['merchant_id']]);
} }
if (empty($users)) { if (empty($users)) {
@ -243,7 +243,7 @@ class BpsAdController
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]); $accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]);
} else { } else {
// TODO: 匹配jwt的商户id还是登录用户id // TODO: 匹配jwt的商户id还是登录用户id
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]); $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id']]);
} }
if (empty($accounts)) { if (empty($accounts)) {
@ -301,7 +301,7 @@ class BpsAdController
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]); $accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]);
} else { } else {
// TODO: 匹配jwt的商户id还是登录用户id // TODO: 匹配jwt的商户id还是登录用户id
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]); $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id']]);
} }
if (empty($accounts)) { if (empty($accounts)) {
@ -357,7 +357,7 @@ class BpsAdController
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]); $accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]);
} else { } else {
// TODO: 匹配jwt的商户id还是登录用户id // TODO: 匹配jwt的商户id还是登录用户id
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]); $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id']]);
} }
if (empty($accounts)) { if (empty($accounts)) {
@ -411,7 +411,7 @@ class BpsAdController
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]); $accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]);
} else { } else {
// TODO: 匹配jwt的商户id还是登录用户id // TODO: 匹配jwt的商户id还是登录用户id
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]); $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id']]);
} }
if (empty($accounts)) { if (empty($accounts)) {
@ -464,7 +464,7 @@ class BpsAdController
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]); $accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]);
} else { } else {
// TODO: 匹配jwt的商户id还是登录用户id // TODO: 匹配jwt的商户id还是登录用户id
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]); $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id']]);
} }
if (empty($accounts)) { if (empty($accounts)) {
@ -517,7 +517,7 @@ class BpsAdController
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]); $accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]);
} else { } else {
// TODO: 匹配jwt的商户id还是登录用户id // TODO: 匹配jwt的商户id还是登录用户id
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]); $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id']]);
} }
if (empty($accounts)) { if (empty($accounts)) {
@ -571,7 +571,7 @@ class BpsAdController
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]); $accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]);
} else { } else {
// TODO: 匹配jwt的商户id还是登录用户id // TODO: 匹配jwt的商户id还是登录用户id
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]); $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id']]);
} }
if (empty($accounts)) { if (empty($accounts)) {
@ -626,7 +626,7 @@ class BpsAdController
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]); $accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]);
} else { } else {
// TODO: 匹配jwt的商户id还是登录用户id // TODO: 匹配jwt的商户id还是登录用户id
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]); $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id']]);
} }
if (empty($accounts)) { if (empty($accounts)) {
@ -684,7 +684,7 @@ class BpsAdController
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]); $accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]);
} else { } else {
// TODO: 匹配jwt的商户id还是登录用户id // TODO: 匹配jwt的商户id还是登录用户id
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]); $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id']]);
} }
if (empty($accounts)) { if (empty($accounts)) {
@ -741,7 +741,7 @@ class BpsAdController
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]); $accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]);
} else { } else {
// TODO: 匹配jwt的商户id还是登录用户id // TODO: 匹配jwt的商户id还是登录用户id
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]); $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id']]);
} }
if (empty($accounts)) { if (empty($accounts)) {
@ -796,7 +796,7 @@ class BpsAdController
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]); $accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]);
} else { } else {
// TODO: 匹配jwt的商户id还是登录用户id // TODO: 匹配jwt的商户id还是登录用户id
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]); $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id']]);
} }
if (empty($accounts)) { if (empty($accounts)) {
@ -832,7 +832,7 @@ class BpsAdController
// $dateRange = 'Last Week'; // 默认日期范围 // $dateRange = 'Last Week'; // 默认日期范围
// TODO: 匹配jwt的商户id还是登录用户id // TODO: 匹配jwt的商户id还是登录用户id
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]); $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id']]);
if (empty($accounts)) { if (empty($accounts)) {
@ -858,25 +858,31 @@ class BpsAdController
// 可以加入一些公共方法 // 可以加入一些公共方法
protected function successResponse($data, Request $request): Response protected function successResponse($data, Request $request): Response
{ {
if ($request->jwtNewToken) { $responseData = [
return new Response(200,
[
'Content-Type' => 'application/json',
'X-New-Token' => $request->jwtNewToken
],
json_encode($data, JSON_UNESCAPED_UNICODE));
} else {
return Json([
'code' => 0, 'code' => 0,
'msg' => 'ok', 'msg' => 'ok',
'data' => $data, 'data' => $data,
]); ];
// 如果 data 是空数组,转换为空对象
if (is_array($data) && empty($data)) {
$responseData['data'] = new \stdClass();
}
// 如果有新 token添加到 header
if ($request->jwtNewToken) {
return new Response(200, [
'Content-Type' => 'application/json',
'X-New-Token' => $request->jwtNewToken,
], json_encode($responseData, JSON_UNESCAPED_UNICODE));
}
return Json($responseData);
} }
} protected
function errorResponse($code, $message, $data = []): Response
protected function errorResponse($code, $message, $data = []): Response
{ {
return Json([ return Json([
'code' => $code, 'code' => $code,

View File

@ -27,18 +27,6 @@ class CustomerController
*/ */
private $googleAdsAccountService; private $googleAdsAccountService;
//绑定某个广告账号
public function bindAdvertiser(Request $request)
{
$customerId = $request->input('customer_id'); // 需要绑定的广告账号 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 getCustomerList(Request $request) public function getCustomerList(Request $request)
{ {

View File

@ -30,7 +30,7 @@ class OAuthController
public function getAuthCode(Request $request) public function getAuthCode(Request $request)
{ {
$state = $request->get('state') ?? $request->jwtClaims['uid']; $state = $request->get('state') ?? $request->jwtClaims['merchant_id'];
$authUrl = $this->googleOAuthService->getAuthUrl($state); $authUrl = $this->googleOAuthService->getAuthUrl($state);
return $this->successResponse([ return $this->successResponse([
'url' => $authUrl, 'url' => $authUrl,
@ -48,7 +48,8 @@ class OAuthController
'google' => ['status' => 0], 'google' => ['status' => 0],
'tiktok' => ['status' => 0], 'tiktok' => ['status' => 0],
]; ];
$accounts = $this->bpsAdAccountService->getAllThirdUsers(['uid' => $options['jwtClaims']['uid']]); // $accounts = $this->bpsAdAccountService->getAllThirdUsers(['uid' => $options['jwtClaims']['uid']]);
$accounts = $this->bpsAdAccountService->getAllThirdUsers(['merchant_id' => $options['jwtClaims']['merchant_id']]);
foreach ($accounts as $account) { foreach ($accounts as $account) {
$authorizedThirdUsers[$account['third_type']] = [ $authorizedThirdUsers[$account['third_type']] = [
@ -64,7 +65,8 @@ class OAuthController
public function handleCallback(Request $request) public function handleCallback(Request $request)
{ {
$state = $request->input('state') ?? $request->jwtClaims['uid']; // $state = $request->input('state') ?? $request->jwtClaims['uid'];
$state = $request->input('state') ?? $request->jwtClaims['merchant_id'];
$code = $request->input('code'); // 授权码 $code = $request->input('code'); // 授权码
if (!$state) { if (!$state) {
@ -123,10 +125,10 @@ class OAuthController
// $customerId = isset($requestData['customer_id']) ? $requestData['customer_id'] : getenv('GOOGLE_ADS_CUSTOMER_ID'); // $customerId = isset($requestData['customer_id']) ? $requestData['customer_id'] : getenv('GOOGLE_ADS_CUSTOMER_ID');
// $customerId = getenv('GOOGLE_ADS_CUSTOMER_ID'); //临时指定 // $customerId = getenv('GOOGLE_ADS_CUSTOMER_ID'); //临时指定
$uid = $request->input('user_id') ?? $request->jwtClaims['uid']; $merchant_id = $request->input('merchant_id') ?? $request->jwtClaims['merchant_id'];
// 通过 advertiser_id 查询 ThirdUserAdvertiser联表查询 ThirdUser 数据 // 通过 advertiser_id 查询 ThirdUserAdvertiser联表查询 ThirdUser 数据
$thirdUser = ThirdUser::where('user_id', $uid)->where('third_type', 'google')->find(); // 获取第一个结果 $thirdUser = ThirdUser::where('merchant_id', $merchant_id)->where('third_type', 'google')->find(); // 获取第一个结果
// dump($thirdUser); return ($uid); // dump($thirdUser); return ($uid);
if (!$thirdUser) { if (!$thirdUser) {
return $this->errorResponse(300, '未授权'); return $this->errorResponse(300, '未授权');

View File

@ -69,7 +69,7 @@ class GoogleAdsCustomers
// $customerId = $account['customer_id']; // $customerId = $account['customer_id'];
// dump($customerId, $thirdUser->id, $rootAccountId, $account); // dump($customerId, $thirdUser->id, $rootAccountId, $account);
// if($account['customer_id'] == 1509096882)continue; // if($account['customer_id'] == 1509096882)continue;
$this->googleOAuthService->saveThirdUserAdvertiser($account['customer_id'], $thirdUser->id, $rootAccountId, $account); $this->googleOAuthService->saveThirdUserAdvertiser($account['customer_id'], $thirdUser->id, $rootAccountId, $account,$thirdUser->merchant_id);
} }
} }
$this->googleOAuthService->updateThirdUserDefault($thirdUser->id, 't'); $this->googleOAuthService->updateThirdUserDefault($thirdUser->id, 't');
@ -103,7 +103,8 @@ class GoogleAdsCustomers
$account['customer_id'], $account['customer_id'],
$thirdUser->id, $thirdUser->id,
$rootId, $rootId,
$account $account,
$thirdUser->merchant_id
); );
} }
} }

View File

@ -16,9 +16,11 @@ class OauthCheck implements MiddlewareInterface
$requestData = $request->all(); // 获取请求数据 $requestData = $request->all(); // 获取请求数据
// $customerId = isset($requestData['ad_customer_id']) ? $requestData['ad_customer_id'] : getenv('GOOGLE_ADS_CUSTOMER_ID'); // $customerId = isset($requestData['ad_customer_id']) ? $requestData['ad_customer_id'] : getenv('GOOGLE_ADS_CUSTOMER_ID');
$uid = $request->jwtClaims['uid']; // $uid = $request->jwtClaims['uid'];
$merchant_id = $request->jwtClaims['merchant_id'];
// 查询指定 user_id 的 ThirdUser // 查询指定 user_id 的 ThirdUser
$thirdUser = ThirdUser::where('user_id', $uid)->where('third_type', 'google')->find(); // $thirdUser = ThirdUser::where('user_id', $uid)->where('third_type', 'google')->find();
$thirdUser = ThirdUser::where('merchant_id', $merchant_id)->where('third_type', 'google')->find();
if ($thirdUser && $thirdUser->access_token) { if ($thirdUser && $thirdUser->access_token) {
// 获取 access_token // 获取 access_token

View File

@ -16,9 +16,11 @@ class OauthThirdCheck implements MiddlewareInterface
$requestData = $request->all(); // 获取请求数据 $requestData = $request->all(); // 获取请求数据
// $customerId = isset($requestData['ad_customer_id']) ? $requestData['ad_customer_id'] : getenv('GOOGLE_ADS_CUSTOMER_ID'); // $customerId = isset($requestData['ad_customer_id']) ? $requestData['ad_customer_id'] : getenv('GOOGLE_ADS_CUSTOMER_ID');
$uid = $request->jwtClaims['uid']; // $uid = $request->jwtClaims['uid'];
$merchant_id = $request->jwtClaims['merchant_id'];
// 查询指定 user_id 的 ThirdUser // 查询指定 user_id 的 ThirdUser
$thirdUserGoogle = ThirdUser::where('user_id', $uid)->where('third_type', 'google')->find(); // $thirdUserGoogle = ThirdUser::where('user_id', $uid)->where('third_type', 'google')->find();
$thirdUserGoogle = ThirdUser::where('merchant_id', $merchant_id)->where('third_type', 'google')->find();
if ($thirdUserGoogle && $thirdUserGoogle->access_token) { if ($thirdUserGoogle && $thirdUserGoogle->access_token) {
// 获取 access_token // 获取 access_token
$request->refresh_token_google = $thirdUserGoogle->access_token; $request->refresh_token_google = $thirdUserGoogle->access_token;
@ -31,12 +33,14 @@ class OauthThirdCheck implements MiddlewareInterface
// dump( $request->access_token,$request->login_customer_id); // dump( $request->access_token,$request->login_customer_id);
} }
} }
$thirdUserFacebook = ThirdUser::where('user_id', $uid)->where('third_type', 'facebook')->find(); // $thirdUserFacebook = ThirdUser::where('user_id', $uid)->where('third_type', 'facebook')->find();
$thirdUserFacebook = ThirdUser::where('merchant_id', $merchant_id)->where('third_type', 'facebook')->find();
if ($thirdUserFacebook && $thirdUserFacebook->access_token) { if ($thirdUserFacebook && $thirdUserFacebook->access_token) {
// 获取 access_token // 获取 access_token
$request->refresh_token_facebook = $thirdUserFacebook->access_token; $request->refresh_token_facebook = $thirdUserFacebook->access_token;
} }
$thirdUserTiktok = ThirdUser::where('user_id', $uid)->where('third_type', 'tiktok')->find(); // $thirdUserTiktok = ThirdUser::where('user_id', $uid)->where('third_type', 'tiktok')->find();
$thirdUserTiktok = ThirdUser::where('merchant_id', $merchant_id)->where('third_type', 'tiktok')->find();
if($thirdUserTiktok && $thirdUserTiktok->access_token){ if($thirdUserTiktok && $thirdUserTiktok->access_token){
$request->refresh_token_tiktok = $thirdUserTiktok->access_token; $request->refresh_token_tiktok = $thirdUserTiktok->access_token;
} }

View File

@ -142,7 +142,7 @@ class BpsAdAccountService
// 获取符合条件的客户ID数组 // 获取符合条件的客户ID数组
$customers = ThirdUserAdvertiser::alias('tua') $customers = ThirdUserAdvertiser::alias('tua')
->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表 ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表
->where('tu.user_id', $options['uid']) // 筛选 third_type 为 google 的记录 ->where('tu.merchant_id', $options['merchant_id']) // 筛选 third_type 为 google 的记录
->where('tu.access_token', '<>', '') // 筛选 access_token 不为空的记录 ->where('tu.access_token', '<>', '') // 筛选 access_token 不为空的记录
->field('tua.advertiser_id as account_id, tu.access_token as refresh_token') // 获取 advertiser_id 字段 ->field('tua.advertiser_id as account_id, tu.access_token as refresh_token') // 获取 advertiser_id 字段
->order('tu.third_type', 'asc') // 获取 advertiser_id 字段 ->order('tu.third_type', 'asc') // 获取 advertiser_id 字段
@ -163,14 +163,13 @@ class BpsAdAccountService
// 获取 Facebook 第三方用户数据 // 获取 Facebook 第三方用户数据
public function getMetaThirdUsers($options = []) public function getMetaThirdUsers($options = [])
{ {
$uid = $options['uid'] ?? null; $merchant_id = $options['merchant_id'] ?? null;
if ($merchant_id) {
if ($uid) {
// 根据 UID 获取符合条件的用户数据 // 根据 UID 获取符合条件的用户数据
$users = ThirdUserAdvertiser::alias('tua') $users = ThirdUserAdvertiser::alias('tua')
->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表 ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表
->where('tu.third_type', 'facebook') // 筛选 third_type 为 facebook 的记录 ->where('tu.third_type', 'facebook') // 筛选 third_type 为 facebook 的记录
->where('tu.user_id', $uid) // 筛选 user_id 的记录 ->where('tu.merchant_id', $merchant_id) // 筛选 merchant_id 的记录
->where('tu.access_token', '<>', '') // 筛选 access_token 不为空的记录 ->where('tu.access_token', '<>', '') // 筛选 access_token 不为空的记录
->field('tu.id, tu.access_token as refresh_token') // 获取相关字段 ->field('tu.id, tu.access_token as refresh_token') // 获取相关字段
->select(); ->select();
@ -195,14 +194,14 @@ class BpsAdAccountService
// 获取 Google 第三方用户数据 // 获取 Google 第三方用户数据
public function getGoogleThirdUsers($options = []) public function getGoogleThirdUsers($options = [])
{ {
$uid = $options['uid'] ?? null; $merchant_id = $options['merchant_id'] ?? null;
if ($uid) { if ($merchant_id) {
// 获取符合条件的用户数据 // 获取符合条件的用户数据
$users = ThirdUserAdvertiser::alias('tua') $users = ThirdUserAdvertiser::alias('tua')
->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表 ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表
->where('tu.third_type', 'google') // 筛选 third_type 为 google 的记录 ->where('tu.third_type', 'google') // 筛选 third_type 为 google 的记录
->where('tu.user_id', $uid) // 筛选 user_id 的记录 ->where('tu.merchant_id', $merchant_id) // 筛选 merchant_id 的记录
->where('tu.access_token', '<>', '') // 筛选 access_token 不为空的记录 ->where('tu.access_token', '<>', '') // 筛选 access_token 不为空的记录
->field('tu.id, tu.access_token as refresh_token') // 获取相关字段 ->field('tu.id, tu.access_token as refresh_token') // 获取相关字段
->select(); ->select();
@ -226,15 +225,15 @@ class BpsAdAccountService
// 获取 TikTok 第三方用户数据 // 获取 TikTok 第三方用户数据
public function getTiktokThirdUsers($options = []) public function getTiktokThirdUsers($options = [])
{ {
$uid = $options['uid'] ?? null; $merchant_id = $options['merchant_id'] ?? null;
if ($uid) { if ($merchant_id) {
// $refreshToken = $options['refresh_token']; // $refreshToken = $options['refresh_token'];
// 获取符合条件的用户数据 // 获取符合条件的用户数据
$users = ThirdUserAdvertiser::alias('tua') $users = ThirdUserAdvertiser::alias('tua')
->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表 ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表
->where('tu.third_type', 'tiktok') // 筛选 third_type 为 tiktok 的记录 ->where('tu.third_type', 'tiktok') // 筛选 third_type 为 tiktok 的记录
->where('tu.user_id', $uid) // 筛选 user_id 的记录 ->where('tu.merchant_id', $merchant_id) // 筛选 merchant_id 的记录
->where('tu.access_token', '<>', '') // 筛选 access_token 不为空的记录 ->where('tu.access_token', '<>', '') // 筛选 access_token 不为空的记录
->field('tu.id, tu.access_token as refresh_token') // 获取相关字段 ->field('tu.id, tu.access_token as refresh_token') // 获取相关字段
->select(); ->select();
@ -258,12 +257,13 @@ class BpsAdAccountService
// 获取所有平台的第三方用户数据 // 获取所有平台的第三方用户数据
public function getAllThirdUsers($options = []) public function getAllThirdUsers($options = [])
{ {
$userId = $options['uid']; // 获取用户ID // $userId = $options['uid']; // 获取用户ID
$merchantId = $options['merchant_id']; // 获取用户ID
// 获取符合条件的用户数据 // 获取符合条件的用户数据
$users = ThirdUserAdvertiser::alias('tua') $users = ThirdUserAdvertiser::alias('tua')
->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表 ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表
->where('tu.user_id', $userId) // 筛选 user_id 的记录 ->where('tu.merchant_id', $merchantId) // 筛选 merchant_id 的记录
->where('tu.access_token', '<>', '') // 筛选 access_token 不为空的记录 ->where('tu.access_token', '<>', '') // 筛选 access_token 不为空的记录
->field('tu.id, tu.third_type, tu.access_token as refresh_token') // 获取相关字段 ->field('tu.id, tu.third_type, tu.access_token as refresh_token') // 获取相关字段
->select(); ->select();

View File

@ -92,7 +92,7 @@ class GoogleOAuthService
} }
//保存或更新某个主体部广告账号 //保存或更新某个主体部广告账号
public function saveThirdUserAdvertiser($customer_id, $third_user_id, $login_customer_id, $customer) public function saveThirdUserAdvertiser($customer_id, $third_user_id, $login_customer_id, $customer, $merchant_id)
{ {
// 确保 customer_id 和 third_user_id 是字符串 // 确保 customer_id 和 third_user_id 是字符串
$customer_id = (string)$customer_id; $customer_id = (string)$customer_id;
@ -103,6 +103,7 @@ class GoogleOAuthService
'advertiser_id' => $customer_id, 'advertiser_id' => $customer_id,
'advertiser_name' => $customer['descriptive_name'], 'advertiser_name' => $customer['descriptive_name'],
'google_login_customer_id' => $login_customer_id, 'google_login_customer_id' => $login_customer_id,
'merchant_id' => $merchant_id,
]; ];
if ($customer['manager'] === true) { if ($customer['manager'] === true) {
$data['google_manager'] = 't'; $data['google_manager'] = 't';
@ -117,35 +118,20 @@ class GoogleOAuthService
// dump($data); // dump($data);
$sql = " $sql = "
INSERT INTO {$tableName} INSERT INTO {$tableName}
(advertiser_id,advertiser_name, doc_,google_login_customer_id,google_manager,google_test_account) (advertiser_id,advertiser_name, doc_,google_login_customer_id,google_manager,google_test_account,merchant_id)
VALUES (:advertiser_id, :advertiser_name,:doc_,:google_login_customer_id,:google_manager,:google_test_account) VALUES (:advertiser_id, :advertiser_name,:doc_,:google_login_customer_id,:google_manager,:google_test_account,:merchant_id)
ON CONFLICT (advertiser_id,doc_) ON CONFLICT (advertiser_id,doc_)
DO UPDATE SET DO UPDATE SET
advertiser_name = EXCLUDED.advertiser_name, advertiser_name = EXCLUDED.advertiser_name,
google_login_customer_id = EXCLUDED.google_login_customer_id, google_login_customer_id = EXCLUDED.google_login_customer_id,
google_manager = EXCLUDED.google_manager, google_manager = EXCLUDED.google_manager,
google_test_account = EXCLUDED.google_test_account google_test_account = EXCLUDED.google_test_account,
merchant_id = EXCLUDED.merchant_id
"; ";
// dump($sql,$data); // dump($sql,$data);
ThinkDb::execute($sql, $data); ThinkDb::execute($sql, $data);
} }
//绑定某个主体的广告账号
public function bindThirdUserAdvertiser($customer_id, $third_user_id)
{
$tableName = 'bps_third_user';
$tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'bps' . $tableName;
$data = [
'id' => $third_user_id,
'user_id' => $customer_id,];
$sql = "
UPDATE {$tableName}
SET user_id = :user_id
WHERE id = :id
";
ThinkDb::execute($sql, $data);
}
public function updateThirdUserDefault($third_user_id, $is_default = 't'): void public function updateThirdUserDefault($third_user_id, $is_default = 't'): void
{ {
$tableName = 'bps_third_user'; $tableName = 'bps_third_user';
@ -181,7 +167,7 @@ class GoogleOAuthService
$tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'bps' . $tableName; $tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'bps' . $tableName;
if (!empty($state)) { if (!empty($state)) {
$thirdUser = ThirdUser::where('user_id', $state)->where('third_type', 'google')->find(); // 获取第一个结果 $thirdUser = ThirdUser::where('merchant_id', $state)->where('third_type', 'google')->find(); // 获取第一个结果
// dump($thirdUser); return ($uid); // dump($thirdUser); return ($uid);
if ($thirdUser) { if ($thirdUser) {
$data['id'] = $thirdUser->id; $data['id'] = $thirdUser->id;
@ -194,11 +180,11 @@ class GoogleOAuthService
WHERE id = :id WHERE id = :id
"; ";
} else { } else {
$data['user_id'] = $state; $data['merchant_id'] = $state;
$sql = " $sql = "
INSERT INTO {$tableName} INSERT INTO {$tableName}
(access_token, is_default, third_type, user_id) (access_token, is_default, third_type, merchant_id)
VALUES (:access_token, :is_default, :third_type, :user_id) VALUES (:access_token, :is_default, :third_type, :merchant_id)
"; ";
} }
} else { } else {
@ -216,21 +202,6 @@ class GoogleOAuthService
$this->queue($options); $this->queue($options);
} }
public function updateRefreshToken($refreshToken)
{
$customer_id = getenv('GOOGLE_ADS_CUSTOMER_ID');
//暂时update进行绑定
$tableName = "bps.bps_third_user";
// $sql = "UPDATE {$tableName} SET access_token = :access_token WHERE random_code = :random_code";
$sql = "UPDATE {$tableName} SET access_token = :access_token WHERE user_id = :user_id";
$data = [
'access_token' => $refreshToken, // 这里的 $accessToken 是您想要匹配的值
'user_id' => $customer_id, // 这里的 $accessToken 是您想要匹配的值
];
// 执行 SQL 语句
$result = ThinkDb::execute($sql, $data);
}
public function revokeToken($accessToken, $third_user_id) public function revokeToken($accessToken, $third_user_id)
{ {

View File

@ -153,7 +153,6 @@ Route::group('/googleads', function () {
]); ]);
Route::group('/customer', function () { Route::group('/customer', function () {
Route::post('/list', [CustomerController::class, 'getCustomerList']); Route::post('/list', [CustomerController::class, 'getCustomerList']);
Route::post('/bind', [CustomerController::class, 'bindAdvertiser']);
Route::post('/list_resource', [CustomerController::class, 'accessibleCustomers']); Route::post('/list_resource', [CustomerController::class, 'accessibleCustomers']);
Route::post('/list_tree', [CustomerController::class, 'accountHierarchy']); Route::post('/list_tree', [CustomerController::class, 'accountHierarchy']);
Route::post('/access_role', [CustomerController::class, 'accountAccess']); Route::post('/access_role', [CustomerController::class, 'accountAccess']);