作者 李美松

更新统计流量代码,新增流量趋势天数的获取并保存

@@ -67,7 +67,7 @@ if (!function_exists('http_post')) { @@ -67,7 +67,7 @@ if (!function_exists('http_post')) {
67 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 67 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
68 $res = curl_exec($ch); 68 $res = curl_exec($ch);
69 if (curl_errno($ch)) { 69 if (curl_errno($ch)) {
70 - \Illuminate\Support\Facades\Log::info(print_r(curl_errno($ch),1),'debug---1'); 70 + \Illuminate\Support\Facades\Log::info(print_r(curl_errno($ch), 1), 'debug---1');
71 } 71 }
72 curl_close($ch); 72 curl_close($ch);
73 return json_decode($res, true); 73 return json_decode($res, true);
@@ -321,3 +321,86 @@ if (!function_exists('stringUnderlineLowercase')) { @@ -321,3 +321,86 @@ if (!function_exists('stringUnderlineLowercase')) {
321 return trim(strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $name))); 321 return trim(strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $name)));
322 } 322 }
323 } 323 }
  324 +
  325 +if (!function_exists('checkIsGreaterMonth')) {
  326 + /**
  327 + * 判断传入日期是否大于当月
  328 + * @param $date
  329 + * @return bool
  330 + */
  331 + function checkIsGreaterMonth($date)
  332 + {
  333 + // 传入日期的时间戳
  334 + $timestamp = strtotime($date);
  335 + // 当前月份的时间戳
  336 + $nowMonth = strtotime(date('Y-m'));
  337 + // 判断传入日期是否大于当前月份
  338 + return $timestamp > $nowMonth;
  339 + }
  340 +}
  341 +
  342 +if (!function_exists('checkIsMonth')) {
  343 + /**
  344 + * 判断传入日期是否是当月
  345 + * @param $date
  346 + * @return bool
  347 + */
  348 + function checkIsMonth($date)
  349 + {
  350 + // 获取当前时间戳
  351 + $now = time();
  352 + // 获取当月的起始时间戳和结束时间戳
  353 + $firstDay = strtotime(date('Y-m-01', $now));
  354 + $lastDay = strtotime(date('Y-m-t', $now));
  355 + // 传入日期的时间戳
  356 + $timestamp = strtotime($date);
  357 + // 判断传入日期是否在当月范围内
  358 + return $timestamp >= $firstDay && $timestamp <= $lastDay;
  359 + }
  360 +}
  361 +
  362 +if (!function_exists('getDateDays')) {
  363 + /**
  364 + * 返回当月到今天的天数
  365 + * @param string|null $date 日期,格式:Y-m
  366 + * @return array
  367 + */
  368 + function getDateDays(string $date = null)
  369 + {
  370 + list($year, $month, $day) = explode('-', date('Y-m-d'));
  371 + // 获取当前月的第一天
  372 + $first_day_of_month = "{$year}-{$month}-01";
  373 + // 获取今天的日期
  374 + $today = "{$year}-{$month}-{$day}";
  375 + if (!is_null($date)) {
  376 + $dd = explode('-', $date);
  377 + if (!checkIsGreaterMonth($date) && !checkIsMonth($date)) {
  378 + $year = $dd[0];
  379 + $month = $dd[1];
  380 + $first_day_of_month = "{$year}-{$month}-01";
  381 + return getDateArray("{$year}-{$month}-" . date("t", strtotime($first_day_of_month)));
  382 + }
  383 + }
  384 + $day_timestamp = strtotime($today) - strtotime($first_day_of_month);
  385 + return getDateArray("{$year}-{$month}-" . date('d', $day_timestamp));
  386 + }
  387 +}
  388 +
  389 +if (!function_exists('getDateArray')) {
  390 + /**
  391 + * 获取当月获取日期
  392 + * @param string $date 日期,格式:Y-m-d
  393 + * @return array
  394 + */
  395 + function getDateArray(string $date)
  396 + {
  397 + list($year, $month, $day) = explode('-', date($date));
  398 + $i = 1;
  399 + $days = [];
  400 + while ($i <= $day) {
  401 + $days[] = "{$year}-{$month}-" . str_pad($i, 2, "0", STR_PAD_LEFT);
  402 + $i++;
  403 + }
  404 + return $days;
  405 + }
  406 +}
@@ -87,8 +87,9 @@ class StatisticsController extends BaseController @@ -87,8 +87,9 @@ class StatisticsController extends BaseController
87 */ 87 */
88 public function trend() 88 public function trend()
89 { 89 {
90 - $type = $this->trafficStatistics::TYPE_TREND;  
91 - $response = $this->statisticsLogic->getStatisticsData($type); 90 + $type = $this->trafficStatistics::TYPE_TREND;
  91 + $response = $this->statisticsLogic->getStatisticsData($type);
  92 + $response['days'] = $this->statisticsLogic->getDaysTrend();
92 return $this->success($response); 93 return $this->success($response);
93 } 94 }
94 95
@@ -4,9 +4,12 @@ namespace App\Http\Logic\Bside\Statistics; @@ -4,9 +4,12 @@ namespace App\Http\Logic\Bside\Statistics;
4 4
5 use App\Http\Logic\Bside\BaseLogic; 5 use App\Http\Logic\Bside\BaseLogic;
6 use App\Models\Bside\Statistics\TrafficStatistics; 6 use App\Models\Bside\Statistics\TrafficStatistics;
  7 +use App\Models\Bside\Statistics\TrafficTrends;
7 use App\Models\CustomerVisit\CustomerVisit; 8 use App\Models\CustomerVisit\CustomerVisit;
8 use GuzzleHttp\Exception\GuzzleException; 9 use GuzzleHttp\Exception\GuzzleException;
9 use Illuminate\Contracts\Cache\Repository; 10 use Illuminate\Contracts\Cache\Repository;
  11 +use Illuminate\Database\Eloquent\Builder;
  12 +use Illuminate\Database\Eloquent\Collection;
10 use Illuminate\Support\Facades\DB; 13 use Illuminate\Support\Facades\DB;
11 use Psr\Container\ContainerExceptionInterface; 14 use Psr\Container\ContainerExceptionInterface;
12 use Psr\Container\NotFoundExceptionInterface; 15 use Psr\Container\NotFoundExceptionInterface;
@@ -30,11 +33,11 @@ class StatisticsLogic extends BaseLogic @@ -30,11 +33,11 @@ class StatisticsLogic extends BaseLogic
30 try { 33 try {
31 $this->date = request()->get('date') ?? null; 34 $this->date = request()->get('date') ?? null;
32 // 判断传入日期是否大于当月 35 // 判断传入日期是否大于当月
33 - if ($this->checkIsGreaterMonth($this->date)) { 36 + if (checkIsGreaterMonth($this->date)) {
34 $this->date = null; 37 $this->date = null;
35 } 38 }
36 // 判断传入日期是否是当月 39 // 判断传入日期是否是当月
37 - if ($this->checkIsMonth($this->date)) { 40 + if (checkIsMonth($this->date)) {
38 $this->date = null; 41 $this->date = null;
39 } 42 }
40 } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) { 43 } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
@@ -83,6 +86,57 @@ class StatisticsLogic extends BaseLogic @@ -83,6 +86,57 @@ class StatisticsLogic extends BaseLogic
83 } 86 }
84 87
85 /** 88 /**
  89 + * 获取当月的每天的流量趋势
  90 + * @return Builder[]|Collection
  91 + */
  92 + public function getDaysTrend()
  93 + {
  94 + $trafficTrends = new TrafficTrends();
  95 + $data = $trafficTrends->getMonthsLists($this->date);
  96 + $original_date = $data->pluck('date')->all();
  97 + // 获取当月的每天的日期
  98 + $month_date = getDateDays($this->date);
  99 + // 判断是否存在缺失的日期
  100 + if (count($month_date) > 1) {
  101 + // 去除当天的日期
  102 + array_pop($month_date);
  103 + // 获取缺失的日期
  104 + $diff = array_diff($month_date, $original_date);
  105 + if ($diff) {
  106 + // 获取缺失的数据并保存
  107 + $data = array_merge($data->toArray(), $this->getTrendsLists($diff));
  108 + }
  109 + }
  110 + $date = date('Y-m-d');
  111 + $ip_count = $this->customerVisit->getDayIPCount();
  112 + $pv_count = $this->customerVisit->getDayPVCount();
  113 + $data[] = compact('date', 'ip_count', 'pv_count');
  114 + return $data;
  115 + }
  116 +
  117 + /**
  118 + * 获取缺失的数据并保存
  119 + * @param array $dates 日期,格式:['y-m-d', 'y-m-d']
  120 + * @return array
  121 + */
  122 + public function getTrendsLists(array $dates)
  123 + {
  124 + $data = [];
  125 + $trafficTrends = new TrafficTrends();
  126 + foreach ($dates as $item) {
  127 + $date = $item;
  128 + $ip_count = $this->customerVisit->getDayIPCount($item);
  129 + $pv_count = $this->customerVisit->getDayPVCount($item);
  130 + $trafficTrends->day = $item;
  131 + $trafficTrends->ipnum = $ip_count;
  132 + $trafficTrends->pvnum = $pv_count;
  133 + $trafficTrends->save();
  134 + $data[] = compact('date', 'ip_count', 'pv_count');
  135 + }
  136 + return $data;
  137 + }
  138 +
  139 + /**
86 * @param $response 140 * @param $response
87 * @return bool 141 * @return bool
88 * @throws Throwable 142 * @throws Throwable
@@ -240,37 +294,4 @@ class StatisticsLogic extends BaseLogic @@ -240,37 +294,4 @@ class StatisticsLogic extends BaseLogic
240 // 判断传入日期是否大于当前日期 294 // 判断传入日期是否大于当前日期
241 return $timestamp > $now; 295 return $timestamp > $now;
242 } 296 }
243 -  
244 - /**  
245 - * 判断传入日期是否大于当月  
246 - * @param $date  
247 - * @return bool  
248 - */  
249 - public function checkIsGreaterMonth($date)  
250 - {  
251 - // 传入日期的时间戳  
252 - $timestamp = strtotime($date);  
253 - // 当前月份的时间戳  
254 - $nowMonth = strtotime(date('Y-m'));  
255 - // 判断传入日期是否大于当前月份  
256 - return $timestamp > $nowMonth;  
257 - }  
258 -  
259 - /**  
260 - * 判断传入日期是否是当月  
261 - * @param $date  
262 - * @return bool  
263 - */  
264 - public function checkIsMonth($date)  
265 - {  
266 - // 获取当前时间戳  
267 - $now = time();  
268 - // 获取当月的起始时间戳和结束时间戳  
269 - $firstDay = strtotime(date('Y-m-01', $now));  
270 - $lastDay = strtotime(date('Y-m-t', $now));  
271 - // 传入日期的时间戳  
272 - $timestamp = strtotime($date);  
273 - // 判断传入日期是否在当月范围内  
274 - return $timestamp >= $firstDay && $timestamp <= $lastDay;  
275 - }  
276 } 297 }
@@ -32,33 +32,50 @@ class TrafficTrends extends Base @@ -32,33 +32,50 @@ class TrafficTrends extends Base
32 protected $table = 'gl_traffic_trends'; 32 protected $table = 'gl_traffic_trends';
33 33
34 /** 34 /**
35 - * * 根据时间获取数据  
36 - * @param array|string $date 35 + * 根据时间获取数据
  36 + * @param string|null $date 日期,格式:Y-m-d
37 * @return TrafficTrends[]|Builder[]|Collection 37 * @return TrafficTrends[]|Builder[]|Collection
38 */ 38 */
39 - public function getDaysLists($date = null) 39 + public function getDaysLists(string $date = null)
40 { 40 {
41 $query = $this->query(); 41 $query = $this->query();
42 - if ($date != null) {  
43 - if (is_array($date)) {  
44 - $query->whereIn('day', $date);  
45 - } else {  
46 - $query->where('day', $date);  
47 - } 42 + if ($date != null && is_array($date)) {
  43 + $query->whereIn('day', $date);
48 } else { 44 } else {
49 - $query->whereYear('day', '=', date("Y"))  
50 - ->whereMonth('day', '=', date("m"))  
51 - ->whereDay('day', '=', date("d")); 45 + $query->where('day', $date);
52 } 46 }
53 return $query->get(); 47 return $query->get();
54 } 48 }
55 49
56 /** 50 /**
57 - * @param string|null $date 51 + * @param string|null $date 日期,格式:Y-m-d
58 * @return TrafficTrends|Builder|Model|object|null 52 * @return TrafficTrends|Builder|Model|object|null
59 */ 53 */
60 - public function getDay(string $date = null) 54 + public function getDay(string $date = null)
61 { 55 {
62 return $this->query()->where('day', $date ?? date('Y-m-d'))->first(); 56 return $this->query()->where('day', $date ?? date('Y-m-d'))->first();
63 } 57 }
  58 +
  59 + /**
  60 + * 获取当月的IP|PV数量
  61 + * @param string|null $date 日期,格式:Y-m
  62 + * @return Builder[]|Collection
  63 + */
  64 + public function getMonthsLists(string $date = null)
  65 + {
  66 + if (!is_null($date)) {
  67 + $dd = explode('-', $date);
  68 + $year = $dd[0];
  69 + $month = $dd[1];
  70 + } else {
  71 + $year = date('Y');
  72 + $month = date('m');
  73 + }
  74 + return $this->query()
  75 + ->selectRaw('day as date, pvnum as pv_count, ipnum as ip_count')
  76 + ->whereYear('day', '=', $year)
  77 + ->whereMonth('day', '=', $month)
  78 + ->orderBy('day', 'asc')
  79 + ->get();
  80 + }
64 } 81 }
@@ -5,6 +5,20 @@ @@ -5,6 +5,20 @@
5 5
6 use Illuminate\Support\Facades\Route; 6 use Illuminate\Support\Facades\Route;
7 7
  8 +// 流量统计
  9 +Route::prefix('stat')->group(function () {
  10 + // 访问来源
  11 + Route::get('/source', [\App\Http\Controllers\Bside\StatisticsController::class, 'source'])->name('stat_source');
  12 + // 地域分布
  13 + Route::get('/distribution', [\App\Http\Controllers\Bside\StatisticsController::class, 'distribution'])->name('stat_distribution');
  14 + // 受访页面
  15 + Route::get('/page', [\App\Http\Controllers\Bside\StatisticsController::class, 'page'])->name('stat_page');
  16 + // 访问终端
  17 + Route::get('/terminal', [\App\Http\Controllers\Bside\StatisticsController::class, 'terminal'])->name('stat_terminal');
  18 + // 流量趋势
  19 + Route::get('/trend', [\App\Http\Controllers\Bside\StatisticsController::class, 'trend'])->name('stat_trend');
  20 +});
  21 +
8 //必须登录验证的路由组 22 //必须登录验证的路由组
9 Route::middleware(['bloginauth'])->group(function () { 23 Route::middleware(['bloginauth'])->group(function () {
10 //登录用户编辑个人资料 24 //登录用户编辑个人资料
@@ -167,19 +181,7 @@ Route::middleware(['bloginauth'])->group(function () { @@ -167,19 +181,7 @@ Route::middleware(['bloginauth'])->group(function () {
167 Route::any('describe/delete', [\App\Http\Controllers\Bside\Product\DescribeController::class, 'delete'])->name('product_describe_delete'); 181 Route::any('describe/delete', [\App\Http\Controllers\Bside\Product\DescribeController::class, 'delete'])->name('product_describe_delete');
168 }); 182 });
169 183
170 - // 流量统计  
171 - Route::prefix('stat')->group(function () {  
172 - // 访问来源  
173 - Route::get('/source', [\App\Http\Controllers\Bside\StatisticsController::class, 'source'])->name('stat_source');  
174 - // 地域分布  
175 - Route::get('/distribution', [\App\Http\Controllers\Bside\StatisticsController::class, 'distribution'])->name('stat_distribution');  
176 - // 受访页面  
177 - Route::get('/page', [\App\Http\Controllers\Bside\StatisticsController::class, 'page'])->name('stat_page');  
178 - // 访问终端  
179 - Route::get('/terminal', [\App\Http\Controllers\Bside\StatisticsController::class, 'terminal'])->name('stat_terminal');  
180 - // 流量趋势  
181 - Route::get('/trend', [\App\Http\Controllers\Bside\StatisticsController::class, 'trend'])->name('stat_trend');  
182 - }); 184 +
183 185
184 //组织架构 186 //组织架构
185 Route::prefix('dept')->group(function () { 187 Route::prefix('dept')->group(function () {