123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\VpAccount;
- use App\Models\VpAdAccountRelation;
- use App\RedisModel;
- use App\Services\HttpService;
- use App\Services\YouZiService;
- use Illuminate\Console\Command;
- class YouZiAdSetList extends Command
- {
- protected $signature = 'YouZiAdSetList {accountLabel?}';
- protected $description = '账号数据源绑定关系数据获取';
- protected $accountLabel;
- protected $platformId;
- public function handle()
- {
- \DB::connection()->disableQueryLog();
- $this->accountLabel = $this->argument('accountLabel') ? $this->argument('accountLabel') : 0;
- $this->info('本次查询的账号对应键值为:' . $this->accountLabel);
- $this->getBindableList();
- }
- private function getBindableList()
- {
- # 获取账号列表
- $fields = ['mp_app_id', 'app_id'];
- if($this->accountLabel == 1) {
- $this->platformId = 3;
- } else {
- $this->platformId = 1;
- }
- $search = [
- 'service_type' => 10
- ];
- $accountList = VpAccount::getAccountList($this->platformId, $fields, $search);
- foreach ($accountList as $item) {
- $maAppId = isset($item->mp_app_id) ? $item->mp_app_id : null;
- $appId = isset($item->app_id) ? $item->app_id : null;
- $this->info('本次处理账号appId【'.$appId.'】');
- if(empty($maAppId) || empty($appId)) {
- Log::logError('账号数据存在异常', $item, 'YouZiBindableList');
- continue;
- }
- $this->doRequest($appId, $maAppId);
- }
- }
- private function doRequest($appId, $maAppId, $retry=0)
- {
- $requestUrl = YouZiService::BASE_URI . YouZiService::AD_SET_LIST_URI;
- $accessToken = YouZiService::getAccessToken($this->accountLabel);
- if(empty($accessToken)) return false;
- $header = [
- 'accesstoken: ' . $accessToken,
- 'maappid: ' . $maAppId,
- 'mpappid: ' . $appId
- ];
- $response = HttpService::httpGet($requestUrl, $header);
- $responseData = json_decode($response, True);
- if(isset($responseData['code']) && $responseData['code'] == -10001 && $retry < 5) {
- # 清除已缓存的AccessToken
- RedisModel::del(YouZiService::ACCESS_TOKEN_RDS_KEY . '_' .$this->accountLabel);
- $retry++;
- $this->doRequest($appId, $maAppId, $retry);
- }
- # 是否存在合法数据
- $data = isset($responseData['data']['bindMpAppIds']) ? $responseData['data']['bindMpAppIds'] : null;
- if(empty($data)) {
- Log::logError('腾讯广告关联公众账号数据抓取失败', [
- 'response' => $responseData,
- 'accessToken' => $accessToken,
- 'retry' => $retry
- ]);
- return false;
- }
- $platformId = $this->platformId;
- # 检出数据
- foreach ($data as $datum) {
- # 公众账号AppId获取
- $userAppId = isset($datum['mpAppId']) ? $datum['mpAppId'] : '';
- if(empty($userAppId)) {
- Log::logError('公众号AppId获取失败', $datum, 'YouZiWxAccount');
- continue;
- }
- $enable = 1;
- $isDeleted = isset($datum['isDeleted']) ? $datum['isDeleted'] : 0;
- if($isDeleted) $enable = -1;
- $insertData = [
- 'created_id' => isset($datum['createdId']) ? $datum['createdId'] : null,
- 'platform_created_at' => isset($datum['createdTs']) ? date('Y-m-d H:i:s', round($datum['createdTs'] / 1000)) : null,
- 'updated_id' => isset($datum['updatedId']) ? $datum['updatedId'] : null,
- 'platform_updated_at' => isset($datum['updatedTs']) ? date('Y-m-d H:i:s', round($datum['updatedTs'] / 1000)) : null,
- 'enable' => $enable,
- 'nickname' => isset($datum['mpNickName']) ? $datum['mpNickName'] : null,
- 'admin_id' => isset($datum['dpAdminId']) ? $datum['dpAdminId'] : null,
- 'action_return_set_type' => isset($datum['userActionReturnSetType']) ? $datum['userActionReturnSetType'] : null,
- 'action_set_id' => isset($datum['userActionSetId']) ? $datum['userActionSetId'] : null,
- 'is_open' => isset($datum['isOpen']) ? $datum['isOpen'] : 0,
- 'is_full_report' => isset($datum['isFullReport']) ? $datum['isFullReport'] : 0,
- 'is_show_full_report' => isset($datum['isShowFullReport']) ? $datum['isShowFullReport'] : null,
- 'is_bind_qw' => isset($datum['isBindQw']) ? $datum['isBindQw'] : null,
- 'is_qw' => isset($datum['isQw']) ? $datum['isQw'] : null,
- 'bind_mp_app_id' => isset($datum['bindMpAppId']) ? $datum['bindMpAppId'] : null,
- 'bind_mp_app_ids' => isset($datum['bindMpAppIds']) ? json_encode($datum['bindMpAppIds']) : null,
- 'open_fans' => isset($datum['openFans']) ? $datum['openFans'] : null,
- 'use_union_id' => isset($datum['useUnionId']) ? $datum['useUnionId'] : null
- ];
- VpAdAccountRelation::updateOrCreate(
- ['platform_id'=>$platformId, 'app_id'=>$userAppId, 'ad_app_id' => $appId, 'ma_app_id' => $maAppId], $insertData
- );
- }
- }
- }
|