<?php

namespace App\Jobs\QappTikTok;

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 $reportParams;

    /**
     * QappTikTokUser constructor.
     * @param QappTikTokUserRequest $reportParams
     */
    public function __construct(QappTikTokUserRequest $reportParams)
    {
        $this->reportParams = $reportParams;
    }

    /**
     * Execute the job.
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function handle()
    {
        $client         = new Client();
        $params         = [
            'ip'            => $this->reportParams->ip,
            'device_no'     => $this->reportParams->device_no,
            'mac'           => $this->reportParams->mac ?? '',
            'channel_id'    => (int)$this->reportParams->channel_id,
            'uid'           => (int)$this->reportParams->uid,
            'register_time' => $this->reportParams->register_time,
            'is_report'     => (string)$this->reportParams->is_report,
            'source'        => 'zsy',
            'send_order_id' => $this->reportParams->send_order_id,
        ];
        $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);
            }
        }
    }
}