StatisticsController.php 4.7 KB
<?php

namespace App\Http\Controllers\Bside;


use App\Models\CustomerVisit\CustomerVisit;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * 流量统计
 * Class StatisticsController
 * @package App\Http\Controllers\Bside
 */
class StatisticsController extends BaseController
{
    private $customerVisit;

    public function __construct()
    {
        $this->customerVisit = new CustomerVisit();
    }

    /**
     * 访问来源
     * @return JsonResponse
     */
    public function source()
    {
        $pv_count = $this->customerVisit->getMonthPVCount();
        $ip_count = $this->customerVisit->getMonthIPCount();
        $lists    = $this->customerVisit->getUrlCount();
        return $this->success(compact('pv_count', 'ip_count', 'lists'));
    }

    /**
     * 地域分布
     * @return JsonResponse
     */
    public function distribution()
    {
        $pv_count = $this->customerVisit->getMonthPVCount();
        $ip_count = $this->customerVisit->getMonthIPCount();
        $lists    = $this->customerVisit->getCountryCount();
        return $this->success(compact('pv_count', 'ip_count', 'lists'));
    }

    /**
     * 受访页面
     * @return JsonResponse
     */
    public function page()
    {
        $pv_count = $this->customerVisit->getMonthPVCount();
        $ip_count = $this->customerVisit->getMonthIPCount();
        $lists    = $this->customerVisit->getPageCount();
        return $this->success(compact('pv_count', 'ip_count', 'lists'));
    }

    /**
     * 访问终端
     * @return JsonResponse
     */
    public function terminal()
    {
        $pv_count     = $this->customerVisit->getMonthPVCount();
        $ip_count     = $this->customerVisit->getMonthIPCount();
        $pc_count     = $this->customerVisit->getTerminalPcCount();
        $mobile_count = $this->customerVisit->getTerminalMobileCount();
        $lists        = $this->customerVisit->getCountryCount();
        return $this->success(compact('pv_count', 'ip_count', 'pc_count', 'mobile_count', 'lists'));
    }

    /**
     * 流量趋势
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws GuzzleException
     * @throws NotFoundExceptionInterface
     */
    public function trend()
    {
//        dd($this->customerVisit->getDayIPCount());
        $pv_count      = $this->customerVisit->getMonthPVCount();
        $ip_count      = $this->customerVisit->getMonthIPCount();
        $inquiry       = $this->getCache('inquiry_information');
        $inquiry_count = $inquiry['count'] ?? 0;
        $lists         = $inquiry['lists'] ?? [];
        return $this->success(compact('pv_count', 'ip_count', 'inquiry_count', 'lists'));
    }

    /**
     * 获取缓存
     * @param string $key 缓存键名
     * @param float|int $time 缓存时间,默认2小时
     * @return mixed
     * @throws ContainerExceptionInterface
     * @throws GuzzleException
     * @throws NotFoundExceptionInterface
     */
    public function getCache(string $key, $time = 60 * 60 * 2)
    {
        $domain   = request()->getHttpHost(); //'www.wowstainless.com';
        $sta_date = date('Y-m-01');
        $key      = $key . '_' . $this->stringUnderlineLowercase($domain) . '_' . str_replace('-', '_', $sta_date);
        $value    = cache()->get($key);
        if (!$value) {
            $value = $this->getInquiryInformation($domain, $sta_date);
            cache()->put($key, $value, $time);
        }
        return $value;
    }

    /**
     * 正则 - 名字转为小写并将空格转为下划线
     * @param $name
     * @return string
     */
    public function stringUnderlineLowercase($name)
    {
        return trim(strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $name)));
    }

    /**
     * 获取第三方询盘信息
     * @return array|string
     * @throws GuzzleException
     */
    public function getInquiryInformation($domain, $sta_date)
    {
        $token  = md5($domain . date("Y-m-d"));
        $source = '1,3';
        $url    = "https://form.globalso.com/api/external-interface/country_con/15243d63ed5a5738?domain={$domain}&token={$token}&source={$source}&sta_date={$sta_date}";
        $client = new Client(['verify' => false]);
        $http   = $client->get($url);
        $data   = [];
        if ($http->getStatusCode() != 200) {
            return $data;
        }
        $content = $http->getBody()->getContents();
        $json    = json_decode($content, true);
        if ($json['status'] != 200) {
            return $content;
        }
        $data['count'] = $json['data']['count'];
        $data['lists'] = $json['data']['data'];
        return $data;
    }
}