Nenhuma Descrição

StarUsersSync.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\JuxingAdAccount;
  4. use App\Models\JxStarUsers;
  5. use App\Support\Log;
  6. use App\Support\RedisModel;
  7. use Illuminate\Console\Command;
  8. use kwaiSDK\JuXing;
  9. class StarUsersSync extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'StarUsersSync';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '同步达人信息';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return int
  36. */
  37. public function handle()
  38. {
  39. $this->info(date('H:i') . ' 开始执行');
  40. $this->start();
  41. $this->info(date('H:i') . ' 结束执行');
  42. return 0;
  43. }
  44. public function start()
  45. {
  46. $curTime = time();
  47. while (true) {
  48. if ((time() - $curTime) >= 60) break;
  49. $data = $this->getUserIds();
  50. if( empty($data) ){
  51. sleep(2);
  52. continue;
  53. }
  54. $this->statData($data);
  55. sleep(1);
  56. }
  57. }
  58. public function getUserIds()
  59. {
  60. $res = array();
  61. $n = 0;
  62. while($n<20){
  63. $dataJson = RedisModel::rPop(JxStarUsers::JUXING_STAR_USER_INFO_SYNC_LIST);
  64. if (empty($dataJson)) {
  65. break;
  66. }
  67. $data = json_decode($dataJson, true);
  68. if (!isset($data['advertiserId'], $data['userId'])) {
  69. # 输出错误日志
  70. Log::error('数据参数不合法', $data, 'StarUsersSync');
  71. continue;
  72. }
  73. $res[$data['advertiserId']][] = $data['userId'];
  74. $n++;
  75. }
  76. return $res;
  77. }
  78. public function statData($data)
  79. {
  80. $time = date('Y-m-d H:i:s', strtotime('-2 day'));
  81. foreach($data as $advertiserId=>$userIds){
  82. $userIds = array_unique($userIds);
  83. #筛选已有的
  84. $alUsers = JxStarUsers::whereIn('user_id', $userIds)->where('updated_at', '>', $time)->pluck('user_id')->all();
  85. $userIds = array_values(array_diff($userIds, $alUsers));
  86. if( !empty($userIds) ){
  87. $this->getData($advertiserId, $userIds);
  88. }
  89. }
  90. }
  91. public function getData($advertiserId, $userIds)
  92. {
  93. $accessToken = JuxingAdAccount::getAccessToken($advertiserId);
  94. $juxing = new JuXing($accessToken);
  95. $param = [
  96. 'advertiser_id' => $advertiserId,
  97. 'user_id_list' => $userIds
  98. ];
  99. $rst = $juxing->thirdData()->encryptUserId($param);
  100. if(isset($rst['code']) && $rst['code']==0){
  101. $userEns = array_flip($rst['data']);
  102. $ensUserIds = array_values($rst['data']);
  103. $user_param = [
  104. 'advertiser_id' => $advertiserId,
  105. 'user_id' => $ensUserIds
  106. ];
  107. $this->getUserInfo($juxing, $user_param, $userEns);
  108. } else {
  109. $info = [
  110. 'param' => $param,
  111. 'token' => $accessToken,
  112. 'res' => $rst
  113. ];
  114. Log::error('获取达人加密ID失败', $info, 'StarUsersSync');
  115. }
  116. }
  117. public function getUserInfo($juxing, $param, $userEns)
  118. {
  119. $rst = $juxing->thirdData()->starInfoDetail($param);
  120. if (isset($rst['code'])) {
  121. if ($rst['code'] == 0) {
  122. foreach($rst['data']['list'] as $val){
  123. $ensUserId = $val['user_id'];
  124. $user_id = $userEns[$ensUserId] ?? null;
  125. if(!$user_id){
  126. return false;
  127. }
  128. $con = [
  129. 'user_id' => $user_id,
  130. 'encrypt_user_id' => $ensUserId,
  131. ];
  132. $attr = [
  133. 'advertiser_id' => $param['advertiser_id'],
  134. 'name' => $val['name'] ?? null,
  135. 'head_url' => $val['head_url'] ?? null,
  136. 'kwai_id' => $val['kwai_id'] ?? null,
  137. 'gender' => $val['gender'] ?? null,
  138. 'industry' => isset($val['industry']) && !empty($val['industry']) ? json_encode($val['industry']) : null,
  139. 'address' => $val['address'] ?? null,
  140. 'verified_info' => $val['verified_info'] ?? null,
  141. 'photo_count' => $val['photo_count'] ?? null,
  142. 'live_count_nearly_month' => $val['live_count_nearly_month'] ?? null,
  143. 'product_live_count_nearly_month' => $val['product_live_count_nearly_month'] ?? null,
  144. 'fans_number' => $val['fans_number'] ?? null,
  145. 'age_percent_age' => isset($val['fan_portrait']['age_percent_age']) && !empty($val['fan_portrait']['age_percent_age']) ? json_encode($val['fan_portrait']['age_percent_age']) : null,
  146. 'gender_percent_age' => isset($val['fan_portrait']['gender_percent_age']) && !empty($val['fan_portrait']['gender_percent_age']) ? json_encode($val['fan_portrait']['gender_percent_age']) : null,
  147. 'city_percent_age' => isset($val['fan_portrait']['city_percent_age']) && !empty($val['fan_portrait']['city_percent_age']) ? json_encode($val['fan_portrait']['city_percent_age']) : null,
  148. 'live_business_report' => isset($val['live_business_report']) && !empty($val['live_business_report']) ? json_encode($val['live_business_report']) : null,
  149. 'video_business_report' => isset($val['video_business_report']) && !empty($val['video_business_report']) ? json_encode($val['video_business_report']) : null,
  150. 'institution' => $val['institution'] ?? null,
  151. 'content_tag' => $val['content_tag'] ?? null,
  152. 'video_cooperation_price' => $val['video_cooperation_price'] ?? null,
  153. 'live_cooperation_price_hour' => $val['live_cooperation_price_hour'] ?? null,
  154. 'live_cooperation_price_day' => $val['live_cooperation_price_day'] ?? null,
  155. 'live_cooperation_price_goods' => $val['live_cooperation_price_goods'] ?? null,
  156. 'video_expected_play_cnt' => $val['video_expected_play_cnt'] ?? null,
  157. 'video_expected_cpm' => $val['video_expected_cpm'] ?? null,
  158. 'video_history_cooperation_industry' => isset($val['video_history_cooperation_industry']) && !empty($val['video_history_cooperation_industry']) ? json_encode($val['video_history_cooperation_industry']) : null,
  159. 'live_history_cooperation_industry' => isset($val['live_history_cooperation_industry']) && !empty($val['live_history_cooperation_industry']) ? json_encode($val['live_history_cooperation_industry']) : null,
  160. 'fans_grown_cnt_30_day' => $val['fans_grown_cnt_30_day'] ?? null,
  161. 'fans_grown_rate_30_day' => $val['fans_grown_rate_30_day'] ?? null,
  162. 'fans_grown_cnt_90_day' => $val['fans_grown_cnt_90_day'] ?? null,
  163. 'fans_grown_rate_90_day' => $val['fans_grown_rate_90_day'] ?? null,
  164. 'personal_video_complete_play_rate_30_day' => $val['personal_video_complete_play_rate_30_day'] ?? null,
  165. 'personal_video_complete_play_rate_benchmark' => $val['personal_video_complete_play_rate_benchmark'] ?? null,
  166. 'social_video_complete_play_rate_30_day' => $val['social_video_complete_play_rate_30_day'] ?? null,
  167. 'social_video_complete_play_rate_benchmark' => $val['social_video_complete_play_rate_benchmark'] ?? null,
  168. 'personal_video_interaction_rate_30_day' => $val['personal_video_interaction_rate_30_day'] ?? null,
  169. 'personal_video_interaction_rate_benchmark' => $val['personal_video_interaction_rate_benchmark'] ?? null,
  170. 'social_video_interaction_rate_30_day' => $val['social_video_interaction_rate_30_day'] ?? null,
  171. 'social_video_interaction_rate_benchmark' => $val['social_video_interaction_rate_benchmark'] ?? null,
  172. 'personal_video_play_median_30_day' => $val['personal_video_play_median_30_day'] ?? null,
  173. 'personal_video_play_median_benchmark' => $val['personal_video_play_median_benchmark'] ?? null,
  174. 'social_video_play_median_30_day' => $val['social_video_play_median_30_day'] ?? null,
  175. 'social_video_play_median_benchmark' => $val['social_video_play_median_benchmark'] ?? null,
  176. 'updated_at' => date('Y-m-d H:i:s')
  177. ];
  178. JxStarUsers::updateOrCreate($con, $attr);
  179. }
  180. return true;
  181. }
  182. if ($rst['code'] == 500000 && ($rst['message'] == '该达人不存在或未授权' || $rst['message'] == '当前达人不可下单')) {
  183. Log::info('该达人不存在或未授权', [
  184. 'param' => $param,
  185. 'res' => $rst
  186. ], 'StarUsersSync');
  187. return true;
  188. }
  189. }
  190. Log::error('获取达人详情失败', [
  191. 'param' => $param,
  192. 'res' => $rst
  193. ], 'StarUsersSync');
  194. }
  195. }