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

    private $uid;
    private $time;
    private $amount;

    /**
     * Create a new job instance.
     * @param int $uid
     * @param string $type 类型
     * @return void
     */
    public function __construct(int $uid, float $amount, string $time)
    {
        $this->uid = $uid;
        $this->amount = $amount;
        $this->time = $time;
    }

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