123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- namespace App\Console\Commands;
- use App\Models\JuxingAdAccount;
- use App\Models\JxStarUsers;
- use App\Support\Log;
- use App\Support\RedisModel;
- use Illuminate\Console\Command;
- use kwaiSDK\JuXing;
- class StarUsersSync extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'StarUsersSync';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '同步达人信息';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return int
- */
- public function handle()
- {
- $this->info(date('H:i') . ' 开始执行');
- $this->start();
- $this->info(date('H:i') . ' 结束执行');
- return 0;
- }
- public function start()
- {
- $curTime = time();
- while (true) {
- if ((time() - $curTime) >= 60) break;
- $data = $this->getUserIds();
- if( empty($data) ){
- sleep(2);
- continue;
- }
- $this->statData($data);
- sleep(1);
- }
- }
- public function getUserIds()
- {
- $res = array();
- $n = 0;
- while($n<20){
- $dataJson = RedisModel::rPop(JxStarUsers::JUXING_STAR_USER_INFO_SYNC_LIST);
- if (empty($dataJson)) {
- break;
- }
- $data = json_decode($dataJson, true);
- if (!isset($data['advertiserId'], $data['userId'])) {
- # 输出错误日志
- Log::error('数据参数不合法', $data, 'StarUsersSync');
- continue;
- }
- $res[$data['advertiserId']][] = $data['userId'];
- $n++;
- }
- return $res;
- }
- public function statData($data)
- {
- $time = date('Y-m-d H:i:s', strtotime('-2 day'));
- foreach($data as $advertiserId=>$userIds){
- $userIds = array_unique($userIds);
- #筛选已有的
- $alUsers = JxStarUsers::whereIn('user_id', $userIds)->where('updated_at', '>', $time)->pluck('user_id')->all();
- $userIds = array_values(array_diff($userIds, $alUsers));
- if( !empty($userIds) ){
- $this->getData($advertiserId, $userIds);
- }
- }
- }
- public function getData($advertiserId, $userIds)
- {
- $accessToken = JuxingAdAccount::getAccessToken($advertiserId);
- $juxing = new JuXing($accessToken);
- $param = [
- 'advertiser_id' => $advertiserId,
- 'user_id_list' => $userIds
- ];
- $rst = $juxing->thirdData()->encryptUserId($param);
- if(isset($rst['code']) && $rst['code']==0){
- $userEns = array_flip($rst['data']);
- $ensUserIds = array_values($rst['data']);
- $user_param = [
- 'advertiser_id' => $advertiserId,
- 'user_id' => $ensUserIds
- ];
- $this->getUserInfo($juxing, $user_param, $userEns);
- } else {
- $info = [
- 'param' => $param,
- 'token' => $accessToken,
- 'res' => $rst
- ];
- Log::error('获取达人加密ID失败', $info, 'StarUsersSync');
- }
- }
- public function getUserInfo($juxing, $param, $userEns)
- {
- $rst = $juxing->thirdData()->starInfoDetail($param);
- if (isset($rst['code'])) {
- if ($rst['code'] == 0) {
- foreach($rst['data']['list'] as $val){
- $ensUserId = $val['user_id'];
- $user_id = $userEns[$ensUserId] ?? null;
- if(!$user_id){
- return false;
- }
- $con = [
- 'user_id' => $user_id,
- 'encrypt_user_id' => $ensUserId,
- ];
- $attr = [
- 'advertiser_id' => $param['advertiser_id'],
- 'name' => $val['name'] ?? null,
- 'head_url' => $val['head_url'] ?? null,
- 'kwai_id' => $val['kwai_id'] ?? null,
- 'gender' => $val['gender'] ?? null,
- 'industry' => isset($val['industry']) && !empty($val['industry']) ? json_encode($val['industry']) : null,
- 'address' => $val['address'] ?? null,
- 'verified_info' => $val['verified_info'] ?? null,
- 'photo_count' => $val['photo_count'] ?? null,
- 'live_count_nearly_month' => $val['live_count_nearly_month'] ?? null,
- 'product_live_count_nearly_month' => $val['product_live_count_nearly_month'] ?? null,
- 'fans_number' => $val['fans_number'] ?? null,
- '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,
- '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,
- '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,
- 'live_business_report' => isset($val['live_business_report']) && !empty($val['live_business_report']) ? json_encode($val['live_business_report']) : null,
- 'video_business_report' => isset($val['video_business_report']) && !empty($val['video_business_report']) ? json_encode($val['video_business_report']) : null,
- 'institution' => $val['institution'] ?? null,
- 'content_tag' => $val['content_tag'] ?? null,
- 'video_cooperation_price' => $val['video_cooperation_price'] ?? null,
- 'live_cooperation_price_hour' => $val['live_cooperation_price_hour'] ?? null,
- 'live_cooperation_price_day' => $val['live_cooperation_price_day'] ?? null,
- 'live_cooperation_price_goods' => $val['live_cooperation_price_goods'] ?? null,
- 'video_expected_play_cnt' => $val['video_expected_play_cnt'] ?? null,
- 'video_expected_cpm' => $val['video_expected_cpm'] ?? null,
- 'video_history_cooperation_industry' => isset($val['video_history_cooperation_industry']) && !empty($val['video_history_cooperation_industry']) ? json_encode($val['video_history_cooperation_industry']) : null,
- 'live_history_cooperation_industry' => isset($val['live_history_cooperation_industry']) && !empty($val['live_history_cooperation_industry']) ? json_encode($val['live_history_cooperation_industry']) : null,
- 'fans_grown_cnt_30_day' => $val['fans_grown_cnt_30_day'] ?? null,
- 'fans_grown_rate_30_day' => $val['fans_grown_rate_30_day'] ?? null,
- 'fans_grown_cnt_90_day' => $val['fans_grown_cnt_90_day'] ?? null,
- 'fans_grown_rate_90_day' => $val['fans_grown_rate_90_day'] ?? null,
- 'personal_video_complete_play_rate_30_day' => $val['personal_video_complete_play_rate_30_day'] ?? null,
- 'personal_video_complete_play_rate_benchmark' => $val['personal_video_complete_play_rate_benchmark'] ?? null,
- 'social_video_complete_play_rate_30_day' => $val['social_video_complete_play_rate_30_day'] ?? null,
- 'social_video_complete_play_rate_benchmark' => $val['social_video_complete_play_rate_benchmark'] ?? null,
- 'personal_video_interaction_rate_30_day' => $val['personal_video_interaction_rate_30_day'] ?? null,
- 'personal_video_interaction_rate_benchmark' => $val['personal_video_interaction_rate_benchmark'] ?? null,
- 'social_video_interaction_rate_30_day' => $val['social_video_interaction_rate_30_day'] ?? null,
- 'social_video_interaction_rate_benchmark' => $val['social_video_interaction_rate_benchmark'] ?? null,
- 'personal_video_play_median_30_day' => $val['personal_video_play_median_30_day'] ?? null,
- 'personal_video_play_median_benchmark' => $val['personal_video_play_median_benchmark'] ?? null,
- 'social_video_play_median_30_day' => $val['social_video_play_median_30_day'] ?? null,
- 'social_video_play_median_benchmark' => $val['social_video_play_median_benchmark'] ?? null,
- 'updated_at' => date('Y-m-d H:i:s')
- ];
- JxStarUsers::updateOrCreate($con, $attr);
- }
- return true;
- }
- if ($rst['code'] == 500000 && ($rst['message'] == '该达人不存在或未授权' || $rst['message'] == '当前达人不可下单')) {
- Log::info('该达人不存在或未授权', [
- 'param' => $param,
- 'res' => $rst
- ], 'StarUsersSync');
- return true;
- }
- }
- Log::error('获取达人详情失败', [
- 'param' => $param,
- 'res' => $rst
- ], 'StarUsersSync');
- }
- }
|