UserController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: vanshao
  5. * Date: 2019-05-10
  6. * Time: 10:20
  7. */
  8. namespace App\Http\Controllers\Admin\User;
  9. use App\Http\Controllers\Controller;
  10. use App\Http\Models\Site;
  11. use App\Http\Models\User;
  12. use GuzzleHttp\Client;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\Auth;
  15. use Illuminate\Support\Facades\Log;
  16. class UserController extends Controller
  17. {
  18. public function __construct()
  19. {
  20. }
  21. public function index(Request $request)
  22. {
  23. if ($siteId = $request->input('siteId')) {
  24. $site = Site::query()->where(['id' => $siteId])->first();
  25. $view = 'admin/user/user_for_site';
  26. $btn['closeBtn'] = 1;
  27. // dd($siteId,$site);
  28. } else {
  29. $user = auth()->user();
  30. $site = $user->sites->first();
  31. $view = 'admin/user/user';
  32. $btn['closeBtn'] = 0;
  33. }
  34. if (!$site) {
  35. if($siteId) {
  36. return view('admin/site/tips', [
  37. 'tips' => '站点信息不存在',
  38. 'siteId' => $siteId
  39. ]);
  40. }
  41. return view('admin/errors/tips', $btn);
  42. }
  43. //访问地区数据
  44. $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';
  45. ////访问量数据//流量趋势 行为趋势
  46. $access = sprintf('SELECT FROM_UNIXTIME(timestamp, "%%Y-%%m-%%d") AS lt,
  47. SUM(pv) AS pv,
  48. SUM(uv) AS uv,
  49. SUM(ip) AS ip,
  50. SUM(duration) AS duration,
  51. SUM(bounces) AS bounces FROM `scn_traffic_report_hourly` WHERE timestamp <= %s GROUP BY `lt` ORDER BY `lt` desc LIMIT 14 ', time());
  52. //总访问量
  53. $totalAccess = 'SELECT
  54. SUM(pv) AS pv,
  55. SUM(uv) AS uv,
  56. SUM(duration) AS duration,
  57. SUM(bounces) AS bounces FROM `scn_traffic_report_hourly` limit 1';
  58. //top10url
  59. // $top10Url = sprintf('SELECT url, SUM(pv) AS pv FROM `scn_traffic_report_url` WHERE timestamp <= %s GROUP BY `url` ORDER BY `pv` DESC limit 100 ', strtotime('-30 days'));
  60. $top10Url = sprintf('SELECT url, SUM(pv) AS pv FROM `scn_traffic_report_url` WHERE timestamp >= %s GROUP BY `url` ORDER BY `pv` DESC limit 100 ', strtotime('-30 days'));
  61. ////top10入口
  62. $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 100', strtotime('-30 days'));
  63. $content = 'select count(1) from scn_content';
  64. $http = new Client();
  65. try {
  66. $response = $http->post($site->api_url . 'api / getdata', [
  67. 'form_params' => [
  68. $area,
  69. $access,
  70. $totalAccess,
  71. $top10Url,
  72. $top10Entrance,
  73. $content
  74. ]
  75. ]);
  76. $data = json_decode($response->getBody(), true);
  77. $country_data = array_reverse($data['0']);
  78. // $pv_data = array_reverse($data['1']);
  79. $pv_data = $data['1'];
  80. $total_pv_data = $data['2']['0'];
  81. $top_url = $data['3'];
  82. $top_host = $data['4'];
  83. $total_content = $data['5']['0']['count(1)'];
  84. //访问地区数据
  85. foreach ($country_data as $value) {
  86. $country_data['country'][] = $value['region'];
  87. $country_data['pv'][] = $value['pv'];
  88. $country_data['uv'][] = $value['uv'];
  89. $country_data['duration'][] = round($value['duration'] / $value['pv'] / 60) . '.' . ($value['duration'] / $value['pv']) % 60;
  90. // dd( $value['uv'] );
  91. // $country_data['bounces'][] = round($value['bounces'] / $value['uv'] * 100) > 100 ? 100 : round($value['bounces'] / $value['uv'] * 100);
  92. if (empty($value['uv'])) {
  93. $country_data['bounces'][] =0;
  94. } else {
  95. $country_data['bounces'][] = round($value['bounces'] / $value['uv'] * 100) > 100 ? 100 : round($value['bounces'] / $value['uv'] * 100);
  96. }
  97. // dd($country_data);
  98. $country_data['pie'][] = array(
  99. 'value' => $value['pv'],
  100. 'name' => $value['region']
  101. );
  102. }
  103. $country_data['pie_country'] = array_reverse($country_data['country']);//饼图国家倒叙
  104. //平均在线时长
  105. $total_pv_data['avg_duration'] = round($total_pv_data['duration'] / $total_pv_data['pv'] / 60) . ':' . ($total_pv_data['duration'] / $total_pv_data['pv']) % 60;
  106. // $pv_data['xAxis'] = $this->getDescDayTime(13);
  107. $pv_data_day = $this->getDescDayTime(13);
  108. foreach ($pv_data_day as $day_key=>$day_value){
  109. $is_in=0;
  110. foreach ($pv_data as $value){
  111. if(substr($value['lt'],5)==$day_value){
  112. $pv_data2['pv'][] = $value['pv'];
  113. $pv_data2['uv'][] = $value['uv'];
  114. $pv_data2['duration'][] = round($value['duration'] / $value['pv'] / 60) . '.' . ($value['duration'] / $value['pv']) % 60;
  115. $pv_data2['bounces'][] = round($value['bounces'] / $value['uv'] * 100) > 100 ? 100 : round($value['bounces'] / $value['uv'] * 100);
  116. $is_in=1;
  117. }
  118. }
  119. if($is_in==0){
  120. $pv_data2['pv'][] = '0';
  121. $pv_data2['uv'][] = '0';
  122. $pv_data2['duration'][] = '0';
  123. $pv_data2['bounces'][] = '100';
  124. }
  125. }
  126. $pv_data=$pv_data2;
  127. $pv_data['xAxis']=$pv_data_day;
  128. //访问量数据
  129. // foreach ($pv_data as $value) {
  130. // $pv_data['pv'][] = $value['pv'];
  131. // $pv_data['uv'][] = $value['uv'];
  132. // $pv_data['duration'][] = round($value['duration'] / $value['pv'] / 60) . '.' . ($value['duration'] / $value['pv']) % 60;
  133. // $pv_data['bounces'][] = round($value['bounces'] / $value['uv'] * 100) > 100 ? 100 : round($value['bounces'] / $value['uv'] * 100);
  134. // }
  135. $addition = [
  136. 'pv_data' => $pv_data,
  137. 'country_data' => $country_data,
  138. 'total_pv_data' => $total_pv_data,
  139. 'top_url' => $top_url,
  140. 'top_host' => $top_host,
  141. 'total_content' => $total_content,
  142. ];
  143. //
  144. //
  145. } catch (\Throwable $throwable) {
  146. Log::info(var_export($throwable->getMessage(), 1));
  147. if($siteId) {
  148. return view('admin/site/tips', [
  149. 'tips' => '站点信息不存在',
  150. 'siteId' => $siteId
  151. ]);
  152. }
  153. return view('admin/errors/tips', [
  154. 'tips' => '请求错误'
  155. ] + $btn);
  156. }
  157. return view($view, ['site' => $site, 'siteId' => $site->id] + $addition);
  158. }
  159. //获取倒序日期时间
  160. public function getDescDayTime($days)
  161. {
  162. for ($i = $days; $i >= 0; $i--) {
  163. $return_data[] = date("m-d", strtotime("-$i day"));
  164. }
  165. return $return_data;
  166. }
  167. }