<?php
namespace App\Common\Services;

class ShortyService
{
    private $host;

    public function __construct()
    {
        $this->host = config('services.short_app.url');
    }

    /**
     * 获取短地址url
     */
    public function getUrls($urls)
    {
        $host = $this->host . '/api/short';
        $headers['X-Requested-With'] = 'XMLHttpRequest';
        try {
            $ret = httpClient('post', $host, [
                "url" => $urls,
                "from" => "independent_web_".config('app.name')
            ], $headers);
            $ret = json_decode($ret, true);
        } catch (\Exception $e) {
            \Log::info($e->getMessage());
            return;
        }
        return $ret['data'] ?? [];
    }

    /**
     * 获取短地址url访问量
     */
    public function getUrlsHits($urls)
    {
        $host = $this->host . '/api/short-hits';
        $headers['X-Requested-With'] = 'XMLHttpRequest';
        try {
            $ret = httpClient('get', $host, [
                "url" => $urls,
                "from" => "independent_web_".config('app.name')
            ], $headers);
            $ret = json_decode($ret, true);
        } catch (\Exception $e) {
            \Log::info($e->getMessage());
            return;
        }
        return $ret['data'] ?? [];
    }
}
