<?php

namespace App\Jobs;

use App\Consts\SysConsts;
use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

/**
 * 抖音用户注册
 */
class NewTikTokUser implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $ip;
    private $ua;
    private $channel_id;
    private $uid;
    private $register_time;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(string $ip, string $ua, int $channel_id, int $uid, string $register_time)
    {
        $this->ip = $ip;
        $this->ua = $ua;
        $this->channel_id = $channel_id;
        $this->uid = $uid;
        $this->register_time = $register_time;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $client = new Client();
        $params = [
            'ip' => $this->ip,
            'ua' => $this->ua,
            'channel_id' => $this->channel_id,
            'uid' => $this->uid,
            'register_time' => $this->register_time,
            'source' => 'wdy'
        ];
        $params['sign'] = _sign($params, SysConsts::TIKTOK_KEY);
        $url = 'https://newtrackapi.zhuishuyun.com/api/user/register';
        myLog('new_user_register')->info("uid:{$this->uid}".json_encode($params));
        $response =  $client->request('post', $url, ['form_params' => $params])->getBody()->getContents();
        myLog('new_user_register')->info("uid:{$this->uid}{$response}");
        $result =  json_decode($response);
        if ($result) {
            if ($result->code != 0) {
                myLog('new_user_register')->info($response);
            }
        }
    }
}
