<?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 QappTikTokUser implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $ip;
    private $device_no;
    private $mac;
    private $channel_id;
    private $uid;
    private $register_time;
    private $is_report;

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

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