小说推广数据系统

YouZiAdSetList.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\VpAccount;
  5. use App\Models\VpAdAccountRelation;
  6. use App\RedisModel;
  7. use App\Services\HttpService;
  8. use App\Services\YouZiService;
  9. use Illuminate\Console\Command;
  10. class YouZiAdSetList extends Command
  11. {
  12. protected $signature = 'YouZiAdSetList {accountLabel?}';
  13. protected $description = '账号数据源绑定关系数据获取';
  14. protected $accountLabel;
  15. protected $platformId;
  16. public function handle()
  17. {
  18. \DB::connection()->disableQueryLog();
  19. $this->accountLabel = $this->argument('accountLabel') ? $this->argument('accountLabel') : 0;
  20. $this->info('本次查询的账号对应键值为:' . $this->accountLabel);
  21. $this->getBindableList();
  22. }
  23. private function getBindableList()
  24. {
  25. # 获取账号列表
  26. $fields = ['mp_app_id', 'app_id'];
  27. if($this->accountLabel == 1) {
  28. $this->platformId = 3;
  29. } else {
  30. $this->platformId = 1;
  31. }
  32. $search = [
  33. 'service_type' => 10
  34. ];
  35. $accountList = VpAccount::getAccountList($this->platformId, $fields, $search);
  36. foreach ($accountList as $item) {
  37. $maAppId = isset($item->mp_app_id) ? $item->mp_app_id : null;
  38. $appId = isset($item->app_id) ? $item->app_id : null;
  39. $this->info('本次处理账号appId【'.$appId.'】');
  40. if(empty($maAppId) || empty($appId)) {
  41. Log::logError('账号数据存在异常', $item, 'YouZiBindableList');
  42. continue;
  43. }
  44. $this->doRequest($appId, $maAppId);
  45. }
  46. }
  47. private function doRequest($appId, $maAppId, $retry=0)
  48. {
  49. $requestUrl = YouZiService::BASE_URI . YouZiService::AD_SET_LIST_URI;
  50. $accessToken = YouZiService::getAccessToken($this->accountLabel);
  51. if(empty($accessToken)) return false;
  52. $header = [
  53. 'accesstoken: ' . $accessToken,
  54. 'maappid: ' . $maAppId,
  55. 'mpappid: ' . $appId
  56. ];
  57. $response = HttpService::httpGet($requestUrl, $header);
  58. $responseData = json_decode($response, True);
  59. if(isset($responseData['code']) && $responseData['code'] == -10001 && $retry < 5) {
  60. # 清除已缓存的AccessToken
  61. RedisModel::del(YouZiService::ACCESS_TOKEN_RDS_KEY . '_' .$this->accountLabel);
  62. $retry++;
  63. $this->doRequest($appId, $maAppId, $retry);
  64. }
  65. # 是否存在合法数据
  66. $data = isset($responseData['data']['bindMpAppIds']) ? $responseData['data']['bindMpAppIds'] : null;
  67. if(empty($data)) {
  68. Log::logError('腾讯广告关联公众账号数据抓取失败', [
  69. 'response' => $responseData,
  70. 'accessToken' => $accessToken,
  71. 'retry' => $retry
  72. ]);
  73. return false;
  74. }
  75. $platformId = $this->platformId;
  76. # 检出数据
  77. foreach ($data as $datum) {
  78. # 公众账号AppId获取
  79. $userAppId = isset($datum['mpAppId']) ? $datum['mpAppId'] : '';
  80. if(empty($userAppId)) {
  81. Log::logError('公众号AppId获取失败', $datum, 'YouZiWxAccount');
  82. continue;
  83. }
  84. $enable = 1;
  85. $isDeleted = isset($datum['isDeleted']) ? $datum['isDeleted'] : 0;
  86. if($isDeleted) $enable = -1;
  87. $insertData = [
  88. 'created_id' => isset($datum['createdId']) ? $datum['createdId'] : null,
  89. 'platform_created_at' => isset($datum['createdTs']) ? date('Y-m-d H:i:s', round($datum['createdTs'] / 1000)) : null,
  90. 'updated_id' => isset($datum['updatedId']) ? $datum['updatedId'] : null,
  91. 'platform_updated_at' => isset($datum['updatedTs']) ? date('Y-m-d H:i:s', round($datum['updatedTs'] / 1000)) : null,
  92. 'enable' => $enable,
  93. 'nickname' => isset($datum['mpNickName']) ? $datum['mpNickName'] : null,
  94. 'admin_id' => isset($datum['dpAdminId']) ? $datum['dpAdminId'] : null,
  95. 'action_return_set_type' => isset($datum['userActionReturnSetType']) ? $datum['userActionReturnSetType'] : null,
  96. 'action_set_id' => isset($datum['userActionSetId']) ? $datum['userActionSetId'] : null,
  97. 'is_open' => isset($datum['isOpen']) ? $datum['isOpen'] : 0,
  98. 'is_full_report' => isset($datum['isFullReport']) ? $datum['isFullReport'] : 0,
  99. 'is_show_full_report' => isset($datum['isShowFullReport']) ? $datum['isShowFullReport'] : null,
  100. 'is_bind_qw' => isset($datum['isBindQw']) ? $datum['isBindQw'] : null,
  101. 'is_qw' => isset($datum['isQw']) ? $datum['isQw'] : null,
  102. 'bind_mp_app_id' => isset($datum['bindMpAppId']) ? $datum['bindMpAppId'] : null,
  103. 'bind_mp_app_ids' => isset($datum['bindMpAppIds']) ? json_encode($datum['bindMpAppIds']) : null,
  104. 'open_fans' => isset($datum['openFans']) ? $datum['openFans'] : null,
  105. 'use_union_id' => isset($datum['useUnionId']) ? $datum['useUnionId'] : null
  106. ];
  107. VpAdAccountRelation::updateOrCreate(
  108. ['platform_id'=>$platformId, 'app_id'=>$userAppId, 'ad_app_id' => $appId, 'ma_app_id' => $maAppId], $insertData
  109. );
  110. }
  111. }
  112. }