|
|
@@ -3,31 +3,165 @@
|
|
|
namespace app\adapter;
|
|
|
|
|
|
// 头条-适配器
|
|
|
+
|
|
|
+use app\process\Http;
|
|
|
use GuzzleHttp\Client;
|
|
|
+use support\think\Db;
|
|
|
|
|
|
class ToutiaoAdapter
|
|
|
{
|
|
|
- public function OAuth($info)
|
|
|
+ public function OAuth($callback)
|
|
|
{
|
|
|
+
|
|
|
+ $authCode = $callback['auth_code'];
|
|
|
+ $cmid = $callback['state']['cmid'];
|
|
|
+
|
|
|
+ $Account = Db::connect('db_advert')->name('ad_advertiser_app')->select()->toArray();
|
|
|
+ $Account = array_column($Account, null, 'id');
|
|
|
+
|
|
|
+ // 1. 根据cmid获取appid和secret
|
|
|
+ $appId = $Account[$cmid]['app_id'];
|
|
|
+ $secret = $Account[$cmid]['app_secret'];
|
|
|
+ $tokenUrl = $Account[$cmid]['token_url'];
|
|
|
+
|
|
|
+ // 2. 授权access_token 和 refresh_token
|
|
|
+ $url = $tokenUrl;
|
|
|
+ $header = ['Content-Type: application/json'];
|
|
|
+ $request_data = json_encode([
|
|
|
+ 'app_id'=>$appId,
|
|
|
+ 'secret'=>$secret,
|
|
|
+ 'grant_type'=>'auth_code',
|
|
|
+ 'auth_code'=>$authCode,
|
|
|
+ ]);
|
|
|
+ $httpClient = new Client(['timeout' => 10]);
|
|
|
+ $res = $httpClient->request('POST', $url, [
|
|
|
+ 'json' => $request_data,
|
|
|
+ 'headers' => $header,
|
|
|
+ ]);
|
|
|
+ $result = json_decode($res->getBody(), true);
|
|
|
+ $data = $result['data'] ?? [];
|
|
|
+
|
|
|
// 执行验证callback, 返回授权后的字段,跟数据库对应
|
|
|
return [
|
|
|
- "advertiser_id" => "",
|
|
|
- "advertiser_name" => "",
|
|
|
- "access_token" => "",
|
|
|
- "refresh_token" => "",
|
|
|
- "expire_time" => "",
|
|
|
+ "advertiser_id" => $data['advertiser_id'],
|
|
|
+ "advertiser_ids" => $data['advertiser_ids'],
|
|
|
+ "access_token" => $data['access_token'],
|
|
|
+ "refresh_token" => $data['refresh_token'],
|
|
|
+ "refresh_token_expires_in" => $data['refresh_token_expires_in'],
|
|
|
+ "expires_in" => $data['expires_in'],
|
|
|
+ "cmid" => $cmid,
|
|
|
+
|
|
|
];
|
|
|
}
|
|
|
+
|
|
|
+ // 写入授权结果
|
|
|
+ public function writeOAuthResult($data)
|
|
|
+ {
|
|
|
+ // 写入ad_advertiser_bm表
|
|
|
+ if ($data['access_token']){
|
|
|
+ $accounts = $data['advertiser_ids'];
|
|
|
+ foreach($accounts as $account_id){
|
|
|
+ $record = Db::connect('db_advert')->name('ad_advertiser_bm')
|
|
|
+ ->where('advertiser_id', $account_id)
|
|
|
+ ->find();
|
|
|
+
|
|
|
+ if (!$record){
|
|
|
+ $record = [
|
|
|
+ 'advertiser_id' => $account_id,
|
|
|
+ 'advertiser_name' => $account_id,
|
|
|
+ 'access_token' => $data['access_token'],
|
|
|
+ 'refresh_token' => $data['refresh_token'],
|
|
|
+ ];
|
|
|
+ Db::connect('db_advert')->name('ad_advertiser_bm')->insert($record);
|
|
|
+ }else{
|
|
|
+ Db::connect('db_advert')->name('ad_advertiser_bm')->where('advertiser_id', $account_id)->update([
|
|
|
+ 'access_token' => $data['access_token'],
|
|
|
+ 'refresh_token' => $data['refresh_token'],
|
|
|
+ 'advertiser_id' => $account_id,
|
|
|
+ 'advertiser_name' => $account_id,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
// 获取子账号列表
|
|
|
- public function getSubAccounts($pmid)
|
|
|
+ public function getSubAccounts($oathInfo)
|
|
|
{
|
|
|
+ $advertiserId = $oathInfo['advertiser_id'];
|
|
|
+ $accessToken = $oathInfo['access_token'];
|
|
|
+ $appId = $oathInfo['app_id'];
|
|
|
+
|
|
|
|
|
|
+ // 根据授权返回的 advertiser_id 和 access_token 获取子账号列表
|
|
|
+ $url = 'https://ad.oceanengine.com/open_api/2/majordomo/advertiser/select/';
|
|
|
+ $header = [
|
|
|
+ 'Access-Token' => $accessToken,
|
|
|
+ 'Content-Type' => 'application/json',
|
|
|
+ ];
|
|
|
+ $requestData = [
|
|
|
+ 'advertiser_id' => $advertiserId,
|
|
|
+ ];
|
|
|
+ $url = $url."?".http_build_query($requestData);
|
|
|
+ $httpClient = new Client(['timeout' => 10]);
|
|
|
+ $res = $httpClient->request('GET', $url, [
|
|
|
+ 'headers' => $header,
|
|
|
+ ]);
|
|
|
+ $result = json_decode($res->getBody(), true);
|
|
|
+ $list = $result['data']['list'] ?? [];
|
|
|
+
|
|
|
+ $toutiaoAccountList = Db::connect('db_advert')->name('ad_advertiser_list')->where('media_id', 1)->select()->toArray();
|
|
|
+ $toutiaoAccountMap = array_column($toutiaoAccountList, null, 'advertiser_id');
|
|
|
+
|
|
|
+
|
|
|
+ foreach($list as $val){
|
|
|
+ if(!isset($toutiaoAccountMap[$val['advertiser_id']])){
|
|
|
+ Db::connect('db_advert')->name('ad_advertiser_list')->insert([
|
|
|
+ 'advertiser_id' => $val['advertiser_id'],
|
|
|
+ 'advertiser_name' => $val['advertiser_name'],
|
|
|
+ 'app_id'=>$appId,
|
|
|
+ 'bmid' => $advertiserId,
|
|
|
+ 'media_id' => 1,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 刷新 token
|
|
|
- public function refreshToken($pmid)
|
|
|
+ public function refreshToken($data)
|
|
|
{
|
|
|
-
|
|
|
+ $cmid = $data['cmid'];
|
|
|
+ $advertiserId = $data['advertiser_id'];
|
|
|
+ $accountApp = Db::connect('db_advert')->name('ad_advertiser_app')->where('id', $cmid)->find();
|
|
|
+ $refreshToken = Db::connect('db_advert')->name('ad_advertiser_bm')->where('advertiser_id', $advertiserId)->value('refresh_token');
|
|
|
+ if($refreshToken){
|
|
|
+ $url = $accountApp['refresh_url'];
|
|
|
+ $header = ['Content-Type: application/json'];
|
|
|
+ $request_data = json_encode([
|
|
|
+ 'app_id'=>$accountApp['app_id'],
|
|
|
+ 'secret'=>$accountApp['app_secret'],
|
|
|
+ 'grant_type'=>'refresh_token',
|
|
|
+ 'refresh_token'=>$refreshToken
|
|
|
+ ]);
|
|
|
+ $httpClient = new Client(['timeout' => 10]);
|
|
|
+ $res = $httpClient->request('POST', $url, [
|
|
|
+ 'json' => $request_data,
|
|
|
+ 'headers' => $header,
|
|
|
+ ]);
|
|
|
+ $result = json_decode($res->getBody(), true);
|
|
|
+ $data = $result['data'] ?? [];
|
|
|
+ // 更新管家的token相关
|
|
|
+ if ($data['access_token']){
|
|
|
+ Db::connect('db_advert')->name('ad_advertiser_bm')->where('advertiser_id', $advertiserId)->update([
|
|
|
+ 'access_token' => $data['access_token'],
|
|
|
+ 'refresh_token' => $data['refresh_token'],
|
|
|
+ 'refresh_token_expires_in' => $data['refresh_token_expires_in'],
|
|
|
+ 'expires_in' => $data['expires_in'],
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $data;
|
|
|
}
|
|
|
|
|
|
// 获取小时消耗
|