meta code add
This commit is contained in:
parent
0818d263a9
commit
ce93c4cc48
@ -3,12 +3,12 @@
|
||||
namespace app\controller;
|
||||
|
||||
//use Webman\Http\Response;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
|
||||
class Controller
|
||||
{
|
||||
// 可以加入一些公共方法
|
||||
protected function successResponse($data)
|
||||
protected function successResponse($data): Response
|
||||
{
|
||||
return Json([
|
||||
'code' => 200,
|
||||
@ -17,7 +17,7 @@ class Controller
|
||||
]);
|
||||
}
|
||||
|
||||
protected function errorResponse($message)
|
||||
protected function errorResponse($message): Response
|
||||
{
|
||||
return Json([
|
||||
'code' => 400,
|
||||
|
@ -19,7 +19,7 @@ class GoogleAdsController extends Controller
|
||||
}
|
||||
|
||||
// 创建广告预算
|
||||
public function createCampaignBudget($request)
|
||||
public function createCampaignBudget($request): \support\Response
|
||||
{
|
||||
$data = $request->post();
|
||||
$customerId = $data['customer_id'];
|
||||
@ -40,7 +40,7 @@ class GoogleAdsController extends Controller
|
||||
}
|
||||
|
||||
// 创建广告系列
|
||||
public function createCampaign($request)
|
||||
public function createCampaign($request): \support\Response
|
||||
{
|
||||
$data = $request->post();
|
||||
$customerId = $data['customer_id'];
|
||||
@ -61,7 +61,7 @@ class GoogleAdsController extends Controller
|
||||
}
|
||||
|
||||
// 创建广告组
|
||||
public function createAdGroup($request)
|
||||
public function createAdGroup($request): \support\Response
|
||||
{
|
||||
$data = $request->post();
|
||||
$customerId = $data['customer_id'];
|
||||
@ -84,7 +84,7 @@ class GoogleAdsController extends Controller
|
||||
}
|
||||
|
||||
// 创建广告
|
||||
public function createAd($request)
|
||||
public function createAd($request): \support\Response
|
||||
{
|
||||
$data = $request->post();
|
||||
$customerId = $data['customer_id'];
|
||||
|
77
app/controller/MetaController.php
Normal file
77
app/controller/MetaController.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace app\controller;
|
||||
|
||||
use app\service\MetaBusinessService;
|
||||
use support\Response;
|
||||
|
||||
//use Webman\Http\Response;
|
||||
|
||||
class MetaController extends Controller
|
||||
{
|
||||
protected $metaBusinessService;
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// 初始化各个服务
|
||||
$this->metaBusinessService = new MetaBusinessService();
|
||||
}
|
||||
|
||||
// 获取广告账户
|
||||
public function getBusinessAccounts(): Response
|
||||
{
|
||||
$accounts = $this->metaBusinessService->getBusinessAccounts();
|
||||
return $this->successResponse(['data' => $accounts]);
|
||||
}
|
||||
|
||||
// 保存广告账户
|
||||
public function saveBusinessAccounts(): Response
|
||||
{
|
||||
$accounts = $this->metaBusinessService->getBusinessAccounts();
|
||||
$this->metaBusinessService->saveBusinessAccounts($accounts);
|
||||
return $this->successResponse(['message' => 'Business accounts saved successfully']);
|
||||
}
|
||||
|
||||
// 获取广告系列列表
|
||||
public function getCampaigns(): Response
|
||||
{
|
||||
$campaigns = $this->metaBusinessService->getCampaigns();
|
||||
return $this->successResponse(['data' => $campaigns]);
|
||||
}
|
||||
|
||||
// 创建广告系列
|
||||
public function createCampaign($name, $objective, $status): Response
|
||||
{
|
||||
$campaign = $this->metaBusinessService->createCampaign($name, $objective, $status);
|
||||
return $this->successResponse(['data' => $campaign]);
|
||||
}
|
||||
|
||||
// 获取广告组列表
|
||||
public function getAdGroups($campaignId): Response
|
||||
{
|
||||
$adGroups = $this->metaBusinessService->getAdGroups($campaignId);
|
||||
return $this->successResponse(['data' => $adGroups]);
|
||||
}
|
||||
|
||||
// 创建广告组
|
||||
public function createAdGroup($campaignId, $name, $status): Response
|
||||
{
|
||||
$adGroup = $this->metaBusinessService->createAdGroup($campaignId, $name, $status);
|
||||
return $this->successResponse(['data' => $adGroup]);
|
||||
}
|
||||
|
||||
// 获取广告列表
|
||||
public function getAds($adGroupId): Response
|
||||
{
|
||||
$ads = $this->metaBusinessService->getAds($adGroupId);
|
||||
return $this->successResponse(['data' => $ads]);
|
||||
}
|
||||
|
||||
// 创建广告
|
||||
public function createAd($adGroupId, $name, $status): Response
|
||||
{
|
||||
$ad = $this->metaBusinessService->createAd($adGroupId, $name, $status);
|
||||
return $this->successResponse(['data' => $ad]);
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Migrator;
|
||||
use think\migration\db\Column;
|
||||
|
||||
class CreateMetaBusinessAccountsTable extends Migrator
|
||||
{
|
||||
public function change()
|
||||
{
|
||||
$table = $this->table('ad_meta_business_accounts');
|
||||
$table->addColumn('account_name', 'string', ['limit' => 255])
|
||||
->addColumn('account_id', 'string', ['limit' => 255, 'unique' => true])
|
||||
->addColumn('status', 'string', ['limit' => 50])
|
||||
->addColumn('created_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP'])
|
||||
->addColumn('updated_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'update' => 'CURRENT_TIMESTAMP'])
|
||||
->create();
|
||||
}
|
||||
}
|
21
app/migrations/2024_12_11_02_create_meta_campaigns_table.php
Normal file
21
app/migrations/2024_12_11_02_create_meta_campaigns_table.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Migrator;
|
||||
use think\migration\db\Column;
|
||||
|
||||
class CreateMetaCampaignsTable extends Migrator
|
||||
{
|
||||
public function change()
|
||||
{
|
||||
$table = $this->table('ad_meta_campaigns');
|
||||
$table->addColumn('campaign_name', 'string', ['limit' => 255])
|
||||
->addColumn('campaign_id', 'string', ['limit' => 255, 'unique' => true])
|
||||
->addColumn('status', 'string', ['limit' => 50])
|
||||
->addColumn('objective', 'string', ['limit' => 100])
|
||||
->addColumn('business_account_id', 'integer')
|
||||
->addForeignKey('business_account_id', 'meta_business_accounts', 'id', ['delete' => 'CASCADE'])
|
||||
->addColumn('created_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP'])
|
||||
->addColumn('updated_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'update' => 'CURRENT_TIMESTAMP'])
|
||||
->create();
|
||||
}
|
||||
}
|
20
app/migrations/2024_12_11_03_create_meta_ad_groups_table.php
Normal file
20
app/migrations/2024_12_11_03_create_meta_ad_groups_table.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Migrator;
|
||||
use think\migration\db\Column;
|
||||
|
||||
class CreateMetaAdGroupsTable extends Migrator
|
||||
{
|
||||
public function change()
|
||||
{
|
||||
$table = $this->table('ad_meta_ad_groups');
|
||||
$table->addColumn('ad_group_name', 'string', ['limit' => 255])
|
||||
->addColumn('ad_group_id', 'string', ['limit' => 255, 'unique' => true])
|
||||
->addColumn('status', 'string', ['limit' => 50])
|
||||
->addColumn('campaign_id', 'integer')
|
||||
->addForeignKey('campaign_id', 'meta_campaigns', 'id', ['delete' => 'CASCADE'])
|
||||
->addColumn('created_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP'])
|
||||
->addColumn('updated_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'update' => 'CURRENT_TIMESTAMP'])
|
||||
->create();
|
||||
}
|
||||
}
|
20
app/migrations/2024_12_11_04_create_meta_ads_table.php
Normal file
20
app/migrations/2024_12_11_04_create_meta_ads_table.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Migrator;
|
||||
use think\migration\db\Column;
|
||||
|
||||
class CreateMetaAdsTable extends Migrator
|
||||
{
|
||||
public function change()
|
||||
{
|
||||
$table = $this->table('ad_meta_ads');
|
||||
$table->addColumn('ad_name', 'string', ['limit' => 255])
|
||||
->addColumn('ad_id', 'string', ['limit' => 255, 'unique' => true])
|
||||
->addColumn('status', 'string', ['limit' => 50])
|
||||
->addColumn('ad_group_id', 'integer')
|
||||
->addForeignKey('ad_group_id', 'meta_ad_groups', 'id', ['delete' => 'CASCADE'])
|
||||
->addColumn('created_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP'])
|
||||
->addColumn('updated_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'update' => 'CURRENT_TIMESTAMP'])
|
||||
->create();
|
||||
}
|
||||
}
|
27
app/model/MetaAd.php
Normal file
27
app/model/MetaAd.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace app\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class MetaAd extends Model
|
||||
{
|
||||
// 数据表名称
|
||||
protected $table = 'ad_meta_ads';
|
||||
|
||||
// 主键字段
|
||||
protected $pk = 'id';
|
||||
|
||||
// 定义时间戳字段
|
||||
protected $createTime = 'created_at';
|
||||
protected $updateTime = 'updated_at';
|
||||
|
||||
// 不自动写入更新时间字段
|
||||
protected $autoWriteTimestamp = true;
|
||||
|
||||
// 关联广告组
|
||||
public function adGroup()
|
||||
{
|
||||
return $this->belongsTo('MetaAdGroup', 'ad_group_id');
|
||||
}
|
||||
}
|
27
app/model/MetaAdGroup.php
Normal file
27
app/model/MetaAdGroup.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace app\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class MetaAdGroup extends Model
|
||||
{
|
||||
// 数据表名称
|
||||
protected $table = 'ad_meta_ad_groups';
|
||||
|
||||
// 主键字段
|
||||
protected $pk = 'id';
|
||||
|
||||
// 定义时间戳字段
|
||||
protected $createTime = 'created_at';
|
||||
protected $updateTime = 'updated_at';
|
||||
|
||||
// 不自动写入更新时间字段
|
||||
protected $autoWriteTimestamp = true;
|
||||
|
||||
// 关联广告系列
|
||||
public function campaign()
|
||||
{
|
||||
return $this->belongsTo('MetaCampaign', 'campaign_id');
|
||||
}
|
||||
}
|
21
app/model/MetaBusinessAccount.php
Normal file
21
app/model/MetaBusinessAccount.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace app\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class MetaBusinessAccount extends Model
|
||||
{
|
||||
// 数据表名称
|
||||
protected $table = 'ad_meta_business_accounts';
|
||||
|
||||
// 主键字段
|
||||
protected $pk = 'id';
|
||||
|
||||
// 定义时间戳字段
|
||||
protected $createTime = 'created_at';
|
||||
protected $updateTime = 'updated_at';
|
||||
|
||||
// 不自动写入更新时间字段
|
||||
protected $autoWriteTimestamp = true;
|
||||
}
|
27
app/model/MetaCampaign.php
Normal file
27
app/model/MetaCampaign.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace app\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class MetaCampaign extends Model
|
||||
{
|
||||
// 数据表名称
|
||||
protected $table = 'ad_meta_campaigns';
|
||||
|
||||
// 主键字段
|
||||
protected $pk = 'id';
|
||||
|
||||
// 定义时间戳字段
|
||||
protected $createTime = 'created_at';
|
||||
protected $updateTime = 'updated_at';
|
||||
|
||||
// 不自动写入更新时间字段
|
||||
protected $autoWriteTimestamp = true;
|
||||
|
||||
// 关联广告账户
|
||||
public function businessAccount()
|
||||
{
|
||||
return $this->belongsTo('MetaBusinessAccount', 'business_account_id');
|
||||
}
|
||||
}
|
@ -1,9 +1,21 @@
|
||||
<?php
|
||||
use app\controller\GoogleAdsController;
|
||||
use app\controller\MetaController;
|
||||
|
||||
return [
|
||||
'POST /google-ads/create-budget' => [GoogleAdsController::class, 'createCampaignBudget'],
|
||||
'POST /google-ads/create-campaign' => [GoogleAdsController::class, 'createCampaign'],
|
||||
'POST /google-ads/create-adgroup' => [GoogleAdsController::class, 'createAdGroup'],
|
||||
'POST /google-ads/create-ad' => [GoogleAdsController::class, 'createAd'],
|
||||
|
||||
// Meta API Routes
|
||||
'POST /meta/business/accounts' => [MetaController::class, 'getBusinessAccounts'],
|
||||
'POST /meta/business/accounts/save' => [MetaController::class, 'saveBusinessAccounts'],
|
||||
'GET /meta/campaigns' => [MetaController::class, 'getCampaigns'],
|
||||
'POST /meta/campaign/create' => [MetaController::class, 'createCampaign'],
|
||||
'GET /meta/adgroups' => [MetaController::class, 'getAdGroups'],
|
||||
'POST /meta/adgroup/create' => [MetaController::class, 'createAdGroup'],
|
||||
'GET /meta/ads' => [MetaController::class, 'getAds'],
|
||||
'POST /meta/ad/create' => [MetaController::class, 'createAd'],
|
||||
|
||||
];
|
||||
|
132
app/service/MetaBusinessService.php
Normal file
132
app/service/MetaBusinessService.php
Normal file
@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace app\service;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class MetaBusinessService
|
||||
{
|
||||
protected $client;
|
||||
protected $accessToken;
|
||||
protected $businessId;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = new Client();
|
||||
$this->accessToken = getenv('META_ACCESS_TOKEN');
|
||||
$this->businessId = getenv('META_BUSINESS_ID');
|
||||
}
|
||||
|
||||
// 获取广告账户
|
||||
public function getBusinessAccounts()
|
||||
{
|
||||
$url = "https://graph.facebook.com/v15.0/{$this->businessId}/owned_ad_accounts";
|
||||
$response = $this->client->get($url, [
|
||||
'query' => [
|
||||
'access_token' => $this->accessToken,
|
||||
]
|
||||
]);
|
||||
|
||||
return json_decode($response->getBody()->getContents(), true)['data'] ?? [];
|
||||
}
|
||||
|
||||
// 保存广告账户到数据库
|
||||
public function saveBusinessAccounts($accounts)
|
||||
{
|
||||
foreach ($accounts as $account) {
|
||||
// 假设你已经定义了 MetaBusinessAccount 模型
|
||||
MetaBusinessAccount::create([
|
||||
'account_name' => $account['name'],
|
||||
'account_id' => $account['id'],
|
||||
'status' => $account['status'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取广告系列
|
||||
public function getCampaigns()
|
||||
{
|
||||
$url = "https://graph.facebook.com/v15.0/{$this->businessId}/campaigns";
|
||||
$response = $this->client->get($url, [
|
||||
'query' => [
|
||||
'access_token' => $this->accessToken,
|
||||
]
|
||||
]);
|
||||
|
||||
return json_decode($response->getBody()->getContents(), true)['data'] ?? [];
|
||||
}
|
||||
|
||||
// 创建广告系列
|
||||
public function createCampaign($name, $objective, $status)
|
||||
{
|
||||
$url = "https://graph.facebook.com/v15.0/{$this->businessId}/campaigns";
|
||||
$response = $this->client->post($url, [
|
||||
'json' => [
|
||||
'name' => $name,
|
||||
'objective' => $objective,
|
||||
'status' => $status,
|
||||
'access_token' => $this->accessToken,
|
||||
]
|
||||
]);
|
||||
|
||||
return json_decode($response->getBody()->getContents(), true);
|
||||
}
|
||||
|
||||
// 获取广告组
|
||||
public function getAdGroups($campaignId)
|
||||
{
|
||||
$url = "https://graph.facebook.com/v15.0/{$campaignId}/adsets";
|
||||
$response = $this->client->get($url, [
|
||||
'query' => [
|
||||
'access_token' => $this->accessToken,
|
||||
]
|
||||
]);
|
||||
|
||||
return json_decode($response->getBody()->getContents(), true)['data'] ?? [];
|
||||
}
|
||||
|
||||
// 创建广告组
|
||||
public function createAdGroup($campaignId, $name, $status)
|
||||
{
|
||||
$url = "https://graph.facebook.com/v15.0/{$this->businessId}/adsets";
|
||||
$response = $this->client->post($url, [
|
||||
'json' => [
|
||||
'name' => $name,
|
||||
'status' => $status,
|
||||
'campaign_id' => $campaignId,
|
||||
'access_token' => $this->accessToken,
|
||||
]
|
||||
]);
|
||||
|
||||
return json_decode($response->getBody()->getContents(), true);
|
||||
}
|
||||
|
||||
// 获取广告
|
||||
public function getAds($adGroupId)
|
||||
{
|
||||
$url = "https://graph.facebook.com/v15.0/{$adGroupId}/ads";
|
||||
$response = $this->client->get($url, [
|
||||
'query' => [
|
||||
'access_token' => $this->accessToken,
|
||||
]
|
||||
]);
|
||||
|
||||
return json_decode($response->getBody()->getContents(), true)['data'] ?? [];
|
||||
}
|
||||
|
||||
// 创建广告
|
||||
public function createAd($adGroupId, $name, $status)
|
||||
{
|
||||
$url = "https://graph.facebook.com/v15.0/{$this->businessId}/ads";
|
||||
$response = $this->client->post($url, [
|
||||
'json' => [
|
||||
'name' => $name,
|
||||
'status' => $status,
|
||||
'adset_id' => $adGroupId,
|
||||
'access_token' => $this->accessToken,
|
||||
]
|
||||
]);
|
||||
|
||||
return json_decode($response->getBody()->getContents(), true);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user