| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 | <?php/** * Created by PhpStorm. * User: vanshao * Date: 2019-05-10 * Time: 10:20 */namespace App\Http\Controllers\Admin\User;use App\Http\Controllers\Controller;use App\Http\Models\Site;use App\Http\Models\User;use GuzzleHttp\Client;use Illuminate\Http\Request;use Illuminate\Support\Facades\Auth;use Illuminate\Support\Facades\Log;class UserController extends Controller{    public function __construct()    {    }    public function index(Request $request)    {        if ($siteId = $request->input('siteId')) {            $site = Site::query()->where(['id' => $siteId])->first();            $view = 'admin/user/user_for_site';            $btn['closeBtn'] = 1;//            dd($siteId,$site);        } else {            $user = auth()->user();            $site = $user->sites->first();            $view = 'admin/user/user';            $btn['closeBtn'] = 0;        }        if (!$site) {            return view('admin/errors/tips', $btn);        }        //访问地区数据        $area = 'SELECT country AS region, SUM(pv) AS pv,SUM(uv) AS uv,SUM(ip) AS ip,SUM(duration) AS duration,SUM(bounces) AS bounces FROM `scn_traffic_report_region` GROUP BY `country` ORDER BY `pv` DESC limit 10';        ////访问量数据        $access = sprintf('SELECT FROM_UNIXTIME(timestamp, "%%Y-%%m-%%d") AS lt,				SUM(pv) AS pv,				SUM(uv) AS uv,				SUM(ip) AS ip,				SUM(duration) AS duration,				SUM(bounces) AS bounces FROM `scn_traffic_report_hourly` WHERE timestamp <= %s GROUP BY `lt` ORDER BY `lt` desc LIMIT 14 ', time());        //总访问量        $totalAccess = 'SELECT 				SUM(pv) AS pv,				SUM(uv) AS uv,				SUM(duration) AS duration,				SUM(bounces) AS bounces FROM `scn_traffic_report_hourly` limit 1';        //top10url        $top10Url = sprintf('SELECT url, SUM(pv) AS pv FROM `scn_traffic_report_url` WHERE timestamp <= %s GROUP BY `url` ORDER BY `pv` DESC limit 10 ', strtotime('-30 days'));        ////top10入口        $top10Entrance = sprintf('SELECT ref_host, SUM(pv) AS pv FROM `scn_traffic_report_referrer` WHERE timestamp >= %s GROUP BY `ref_host` ORDER BY `pv` DESC limit 10', strtotime('-30 days'));        $content = 'select count(1) from scn_content';        $http = new Client();//        $sql = array(//            'SELECT country AS region,//                        SUM(pv) AS pv,//						SUM(uv) AS uv,//						SUM(ip) AS ip,//						SUM(duration) AS duration,//						SUM(bounces) AS bounces FROM `scn_traffic_report_region` GROUP BY `country` ORDER BY `pv` DESC limit 10 ',//访问地区数据//            'SELECT FROM_UNIXTIME(timestamp, "%Y-%m-%d") AS lt,//				SUM(pv) AS pv,//				SUM(uv) AS uv,//				SUM(ip) AS ip,//				SUM(duration) AS duration,//				SUM(bounces) AS bounces FROM `scn_traffic_report_hourly` WHERE timestamp <= ' . "'" . time() . "'" . ' GROUP BY `lt` ORDER BY `lt` desc LIMIT 14 ',//访问量数据//            'SELECT//				SUM(pv) AS pv,//				SUM(uv) AS uv,//				SUM(duration) AS duration,//				SUM(bounces) AS bounces FROM `scn_traffic_report_hourly` limit 1',//总访问量//            'SELECT url, SUM(pv) AS pv FROM `scn_traffic_report_url` WHERE timestamp <= ' . "'" . strtotime('-30 days') . "'" . ' GROUP BY `url` ORDER BY `pv` DESC limit 10 ',//top10url//            'SELECT ref_host, SUM(pv) AS pv FROM `scn_traffic_report_referrer` WHERE timestamp >= ' . "'" . strtotime('-30 days') . "'" . ' GROUP BY `ref_host` ORDER BY `pv` DESC limit 10 ',//top10入口//            'select count(1) from scn_content'//        );        try {            $response = $http->post($site->api_url . 'api / getdata', [                'form_params' => [                    $area,                    $access,                    $totalAccess,                    $top10Url,                    $top10Entrance,                    $content                ]            ]);            $data = json_decode($response->getBody(), true);            $country_data = array_reverse($data['0']);            $pv_data = array_reverse($data['1']);            $total_pv_data = $data['2']['0'];            $top_url = $data['3'];            $top_host = $data['4'];            $total_content = $data['5']['0']['count(1)'];            //访问地区数据            foreach ($country_data as $value) {                $country_data['country'][] = $value['region'];                $country_data['pv'][] = $value['pv'];                $country_data['uv'][] = $value['uv'];                $country_data['duration'][] = round($value['duration'] / $value['pv'] / 60) . ' . ' . ($value['duration'] / $value['pv']) % 60;                $country_data['bounces'][] = round($value['bounces'] / $value['uv'] * 100) > 100 ? 100 : round($value['bounces'] / $value['uv'] * 100);                $country_data['pie'][] = array(                    'value' => $value['pv'],                    'name' => $value['region']                );            }            $country_data['pie_country'] = array_reverse($country_data['country']);//饼图国家倒叙            //平均在线时长            $total_pv_data['avg_duration'] = round($total_pv_data['duration'] / $total_pv_data['pv'] / 60) . ':' . ($total_pv_data['duration'] / $total_pv_data['pv']) % 60;            //访问量数据            foreach ($pv_data as $value) {                $pv_data['pv'][] = $value['pv'];                $pv_data['uv'][] = $value['uv'];                $pv_data['duration'][] = round($value['duration'] / $value['pv'] / 60) . ' . ' . ($value['duration'] / $value['pv']) % 60;                $pv_data['bounces'][] = round($value['bounces'] / $value['uv'] * 100) > 100 ? 100 : round($value['bounces'] / $value['uv'] * 100);            }            $pv_data['xAxis'] = $this->getDescDayTime(13);            $addition = [                'pv_data' => $pv_data,                'country_data' => $country_data,                'total_pv_data' => $total_pv_data,                'top_url' => $top_url,                'top_host' => $top_host,                'total_content' => $total_content,            ];        } catch (\Throwable $throwable) {            Log::info(var_export($throwable->getMessage(), 1));            return view('admin / errors / tips', [                    'tips' => '请求错误'                ] + $btn);        }        return view($view, ['site' => $site, 'siteId' => $site->id] + $addition);    }    //获取倒序日期时间    public function getDescDayTime($days)    {        for ($i = $days; $i >= 0; $i--) {            $return_data[] = date("m-d", strtotime("-$i day"));        }        return $return_data;    }}
 |