123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692 |
- <?php
- namespace App\Support;
- use App\Log;
- use App\Service\AccessTokenService;
- use App\Service\HttpService;
- class DouYinApi
- {
- /**
- * 生成签名
- * @param $appId string 小程序app_id
- * @param $method string HTTP请求方法,如:POST,GET,PUT 等。注意请使用大写。
- * @param $url string 获取请求的开放平台接口的绝对 URL,并去除域名部分得到参与签名的 URI。 URI 必须以斜杠字符“/”开头。
- * @param $body string 请求报文主体;请求方法为GET时,报文主体为空。当请求方法为POST或PUT时,请使用报文内容。
- * @param $timestamp string 发起请求时系统的当前时间戳,即格林威治时间 1970年01月01日 00时00分00秒(北京时间 1970年01月01日 08时00分00秒)起至现在的总秒数,作为请求时间戳。「开放平台」会拒绝处理一个小时之前发起的请求。
- * @param $nonce_str string 任意生成一个随机字符串(长度不限制),以保证相同时间相同参数发起的请求签名值不一样。
- * @return string|null
- */
- private static function makeSign($appId, $method, $url, $body, $timestamp, $nonce_str) {
- //method内容必须大写,如GET、POST,uri不包含域名,必须以'/'开头
- $text = $method . "\n" . $url . "\n" . $timestamp . "\n" . $nonce_str . "\n" . $body . "\n";
- if(!file_exists(public_path('key/'.$appId."_private_key.pem"))) {
- return null;
- }
- $priKey = file_get_contents(public_path('key/'.$appId."_private_key.pem"));
- $privateKey = openssl_get_privatekey($priKey, '');
- openssl_sign($text, $sign, $privateKey, OPENSSL_ALGO_SHA256);
- $sign = base64_encode($sign);
- openssl_free_key($privateKey);
- return $sign;
- }
- /**
- * 验签 (接收支付回调使用)
- * @param $appId
- * @param $http_body
- * @param $timestamp
- * @param $nonce_str
- * @param $sign
- * @return bool|null
- */
- private static function verify($appId, $http_body, $timestamp, $nonce_str, $sign)
- {
- $data = $timestamp . "\n" . $nonce_str . "\n" . $http_body . "\n";
- $publicKey = file_get_contents(public_path("key/".$appId."platform_public_key.pem"));
- if (!$publicKey) {
- return null;
- }
- $res = openssl_get_publickey($publicKey); // 注意验签时publicKey使用平台公钥而非应用公钥
- $result = (bool)openssl_verify($data, base64_decode($sign), $res, OPENSSL_ALGO_SHA256);
- openssl_free_key($res);
- return $result; //bool
- }
- private static function commonRequest($appId, $requestUrl, $uri, $method, $body, $header = [], $retry = 0) {
- $timestamp = time();
- $nonce_str = md5(time().rand(10000,99999));
- $sign = DouYinApi::makeSign($appId, $method, $uri, json_encode($body, 256), $timestamp, $nonce_str);
- $headers = [
- 'Byte-Authorization: SHA256-RSA2048 appid="'.$appId.'",nonce_str="'.$nonce_str.'",timestamp="'.$timestamp.'",key_version="1"'.',signature="'.$sign.'"'
- ];
- if(!empty($header)){
- $headers = array_merge($headers, $header);
- }
- switch($method) {
- case 'POST':
- $response = HttpService::httpPost($requestUrl.$uri, json_encode($body, 256), true, $headers);
- break;
- case 'GET':
- $response = HttpService::httpGet($requestUrl.$uri, $headers);
- break;
- default:
- $response = null;
- break;
- }
- if($response === false) {
- if($retry <= 3) {
- $retry++;
- return self::commonRequest($appId, $requestUrl, $uri, $method, $body, $header, $retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序请求api异常', json_encode([
- 'app_id' => $appId,
- 'uri' => $uri,
- 'body' => $body,
- 'header' => $headers,
- 'response' => $response
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- $responseData = json_decode($response, 1);
- Log::logInfo($uri, [
- 'appid' => $appId,
- 'body' => $body,
- 'header' => $headers,
- 'response' => $responseData
- ], 'douYinApi');
- return $responseData;
- }
- # 获取小程序接口调用凭证 access_token
- public static function getAccessToken($appId, $retry = 0) {
- $requestUrl = 'https://developer.toutiao.com';
- $uri = config('douyin.getAccessToken');
- $params = [
- 'app_id' => $appId,
- 'secret' => config('douyin.app_info')[$appId]['secret'] ?? '',
- 'grant_type' => 'client_credential',
- ];
- $responseData = self::commonRequest($appId, $requestUrl, $uri, 'POST', $params);
- if(isset($responseData['err_no']) && 0 != $responseData['err_no']) {
- if($retry <= 3) {
- $retry++;
- return self::getAccessToken($appId, $retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序获取accessToken异常', json_encode([
- 'app_id' => $appId,
- 'uri' => $uri,
- 'body' => $params,
- 'response' => $responseData
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- return $responseData['data'] ?? [];
- }
- public static function getClientToken($appId, $retry = 0) {
- $requestUrl = 'https://open.douyin.com';
- $uri = config('douyin.getClientToken');
- $params = [
- 'client_key' => $appId,
- 'client_secret' => config('douyin.app_info')[$appId]['secret'] ?? '',
- 'grant_type' => 'client_credential',
- ];
- $responseData = self::commonRequest($appId, $requestUrl, $uri, 'POST', $params);
- if(isset($responseData['data']['error_code']) && 0 != $responseData['data']['error_code']) {
- if($retry <= 3) {
- $retry++;
- return self::getClientToken($appId, $retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序获取clientToken异常', json_encode([
- 'app_id' => $appId,
- 'uri' => $uri,
- 'body' => $params,
- 'response' => $responseData
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- return $responseData['data'] ?? [];
- }
- # 根据小程序code获取用户信息
- public static function code2Session($appId, $code, $anonymousCode = null, $retry = 0) {
- $requestUrl = 'https://developer.toutiao.com';
- $uri = config('douyin.getSession');
- if(empty($code) && empty($anonymousCode)) {
- return 1102;
- }
- $params = [
- 'appid' => $appId,
- 'secret' => config('douyin.app_info')[$appId]['secret'] ?? '',
- 'code' => $code ?? '',
- 'anonymous_code' => $anonymousCode ?? '',
- ];
- $responseData = self::commonRequest($appId, $requestUrl, $uri, 'POST', $params);
- if(isset($responseData['err_no']) && 0 != $responseData['err_no']) {
- if($retry <= 3) {
- $retry++;
- return self::code2Session($appId, $code, $anonymousCode, $retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序请求codeSession异常', json_encode([
- 'app_id' => $appId,
- 'code' => $code,
- 'anonymous_code' => $anonymousCode,
- 'uri' => $uri,
- 'body' => $params,
- 'response' => $responseData
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- return $responseData['data'] ?? [];
- }
- # 上传图片或者视频到抖音内容库
- public static function uploadMedia($appId, $type, $fileUrl, $videoTitle, $videoDesc, $format, $retry = 0) {
- $requestUrl = 'https://open.douyin.com';
- $uri = config('douyin.uploadMedia');
- $params = [
- 'resource_type' => $type,
- 'ma_app_id' => $appId,
- ];
- switch($type) {
- case 1:
- $params['video_meta'] = [
- 'url' => $fileUrl,
- 'title' => $videoTitle,
- 'description' => $videoDesc,
- 'format' => $format
- ];
- break;
- case 2:
- $params['image_meta'] = [
- 'url' => $fileUrl
- ];
- }
- $accessToken = AccessTokenService::getAccessToken($appId);
- $header = [
- 'access-token: '.$accessToken
- ];
- $responseData = self::commonRequest($appId, $requestUrl, $uri, 'POST', $params, $header);
- if(isset($responseData['err_no']) && 0 != $responseData['err_no']) {
- if($retry <= 3) {
- $retry++;
- return self::uploadMedia($appId, $type, $fileUrl, $videoTitle, $videoDesc, $format, $retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序请求上传素材异常', json_encode([
- 'app_id' => $appId,
- 'file_url' => $fileUrl,
- 'type' => $type,
- 'title' => $videoTitle,
- 'desc' => $videoDesc,
- 'uri' => $uri,
- 'body' => $params,
- 'response' => $responseData
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- return $responseData['data'] ?? [];
- }
- # 查询视频状态
- public static function queryVideo($appId, $videoIdType, $openVideoId, $dyCloudId, $retry = 0) {
- $requestUrl = 'https://open.douyin.com';
- $uri = config('douyin.queryVideo');
- $accessToken = AccessTokenService::getAccessToken($appId);
- $header = [
- 'access-token: '.$accessToken
- ];
- $params = [
- 'video_id_type' => $videoIdType, // 1通过open_video_id 查询 2通过抖音云的视频vid查询
- 'ma_app_id' => $appId,
- 'open_video_id' => $openVideoId,
- 'dy_cloud_id' => $dyCloudId
- ];
- $responseData = self::commonRequest($appId, $requestUrl, $uri, 'POST', $params, $header);
- if(isset($responseData['err_no']) && 0 != $responseData['err_no']) {
- if($retry <= 3) {
- $retry++;
- return self::queryVideo($appId, $videoIdType, $openVideoId, $dyCloudId,$retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序获取视频状态结果异常', json_encode([
- 'app_id' => $appId,
- 'type' => $videoIdType,
- 'open_video_id' => $openVideoId,
- 'dy_cloud_id' => $dyCloudId,
- 'uri' => $uri,
- 'body' => $params,
- 'response' => $responseData
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- return $responseData['data'] ?? [];
- }
- # 创建短剧
- public static function createAlbum($appId, $title, $seqNum, $coverList, $year, $albumStatus, $recommendation
- , $desp, $tagList, $qualification, $recordInfo, $recordAuditInfo, $retry = 0) {
- $requestUrl = 'https://open.douyin.com';
- $uri = config('douyin.createAlbum');
- $accessToken = AccessTokenService::getAccessToken($appId);
- $header = [
- 'access-token: '.$accessToken
- ];
- $params = [
- 'ma_app_id' => $appId,
- 'album_info' => [
- 'title' => $title,
- 'seq_num' => $seqNum,
- 'cover_list' => $coverList,
- 'year' => $year,
- 'album_status' => $albumStatus,// 短剧更新状态 1未上映 2更新中 3已完结
- 'recommendation' => $recommendation,
- 'desp' => $desp,
- 'tag_list' => $tagList,
- 'qualification' => $qualification, // 资质状态 1未报审 2报审通过 3报审不通过 4不建议报审
- ],
- ];
- if(!empty($recordInfo)) {
- $params['album_info']['record_info'] = $recordInfo;// 备案信息(报审通过必填)
- }
- if(!empty($recordAuditInfo)) { # 备案审核信息
- $params['album_info']['record_audit_info'] = $recordAuditInfo;
- }
- $responseData = self::commonRequest($appId, $requestUrl, $uri, 'POST', $params, $header);
- if(isset($responseData['err_no']) && 0 != $responseData['err_no']) {
- if($retry <= 3) {
- $retry++;
- return self::createAlbum($appId, $title, $seqNum, $coverList, $year, $albumStatus, $recommendation, $desp
- , $tagList, $qualification, $recordInfo, $recordAuditInfo, $retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序创建短剧结果异常', json_encode([
- 'app_id' => $appId, 'title' => $title, 'seq_num' => $seqNum, 'cover_list' => $coverList,
- 'year' => $year, 'album_status' => $albumStatus,// 短剧更新状态 1未上映 2更新中 3已完结
- 'recommendation' => $recommendation, 'desp' => $desp, 'tag_list' => $tagList,
- 'qualification' => $qualification, // 资质状态 1未报审 2报审通过 3报审不通过 4不建议报审
- 'record_info' => $recordInfo, 'uri' => $uri, 'body' => $params, 'response' => $responseData
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- return $responseData['data'] ?? [];
- }
- public static function editAlbum($appId, $albumId, $title, $seqNum, $coverList, $year, $albumStatus, $recommendation
- , $desp, $tagList, $qualification, $recordInfo, $recordAuditInfo, $episodeInfoList, $retry = 0) {
- $requestUrl = 'https://open.douyin.com';
- $uri = config('douyin.editAlbum');
- $accessToken = AccessTokenService::getAccessToken($appId);
- $header = [
- 'access-token: '.$accessToken
- ];
- $params = [
- 'ma_app_id' => $appId,
- 'album_id' => $albumId,
- 'album_info' => [
- 'title' => $title,
- 'seq_num' => $seqNum,
- 'cover_list' => $coverList,
- 'year' => $year,
- 'album_status' => $albumStatus,// 短剧更新状态 1未上映 2更新中 3已完结
- 'recommendation' => $recommendation,
- 'desp' => $desp,
- 'tag_list' => $tagList,
- 'qualification' => $qualification, // 资质状态 1未报审 2报审通过 3报审不通过 4不建议报审
- ],
- 'episode_info_list' => $episodeInfoList
- ];
- if(!empty($recordInfo)) {
- $params['album_info']['record_info'] = $recordInfo;// 备案信息(报审通过必填)
- }
- if(!empty($recordAuditInfo)) { # 备案审核信息
- $params['album_info']['record_audit_info'] = $recordAuditInfo;
- }
- $responseData = self::commonRequest($appId, $requestUrl, $uri, 'POST', $params, $header);
- if(isset($responseData['err_no']) && 0 != $responseData['err_no']) {
- if($retry <= 3) {
- $retry++;
- return self::editAlbum($appId, $albumId, $title, $seqNum, $coverList, $year, $albumStatus, $recommendation, $desp
- , $tagList, $qualification, $recordInfo, $recordAuditInfo, $episodeInfoList, $retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序编辑短剧结果异常', json_encode([
- 'app_id' => $appId, 'album_id' => $albumId, 'title' => $title, 'seq_num' => $seqNum,
- 'cover_list' => $coverList, 'year' => $year, 'album_status' => $albumStatus,// 短剧更新状态 1未上映 2更新中 3已完结
- 'recommendation' => $recommendation, 'desp' => $desp, 'tag_list' => $tagList,
- 'qualification' => $qualification, // 资质状态 1未报审 2报审通过 3报审不通过 4不建议报审
- 'record_info' => $recordInfo, 'episode_info_list' => $episodeInfoList,// 剧集信息
- 'record_audit_info' => $recordAuditInfo,
- 'uri' => $uri, 'body' => $params, 'response' => $responseData
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- return $responseData['data'] ?? [];
- }
- public static function bindPage($appId, $singleSchemaBind, $schemaBindType, $retry = 0) {
- $requestUrl = 'https://open.douyin.com';
- $uri = config('douyin.bindPage');
- $accessToken = AccessTokenService::getAccessToken($appId);
- $header = [
- 'access-token: '.$accessToken
- ];
- $params = [
- 'app_id' => $appId,
- 'schema_bind_type' => $schemaBindType,
- 'single_schema_bind' => $singleSchemaBind
- ];
- $responseData = self::commonRequest($appId, $requestUrl, $uri, 'POST', $params, $header);
- if(isset($responseData['err_no']) && 0 != $responseData['err_no']) {
- if($retry <= 3) {
- $retry++;
- return self::bindPage($appId, $singleSchemaBind, $schemaBindType, $retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序短剧绑定页面结果异常', json_encode([
- 'app_id' => $appId, 'schema_bind_type' => $schemaBindType,
- 'single_schema_bind' => $singleSchemaBind,
- 'uri' => $uri, 'body' => $params, 'response' => $responseData
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- return $responseData['data'] ?? [];
- }
- public static function albumAuth($maAppId, $appIdList, $albumId, $increment, $retry = 0) {
- $requestUrl = 'https://open.douyin.com';
- $uri = config('douyin.albumAuth');
- $accessToken = AccessTokenService::getAccessToken($maAppId);
- $header = [
- 'access-token: '.$accessToken
- ];
- $params = [
- 'app_id' => $maAppId,
- 'app_id_list' => $appIdList,
- 'album_id' => $albumId,
- 'increment' => $increment
- ];
- $responseData = self::commonRequest($maAppId, $requestUrl, $uri, 'POST', $params, $header);
- if(isset($responseData['err_no']) && 0 != $responseData['err_no']) {
- if($retry <= 3) {
- $retry++;
- return self::albumAuth($maAppId, $appIdList, $albumId, $increment, $retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序短剧授权结果异常', json_encode([
- 'app_id' => $maAppId, 'app_id_list' => $appIdList,
- 'album_id' => $albumId, 'increment' => $increment,
- 'uri' => $uri, 'body' => $params, 'response' => $responseData
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- return $responseData['data'] ?? [];
- }
- public static function albumToExamine($appId, $albumId, $retry = 0) {
- $requestUrl = 'https://open.douyin.com';
- $uri = config('douyin.albumExamine');
- $accessToken = AccessTokenService::getAccessToken($appId);
- $header = [
- 'access-token: '.$accessToken
- ];
- $params = [
- 'ma_app_id' => $appId,
- 'album_id' => $albumId,
- ];
- $responseData = self::commonRequest($appId, $requestUrl, $uri, 'POST', $params, $header);
- if(isset($responseData['err_no']) && 0 != $responseData['err_no']) {
- if($retry <= 3) {
- $retry++;
- return self::albumToExamine($appId, $albumId, $retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序短剧送审结果异常', json_encode([
- 'app_id' => $appId, 'album_id' => $albumId,
- 'uri' => $uri, 'body' => $params, 'response' => $responseData
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- return $responseData['data'] ?? [];
- }
- public static function albumOnline($appId, $albumId, $operate, $version, $retry = 0) {
- $requestUrl = 'https://open.douyin.com';
- $uri = config('douyin.albumOnline');
- $accessToken = AccessTokenService::getAccessToken($appId);
- $header = [
- 'access-token: '.$accessToken
- ];
- $params = [
- 'operate' => $operate,
- 'album_id' => $albumId,
- 'version' => $version,
- ];
- $responseData = self::commonRequest($appId, $requestUrl, $uri, 'POST', $params, $header);
- if(isset($responseData['err_no']) && 0 != $responseData['err_no']) {
- if($retry <= 3) {
- $retry++;
- return self::albumOnline($appId, $albumId, $operate, $version, $retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序短剧上线结果异常', json_encode([
- 'app_id' => $appId, 'album_id' => $albumId, 'operate' => $operate, 'version' => $version,
- 'uri' => $uri, 'body' => $params, 'response' => $responseData
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- return $responseData['data'] ?? [];
- }
- public static function albumFetch($appId, $queryType, $batchQuery, $singleQuery, $detailQuery, $retry = 0) {
- $requestUrl = 'https://open.douyin.com';
- $uri = config('douyin.albumFetch');
- $accessToken = AccessTokenService::getAccessToken($appId);
- $header = [
- 'access-token: '.$accessToken
- ];
- $params = [
- 'ma_app_id' => $appId,
- 'query_type' => $queryType,
- ];
- if($batchQuery) {
- $params['batch_query'] = $batchQuery;
- }
- if($singleQuery) {
- $params['single_query'] = $singleQuery;
- }
- if($detailQuery) {
- $params['detail_query'] = $detailQuery;
- }
- $responseData = self::commonRequest($appId, $requestUrl, $uri, 'POST', $params, $header);
- if(isset($responseData['err_no']) && 0 != $responseData['err_no']) {
- if($retry <= 3) {
- $retry++;
- return self::albumFetch($appId, $queryType, $batchQuery, $singleQuery, $detailQuery, $retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序短剧查询果异常', json_encode([
- 'app_id' => $appId, 'query_type' => $queryType, 'batch_query' => $batchQuery
- , 'single_query' => $singleQuery, 'detail_query' => $detailQuery,
- 'uri' => $uri, 'body' => $params, 'response' => $responseData
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- return $responseData['data'] ?? [];
- }
- public static function getCustomerServiceList($appId, $pageNum, $pageSize, $type, $retry = 0) {
- $requestUrl = 'https://open.douyin.com';
- $uri = config('douyin.customerService');
- $accessToken = AccessTokenService::getAccessToken($appId);
- $header = [
- 'access-token: '.$accessToken
- ];
- $params = [
- 'page_num' => $pageNum,
- 'page_size'=> $pageSize,
- 'type' => $type
- ];
- $uri = $uri.'?'.http_build_query($params);
- $responseData = self::commonRequest($appId, $requestUrl, $uri, 'GET', [], $header);
- if(isset($responseData['err_no']) && 0 != $responseData['err_no']) {
- if($retry <= 3) {
- $retry++;
- return self::getCustomerServiceList($appId, $pageNum, $pageSize, $type, $retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序短剧绑定抖音号结果异常', json_encode([
- 'app_id' => $appId, 'page_num' => $pageNum, 'page_size' => $pageSize,'type' => $type,
- 'uri' => $uri, 'body' => $params, 'response' => $responseData
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- return $responseData['data'] ?? [];
- }
- # 查询广告位列表
- public static function syncAdPlacement($appId, $retry = 0) {
- $requestUrl = 'https://open.douyin.com';
- $uri = config('douyin.adPlacementQuery');
- $accessToken = AccessTokenService::getAccessToken($appId);
- $header = [
- 'access-token: '.$accessToken
- ];
- $responseData = self::commonRequest($appId, $requestUrl, $uri, 'GET', [], $header);
- if(isset($responseData['err_no']) && !in_array($responseData['err_no'], [0, 28005042])) {# 尚未开通流量主
- if($retry <= 3) {
- $retry++;
- return self::syncAdPlacement($appId, $retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序广告位列表结果异常', json_encode([
- 'app_id' => $appId, 'uri' => $uri, 'response' => $responseData
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- return $responseData['data'] ?? [];
- }
- # 添加广告位
- public static function adPlacementAdd($appId, $adType, $adName, $retry = 0) {
- $requestUrl = 'https://open.douyin.com';
- $uri = config('douyin.adPlacementAdd');
- $accessToken = AccessTokenService::getAccessToken($appId);
- $header = [
- 'access-token: '.$accessToken,
- 'content-type: application/json'
- ];
- $params = [
- 'ad_placement_name' => $adName,
- 'ad_placement_type' => $adType
- ];
- $responseData = self::commonRequest($appId, $requestUrl, $uri, 'POST', $params, $header);
- if(isset($responseData['err_no']) && !in_array($responseData['err_no'], [0, 28005042])) {# 尚未开通流量主
- if($retry <= 3) {
- $retry++;
- return self::adPlacementAdd($appId, $adType, $adName, $retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序添加广告位结果异常', json_encode([
- 'app_id' => $appId, 'ad_type' => $adType, 'ad_name' => $adName, 'uri' => $uri, 'response' => $responseData
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- return (0 == $responseData['err_no']);
- }
- public static function adPlacementUpdate($appId, $adPlacementId, $status, $retry = 0) {
- $requestUrl = 'https://open.douyin.com';
- $uri = config('douyin.adPlacementUpdate');
- $accessToken = AccessTokenService::getAccessToken($appId);
- $header = [
- 'access-token: '.$accessToken
- ];
- $params = [
- 'ad_placement_id' => $adPlacementId,
- 'status' => $status
- ];
- $responseData = self::commonRequest($appId, $requestUrl, $uri, 'POST', $params, $header);
- if(isset($responseData['err_no']) && !in_array($responseData['err_no'], [0, 28005042])) {# 尚未开通流量主
- if($retry <= 3) {
- $retry++;
- return self::adPlacementUpdate($appId, $adPlacementId, $status, $retry);
- } else {
- # 重试几次还是失败了,要报警
- EmailQueue::rPush('抖音小程序更新广告位状态结果异常', json_encode([
- 'app_id' => $appId, 'ad_placement_id' => $adPlacementId, 'status' => $status, 'uri' => $uri, 'response' => $responseData
- ], 1), ['song.shen@kuxuan-inc.com'], '抖音小程序');
- }
- }
- return (0 == $responseData['err_no']);
- }
- }
|