作者 李美松

新增网站流量统计 - 2

  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Bside\Statistics;
  4 +
  5 +use App\Models\Bside\Statistics\TrafficStatistics;
  6 +use App\Models\CustomerVisit\CustomerVisit;
  7 +use Illuminate\Console\Command;
  8 +use Illuminate\Support\Facades\DB;
  9 +use Throwable;
  10 +
  11 +class Logic
  12 +{
  13 +
  14 + /**
  15 + * @param int $type 统计类型
  16 + * @return array
  17 + */
  18 + public function getMonths(int $type = TrafficStatistics::TYPE_SOURCE)
  19 + {
  20 + $date = getPreviousMonthsDate();
  21 + $TrafficStatistics = new TrafficStatistics();
  22 + $lists = $TrafficStatistics->getMonthsLists($type, $date);
  23 + if (!empty($lists)) {
  24 + $dd = [];
  25 + $lists->map(function ($item) use (&$dd) {
  26 + $dd[] = $item->year . '-' . str_pad($item->month, 2, '0', STR_PAD_LEFT);
  27 + });
  28 + $date = array_diff($date, $dd);
  29 + }
  30 + return $date;
  31 + }
  32 +
  33 + /**
  34 + * @param array $column 统计数据
  35 + * @param int $type 统计类型
  36 + * @param string|null $date 日期 格式:Y-m
  37 + * @param string $message
  38 + * @return array
  39 + * @throws Throwable
  40 + */
  41 + public function save(array $column, int $type = TrafficStatistics::TYPE_SOURCE, string $date = null, $message = '统计当月访问来源数据')
  42 + {
  43 + DB::beginTransaction();
  44 + // 获取当天的IP|PV数量
  45 + $customerVisit = new CustomerVisit();
  46 + $ip_count = $customerVisit->getMonthIPCount($date);
  47 + $pv_count = $customerVisit->getMonthPVCount($date);
  48 + $column['pvnum'] = $pv_count;
  49 + $column['ipnum'] = $ip_count;
  50 + $date = $date ?? date('Y-m');
  51 + // 统计数据并插入到数据库
  52 + $dd = explode('-', $date);
  53 + $year = $dd[0];
  54 + $month = $dd[1];
  55 + $column['year'] = $year;
  56 + $column['month'] = $month;
  57 + $trafficStatistics = new TrafficStatistics();
  58 + $isRes = $trafficStatistics->getMonth($type, $year, $month);
  59 + if ($isRes) {
  60 + $trafficStatistics = $isRes;
  61 + }
  62 + foreach ($column as $key => $value) {
  63 + $trafficStatistics->$key = $value;
  64 + }
  65 + $res = $trafficStatistics->save();
  66 + if ($res) {
  67 + DB::commit();
  68 + $meg = "{$date} - {$message}成功!";
  69 + } else {
  70 + DB::rollBack();
  71 + $meg = "{$date} - {$message}失败!";
  72 + }
  73 + return ['status' => $res, 'msg' => $meg];
  74 + }
  75 +}
  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Bside\Statistics;
  4 +
  5 +use App\Models\Bside\Statistics\TrafficTrends;
  6 +use App\Models\CustomerVisit\CustomerVisit;
  7 +use GuzzleHttp\Exception\GuzzleException;
  8 +use Illuminate\Console\Command;
  9 +use Illuminate\Support\Facades\DB;
  10 +use Throwable;
  11 +
  12 +class StatisticsDayTrend extends Command
  13 +{
  14 + public $error = 0;
  15 + /**
  16 + * The name and signature of the console command.
  17 + *
  18 + * @var string
  19 + */
  20 + protected $signature = 'statistics_day_trend';
  21 +
  22 + /**
  23 + * The console command description.
  24 + *
  25 + * @var string
  26 + */
  27 + protected $description = '统计当天流量趋势数据';
  28 +
  29 + /**
  30 + * @return void
  31 + * @throws GuzzleException
  32 + * @throws Throwable
  33 + */
  34 + public function handle()
  35 + {
  36 + $date = getPreviousDaysDate();
  37 + $trafficTrends = new TrafficTrends();
  38 + $lists = $trafficTrends->getDaysLists($date);
  39 + if (!empty($lists)) {
  40 + $date = array_diff($date, $lists->pluck('day')->all());
  41 + }
  42 + if (!empty($date)) {
  43 + foreach ($date as $value) {
  44 + echo $this->statistics_day_trend($value);
  45 + }
  46 + }
  47 + echo $this->statistics_day_trend();
  48 + }
  49 +
  50 + /**
  51 + * 统计当天流量趋势数据PV|IP数量
  52 + * @return int|mixed
  53 + * @throws GuzzleException|Throwable
  54 + */
  55 + protected function statistics_day_trend($date = null)
  56 + {
  57 + DB::beginTransaction();
  58 + $date = $date ?? date('Y-m-d');
  59 + // 获取当天的IP|PV数量
  60 + $customerVisit = new CustomerVisit();
  61 + $ip_count = $customerVisit->getDayIPCount($date);
  62 + $pv_count = $customerVisit->getDayPVCount($date);
  63 + // 统计数据并插入到数据库
  64 + $trafficTrends = new TrafficTrends();
  65 + $isRes = $trafficTrends->getDay($date);
  66 + if ($isRes) {
  67 + $trafficTrends = $isRes;
  68 + }
  69 + $trafficTrends->day = $date;
  70 + $trafficTrends->pvnum = $pv_count;
  71 + $trafficTrends->ipnum = $ip_count;
  72 + $res = $trafficTrends->save();
  73 + if ($res) {
  74 + DB::commit();
  75 + $this->info($date . ' - 统计当天流量趋势数据PV|IP数量成功');
  76 + } else {
  77 + DB::rollBack();
  78 + $this->error++;
  79 + $this->info($date . ' - 统计当天流量趋势数据PV|IP数量失败');
  80 + }
  81 + return $this->error;
  82 + }
  83 +}
  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Bside\Statistics;
  4 +
  5 +use App\Models\Bside\Statistics\TrafficStatistics;
  6 +use App\Models\CustomerVisit\CustomerVisit;
  7 +use Illuminate\Console\Command;
  8 +use Throwable;
  9 +
  10 +class StatisticsDistribution extends Command
  11 +{
  12 + public $error = 0;
  13 + /**
  14 + * The name and signature of the console command.
  15 + *
  16 + * @var string
  17 + */
  18 + protected $signature = 'statistics_distribution';
  19 +
  20 + /**
  21 + * The console command description.
  22 + *
  23 + * @var string
  24 + */
  25 + protected $description = '统计当月地域分布数据';
  26 +
  27 + public $logic;
  28 +
  29 + public function __construct()
  30 + {
  31 + parent::__construct();
  32 + $this->logic = new Logic();
  33 + }
  34 +
  35 + /**
  36 + * 定时执行
  37 + * @return void
  38 + * @throws Throwable
  39 + */
  40 + public function handle()
  41 + {
  42 + $date = $this->logic->getMonths(TrafficStatistics::TYPE_DISTRIBUTION);
  43 + if (!empty($date)) {
  44 + foreach ($date as $value) {
  45 + echo $this->statistics_distribution($value);
  46 + }
  47 + }
  48 + echo $this->statistics_distribution();
  49 + }
  50 +
  51 + /**
  52 + * 统计当天流量趋势数据PV|IP数量
  53 + * @param string|null $date
  54 + * @return int|mixed
  55 + * @throws Throwable
  56 + */
  57 + protected function statistics_distribution(string $date = null)
  58 + {
  59 + $customerVisit = new CustomerVisit();
  60 + $type = TrafficStatistics::TYPE_DISTRIBUTION;
  61 + $top = json_encode($customerVisit->getCountryCount($date) ?? []);
  62 + $res = $this->logic->save(compact('top', 'type'), $type, $date, '统计当月地域分布数据');
  63 + if (!$res['status']) {
  64 + $this->info($res['msg']);
  65 + $this->error++;
  66 + }
  67 + $this->info($res['msg']);
  68 + return $this->error;
  69 + }
  70 +}
  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Bside\Statistics;
  4 +
  5 +use App\Models\Bside\Statistics\TrafficStatistics;
  6 +use App\Models\CustomerVisit\CustomerVisit;
  7 +use Illuminate\Console\Command;
  8 +use Throwable;
  9 +
  10 +class StatisticsPage extends Command
  11 +{
  12 + public $error = 0;
  13 +
  14 + /**
  15 + * The name and signature of the console command.
  16 + *
  17 + * @var string
  18 + */
  19 + protected $signature = 'statistics_page';
  20 +
  21 + /**
  22 + * The console command description.
  23 + *
  24 + * @var string
  25 + */
  26 + protected $description = '统计当月受访页面数据';
  27 +
  28 + public $logic;
  29 +
  30 + public function __construct()
  31 + {
  32 + parent::__construct();
  33 + $this->logic = new Logic();
  34 + }
  35 +
  36 + /**
  37 + * 定时执行
  38 + * @return void
  39 + */
  40 + public function handle()
  41 + {
  42 + $date = $this->logic->getMonths(TrafficStatistics::TYPE_PAGE);
  43 + if (!empty($date)) {
  44 + foreach ($date as $value) {
  45 + echo $this->statistics_page($value);
  46 + }
  47 + }
  48 + echo $this->statistics_page();
  49 + }
  50 +
  51 + /**
  52 + * 统计当月受访页面数据
  53 + * @param null $date
  54 + * @return int|mixed
  55 + * @throws Throwable
  56 + */
  57 + protected function statistics_page($date = null)
  58 + {
  59 + $customerVisit = new CustomerVisit();
  60 + $top = json_encode($customerVisit->getPageCount($date) ?? []);
  61 + $type = TrafficStatistics::TYPE_PAGE;
  62 + $res = $this->logic->save(compact('top', 'type'), $type, $date, '统计当月受访页面数据');
  63 + if (!$res['status']) {
  64 + $this->info($res['msg']);
  65 + $this->error++;
  66 + }
  67 + $this->info($res['msg']);
  68 + return $this->error;
  69 + }
  70 +}
  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Bside\Statistics;
  4 +
  5 +use App\Models\Bside\Statistics\TrafficStatistics;
  6 +use App\Models\CustomerVisit\CustomerVisit;
  7 +use Illuminate\Console\Command;
  8 +use Throwable;
  9 +
  10 +class StatisticsSource extends Command
  11 +{
  12 + public $error = 0;
  13 + /**
  14 + * The name and signature of the console command.
  15 + *
  16 + * @var string
  17 + */
  18 + protected $signature = 'statistics_source';
  19 +
  20 + /**
  21 + * The console command description.
  22 + *
  23 + * @var string
  24 + */
  25 + protected $description = '统计当月访问来源数据';
  26 +
  27 + public $logic;
  28 +
  29 + public function __construct()
  30 + {
  31 + parent::__construct();
  32 + $this->logic = new Logic();
  33 + }
  34 +
  35 + /**
  36 + * @return void
  37 + * @throws Throwable
  38 + */
  39 + public function handle()
  40 + {
  41 + $date = $this->logic->getMonths();
  42 + if (!empty($date)) {
  43 + foreach ($date as $value) {
  44 + echo $this->statistics_source($value);
  45 + }
  46 + }
  47 + echo $this->statistics_source();
  48 + }
  49 +
  50 + /**
  51 + * 统计当天流量趋势数据
  52 + * @param string|null $date
  53 + * @return int|mixed
  54 + * @throws Throwable
  55 + */
  56 + protected function statistics_source(string $date = null)
  57 + {
  58 + // 获取当天的IP|PV数量
  59 + $customerVisit = new CustomerVisit();
  60 + $top = json_encode($customerVisit->getUrlCount($date) ?? []);
  61 + $type = TrafficStatistics::TYPE_SOURCE;
  62 + $res = $this->logic->save(compact('top', 'type'), $type, $date);
  63 + if (!$res['status']) {
  64 + $this->info($res['msg']);
  65 + $this->error++;
  66 + }
  67 + $this->info($res['msg']);
  68 + return $this->error;
  69 + }
  70 +}
  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Bside\Statistics;
  4 +
  5 +use App\Models\Bside\Statistics\TrafficStatistics;
  6 +use App\Models\CustomerVisit\CustomerVisit;
  7 +use GuzzleHttp\Exception\GuzzleException;
  8 +use Illuminate\Console\Command;
  9 +use Throwable;
  10 +
  11 +class StatisticsTerminal extends Command
  12 +{
  13 + public $error = 0;
  14 +
  15 + /**
  16 + * The name and signature of the console command.
  17 + *
  18 + * @var string
  19 + */
  20 + protected $signature = 'statistics_terminal';
  21 +
  22 + /**
  23 + * The console command description.
  24 + *
  25 + * @var string
  26 + */
  27 + protected $description = '统计当月访问终端数据';
  28 +
  29 + public $logic;
  30 +
  31 + public function __construct()
  32 + {
  33 + parent::__construct();
  34 + $this->logic = new Logic();
  35 + }
  36 +
  37 + /**
  38 + * @return void
  39 + * @throws Throwable
  40 + */
  41 + public function handle()
  42 + {
  43 + $date = $this->logic->getMonths(TrafficStatistics::TYPE_TERMINAL);
  44 + if (!empty($date)) {
  45 + foreach ($date as $value) {
  46 + echo $this->statistics_terminal($value);
  47 + }
  48 + }
  49 + echo $this->statistics_terminal();
  50 + }
  51 +
  52 + /**
  53 + * 统计当月访问终端数据
  54 + * @param string|null $date
  55 + * @return int|mixed
  56 + * @throws Throwable
  57 + */
  58 + protected function statistics_terminal(string $date = null)
  59 + {
  60 + $customerVisit = new CustomerVisit();
  61 + $pcnum = $customerVisit->getTerminalPcCount($date);
  62 + $mbnum = $customerVisit->getTerminalMobileCount($date);
  63 + $top = json_encode($customerVisit->getCountryCount($date) ?? []);
  64 + $type = TrafficStatistics::TYPE_TERMINAL;
  65 + $res = $this->logic->save(compact('top', 'type', 'pcnum', 'mbnum'), $type, $date, '统计当月访问终端数据');
  66 + if (!$res['status']) {
  67 + $this->info($res['msg']);
  68 + $this->error++;
  69 + }
  70 + $this->info($res['msg']);
  71 + return $this->error;
  72 + }
  73 +}
@@ -3,12 +3,14 @@ @@ -3,12 +3,14 @@
3 namespace App\Console\Commands\Bside\Statistics; 3 namespace App\Console\Commands\Bside\Statistics;
4 4
5 use App\Models\Bside\Statistics\TrafficStatistics; 5 use App\Models\Bside\Statistics\TrafficStatistics;
6 -use App\Models\CustomerVisit\CustomerVisit; 6 +use GuzzleHttp\Exception\GuzzleException;
7 use Illuminate\Console\Command; 7 use Illuminate\Console\Command;
  8 +use Throwable;
8 9
9 class StatisticsTrend extends Command 10 class StatisticsTrend extends Command
10 { 11 {
11 public $error = 0; 12 public $error = 0;
  13 +
12 /** 14 /**
13 * The name and signature of the console command. 15 * The name and signature of the console command.
14 * 16 *
@@ -21,44 +23,52 @@ class StatisticsTrend extends Command @@ -21,44 +23,52 @@ class StatisticsTrend extends Command
21 * 23 *
22 * @var string 24 * @var string
23 */ 25 */
24 - protected $description = '统计当天流量趋势数据PV|IP数量'; 26 + protected $description = '统计当月流量趋势数据';
  27 +
  28 + public $logic;
  29 +
  30 + public function __construct()
  31 + {
  32 + parent::__construct();
  33 + $this->logic = new Logic();
  34 + }
25 35
26 /** 36 /**
27 - * @name :(定时执行)handle  
28 - * @author :lyh  
29 - * @method :post  
30 - * @time :2023/5/12 14:48 37 + * 定时执行
  38 + * @return void
  39 + * @throws GuzzleException|Throwable
31 */ 40 */
32 public function handle() 41 public function handle()
33 { 42 {
  43 + $date = $this->logic->getMonths(TrafficStatistics::TYPE_TREND);
  44 + if (!empty($date)) {
  45 + foreach ($date as $value) {
  46 + echo $this->statistics_trend($value);
  47 + }
  48 + }
34 echo $this->statistics_trend(); 49 echo $this->statistics_trend();
35 } 50 }
36 51
37 /** 52 /**
38 - * 统计当天流量趋势数据PV|IP数量 53 + * 统计当天流量趋势数据
  54 + * @param string|null $date
39 * @return int|mixed 55 * @return int|mixed
  56 + * @throws GuzzleException|Throwable
40 */ 57 */
41 - protected function statistics_trend() 58 + protected function statistics_trend(string $date = null)
42 { 59 {
43 - $customerVisit = new CustomerVisit();  
44 - $date = date('Y-m-d');  
45 - $ip_count = $customerVisit->getDayIPCount();  
46 - $pv_count = $customerVisit->getDayPVCount();  
47 - $trafficStatistics = new TrafficStatistics();  
48 - $trafficStatistics->type = TrafficStatistics::TYPE_TREND;  
49 - $trafficStatistics->  
50 - $data = [  
51 - 'date' => $date,  
52 - 'ip_count' => $ip_count,  
53 - 'pv_count' => $pv_count,  
54 - ];  
55 -// $res = $customerVisit->insert($data);  
56 - if ($res) {  
57 - $this->info('统计当天流量趋势数据PV|IP数量成功');  
58 - } else { 60 + $domain = request()->getHttpHost(); //'www.wowstainless.com';
  61 + $sta_date = !is_null($date) ? $date . "-01" : date('Y-m-01');
  62 + $inquiry = getInquiryInformation($domain, $sta_date);
  63 + $innum = $inquiry['count'] ?? 0;
  64 + $top = json_encode($inquiry['lists'] ?? []);
  65 + $type = TrafficStatistics::TYPE_TREND;
  66 + $res = $this->logic->save(compact('top', 'type', 'innum'), $type, $date, '统计当月流量趋势数据');
  67 + if (!$res['status']) {
  68 + $this->info($res['msg']);
59 $this->error++; 69 $this->error++;
60 - $this->info('统计当天流量趋势数据PV|IP数量失败');  
61 } 70 }
  71 + $this->info($res['msg']);
62 return $this->error; 72 return $this->error;
63 } 73 }
64 } 74 }
1 <?php 1 <?php
2 2
3 -namespace App\Console\Commands\Bside\Domain; 3 +namespace App\Console\Commands\Domain;
4 4
5 use App\Exceptions\AsideGlobalException; 5 use App\Exceptions\AsideGlobalException;
6 use App\Exceptions\BsideGlobalException; 6 use App\Exceptions\BsideGlobalException;
7 use App\Http\Logic\Aside\Domain\DomainInfoLogic; 7 use App\Http\Logic\Aside\Domain\DomainInfoLogic;
  8 +use GuzzleHttp\Exception\GuzzleException;
8 use Illuminate\Console\Command; 9 use Illuminate\Console\Command;
9 10
10 class DomainTime extends Command 11 class DomainTime extends Command
@@ -39,7 +40,7 @@ class DomainTime extends Command @@ -39,7 +40,7 @@ class DomainTime extends Command
39 * 更新域名|证书到期时间 40 * 更新域名|证书到期时间
40 * @return int|mixed|void 41 * @return int|mixed|void
41 * @throws AsideGlobalException 42 * @throws AsideGlobalException
42 - * @throws BsideGlobalException 43 + * @throws BsideGlobalException|GuzzleException
43 */ 44 */
44 protected function update_domain_time() 45 protected function update_domain_time()
45 { 46 {
@@ -27,7 +27,23 @@ class Kernel extends ConsoleKernel @@ -27,7 +27,23 @@ class Kernel extends ConsoleKernel
27 $schedule->command('web_traffic 1')->everyThirtyMinutes(); // 引流 1-3个月的项目,半小时一次 27 $schedule->command('web_traffic 1')->everyThirtyMinutes(); // 引流 1-3个月的项目,半小时一次
28 $schedule->command('web_traffic 2')->cron('*/18 * * * *'); // 引流 4-8个月的项目,18分钟一次 28 $schedule->command('web_traffic 2')->cron('*/18 * * * *'); // 引流 4-8个月的项目,18分钟一次
29 $schedule->command('web_traffic 3')->cron('*/12 * * * *'); // 引流 大于9个月的项目,12分钟一次 29 $schedule->command('web_traffic 3')->cron('*/12 * * * *'); // 引流 大于9个月的项目,12分钟一次
30 -// $schedule->command('domain_time')->dailyAt('01:00')->withoutOverlapping(1); // 更新域名|证书结束时间,每天凌晨1点执行一次 30 + // 更新域名|证书结束时间,每天凌晨1点执行一次
  31 +// $schedule->command('domain_time')->dailyAt('01:00')->withoutOverlapping(1);
  32 + // B站 - 网站数据统计
  33 + // 获取当前月份最后一天
  34 + $lastDay = date('Y-m-t');
  35 + // 统计当月访问来源数据,每月最后一天23:59点执行一次
  36 + $schedule->command('statistics_source')->monthlyOn($lastDay, '23:59')->withoutOverlapping(1);
  37 + // 统计当月地域分布数据,每月最后一天23:59点执行一次
  38 + $schedule->command('statistics_distribution')->monthlyOn($lastDay, '23:59')->withoutOverlapping(1);
  39 + // 统计当月受访页面数据,每月最后一天23:59点执行一次
  40 + $schedule->command('statistics_page')->monthlyOn($lastDay, '23:59')->withoutOverlapping(1);
  41 + // 统计当月访问终端数据,每月最后一天23:59点执行一次
  42 + $schedule->command('statistics_terminal')->monthlyOn($lastDay, '23:59')->withoutOverlapping(1);
  43 + // 统计当月流量趋势数据,每月最后一天23:59点执行一次
  44 + $schedule->command('statistics_trend')->monthlyOn($lastDay, '23:59')->withoutOverlapping(1);
  45 + // 统计当天流量趋势数据,每天23:59点执行一次
  46 + $schedule->command('statistics_day_trend')->dailyAt('23:59')->withoutOverlapping(1);
31 } 47 }
32 48
33 /** 49 /**
1 <?php 1 <?php
2 2
3 use App\Utils\LogUtils; 3 use App\Utils\LogUtils;
  4 +use GuzzleHttp\Client;
  5 +use GuzzleHttp\Exception\GuzzleException;
4 use Illuminate\Support\Carbon; 6 use Illuminate\Support\Carbon;
5 7
6 -define('HTTP_OPENAI_URL','http://openai.waimaoq.com/'); 8 +define('HTTP_OPENAI_URL', 'http://openai.waimaoq.com/');
7 /** 9 /**
8 * 生成路由标识 10 * 生成路由标识
9 * @param $string 11 * @param $string
@@ -28,7 +30,8 @@ if (!function_exists('generateRoute')) { @@ -28,7 +30,8 @@ if (!function_exists('generateRoute')) {
28 * @author zbj 30 * @author zbj
29 * @date 2023/4/27 31 * @date 2023/4/27
30 */ 32 */
31 -function errorLog($title, $params, Throwable $exception){ 33 +function errorLog($title, $params, Throwable $exception)
  34 +{
32 $exceptionMessage = "错误CODE:" . $exception->getCode() . 35 $exceptionMessage = "错误CODE:" . $exception->getCode() .
33 "-----错误message:" . $exception->getMessage() . 36 "-----错误message:" . $exception->getMessage() .
34 '------错误文件:' . $exception->getFile() . 37 '------错误文件:' . $exception->getFile() .
@@ -37,15 +40,15 @@ function errorLog($title, $params, Throwable $exception){ @@ -37,15 +40,15 @@ function errorLog($title, $params, Throwable $exception){
37 LogUtils::error($title, $params, $exceptionMessage); 40 LogUtils::error($title, $params, $exceptionMessage);
38 } 41 }
39 42
40 -if(!function_exists('http_post')){ 43 +if (!function_exists('http_post')) {
41 /** 44 /**
42 * 发送http post请求 45 * 发送http post请求
43 * @param type $url 46 * @param type $url
44 * @param type $post_data 47 * @param type $post_data
45 */ 48 */
46 - function http_post($url, $post_data,$header = []) 49 + function http_post($url, $post_data, $header = [])
47 { 50 {
48 - if(empty($header)){ 51 + if (empty($header)) {
49 $header = array( 52 $header = array(
50 "Accept: application/json", 53 "Accept: application/json",
51 "Content-Type:application/json;charset=utf-8", 54 "Content-Type:application/json;charset=utf-8",
@@ -64,22 +67,22 @@ if(!function_exists('http_post')){ @@ -64,22 +67,22 @@ if(!function_exists('http_post')){
64 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 67 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
65 $res = curl_exec($ch); 68 $res = curl_exec($ch);
66 if (curl_errno($ch)) { 69 if (curl_errno($ch)) {
67 - \Illuminate\Support\Facades\Log::write(print_r(curl_errno($ch),1),'debug---1'); 70 + \Illuminate\Support\Facades\Log::write(print_r(curl_errno($ch), 1), 'debug---1');
68 } 71 }
69 curl_close($ch); 72 curl_close($ch);
70 return json_decode($res, true); 73 return json_decode($res, true);
71 } 74 }
72 } 75 }
73 76
74 -if(!function_exists('http_get')){ 77 +if (!function_exists('http_get')) {
75 /** 78 /**
76 * 发送http get请求 79 * 发送http get请求
77 * @param type $url 80 * @param type $url
78 * @return type 81 * @return type
79 */ 82 */
80 - function http_get($url,$header = []) 83 + function http_get($url, $header = [])
81 { 84 {
82 - if(empty($header)){ 85 + if (empty($header)) {
83 $header[] = "content-type: application/json; 86 $header[] = "content-type: application/json;
84 charset = UTF-8"; 87 charset = UTF-8";
85 } 88 }
@@ -98,7 +101,7 @@ if(!function_exists('http_get')){ @@ -98,7 +101,7 @@ if(!function_exists('http_get')){
98 } 101 }
99 102
100 103
101 -if(!function_exists('_get_child')){ 104 +if (!function_exists('_get_child')) {
102 /** 105 /**
103 * 菜单权限->得到子级数组 106 * 菜单权限->得到子级数组
104 * @param int 107 * @param int
@@ -110,7 +113,7 @@ if(!function_exists('_get_child')){ @@ -110,7 +113,7 @@ if(!function_exists('_get_child')){
110 foreach ($arr as $k => $v) { 113 foreach ($arr as $k => $v) {
111 $v = (array)$v; 114 $v = (array)$v;
112 if ($v['pid'] == $my_id) { 115 if ($v['pid'] == $my_id) {
113 - $v['sub'] = _get_child($v['id'],$arr); 116 + $v['sub'] = _get_child($v['id'], $arr);
114 $new_arr[] = $v; 117 $new_arr[] = $v;
115 } 118 }
116 } 119 }
@@ -119,7 +122,6 @@ if(!function_exists('_get_child')){ @@ -119,7 +122,6 @@ if(!function_exists('_get_child')){
119 } 122 }
120 123
121 124
122 -  
123 if (!function_exists('checkDomain')) { 125 if (!function_exists('checkDomain')) {
124 /** 126 /**
125 * 检查并补全域名协议 127 * 检查并补全域名协议
@@ -130,12 +132,12 @@ if (!function_exists('checkDomain')) { @@ -130,12 +132,12 @@ if (!function_exists('checkDomain')) {
130 function checkDomain($value) 132 function checkDomain($value)
131 { 133 {
132 $urlParts = parse_url(strtolower($value)); 134 $urlParts = parse_url(strtolower($value));
133 - if(empty($urlParts['host'])){ 135 + if (empty($urlParts['host'])) {
134 $urlParts = parse_url('https://' . $value); 136 $urlParts = parse_url('https://' . $value);
135 } 137 }
136 - $host = $urlParts['host'] ?? ''; 138 + $host = $urlParts['host'] ?? '';
137 $scheme = $urlParts['scheme'] ?? 'https'; 139 $scheme = $urlParts['scheme'] ?? 'https';
138 - if(!in_array($scheme, ['http', 'https'])){ 140 + if (!in_array($scheme, ['http', 'https'])) {
139 return false; 141 return false;
140 } 142 }
141 if (preg_match('/^(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,6}$/', $host)) { 143 if (preg_match('/^(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,6}$/', $host)) {
@@ -147,7 +149,6 @@ if (!function_exists('checkDomain')) { @@ -147,7 +149,6 @@ if (!function_exists('checkDomain')) {
147 } 149 }
148 150
149 151
150 -  
151 /** 152 /**
152 * 把返回的数据集转换成Tree 153 * 把返回的数据集转换成Tree
153 * @param $list array 数据列表 154 * @param $list array 数据列表
@@ -158,20 +159,21 @@ if (!function_exists('checkDomain')) { @@ -158,20 +159,21 @@ if (!function_exists('checkDomain')) {
158 * @param bool $empty_child 当子数据不存在,是否要返回空子数据 159 * @param bool $empty_child 当子数据不存在,是否要返回空子数据
159 * @return array 160 * @return array
160 */ 161 */
161 -function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0,$empty_child=true) { 162 +function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0, $empty_child = true)
  163 +{
162 // 如果是数字,则是root 164 // 如果是数字,则是root
163 - if(is_numeric($pk)){  
164 - $root = $pk;  
165 - $pk = 'id'; 165 + if (is_numeric($pk)) {
  166 + $root = $pk;
  167 + $pk = 'id';
166 } 168 }
167 // 创建Tree 169 // 创建Tree
168 $tree = array(); 170 $tree = array();
169 - if(is_array($list)) { 171 + if (is_array($list)) {
170 // 创建基于主键的数组引用 172 // 创建基于主键的数组引用
171 $refer = array(); 173 $refer = array();
172 foreach ($list as $key => $data) { 174 foreach ($list as $key => $data) {
173 - if($empty_child){  
174 - $list[$key][$child] = []; 175 + if ($empty_child) {
  176 + $list[$key][$child] = [];
175 } 177 }
176 $refer[$data[$pk]] =& $list[$key]; 178 $refer[$data[$pk]] =& $list[$key];
177 } 179 }
@@ -180,9 +182,9 @@ function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0,$em @@ -180,9 +182,9 @@ function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0,$em
180 $parentId = $data[$pid]; 182 $parentId = $data[$pid];
181 if ($root == $parentId) { 183 if ($root == $parentId) {
182 $tree[] =& $list[$key]; 184 $tree[] =& $list[$key];
183 - }else{ 185 + } else {
184 if (isset($refer[$parentId])) { 186 if (isset($refer[$parentId])) {
185 - $refer[$parentId][$child][] = & $list[$key]; 187 + $refer[$parentId][$child][] = &$list[$key];
186 } 188 }
187 } 189 }
188 } 190 }
@@ -198,14 +200,15 @@ function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0,$em @@ -198,14 +200,15 @@ function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0,$em
198 * @author:dc 200 * @author:dc
199 * @time 2022/1/11 10:13 201 * @time 2022/1/11 10:13
200 */ 202 */
201 -function tree_to_list($tree, $child='_child'){  
202 - $lists = [];  
203 - foreach ($tree as $item){  
204 - $c = $item[$child]??[]; 203 +function tree_to_list($tree, $child = '_child')
  204 +{
  205 + $lists = [];
  206 + foreach ($tree as $item) {
  207 + $c = $item[$child] ?? [];
205 unset($item[$child]); 208 unset($item[$child]);
206 $lists[] = $item; 209 $lists[] = $item;
207 - if ($c){  
208 - $lists = array_merge($lists,tree_to_list($c, $child)); 210 + if ($c) {
  211 + $lists = array_merge($lists, tree_to_list($c, $child));
209 } 212 }
210 } 213 }
211 return $lists; 214 return $lists;
@@ -233,10 +236,10 @@ if (!function_exists('object_to_array')) { @@ -233,10 +236,10 @@ if (!function_exists('object_to_array')) {
233 */ 236 */
234 function object_to_array($data) 237 function object_to_array($data)
235 { 238 {
236 - if(is_object($data)){ 239 + if (is_object($data)) {
237 $data = (array)$data; 240 $data = (array)$data;
238 - }else{  
239 - foreach ($data as $k => $v){ 241 + } else {
  242 + foreach ($data as $k => $v) {
240 $data[$k] = object_to_array($v); 243 $data[$k] = object_to_array($v);
241 } 244 }
242 } 245 }
@@ -244,3 +247,76 @@ if (!function_exists('object_to_array')) { @@ -244,3 +247,76 @@ if (!function_exists('object_to_array')) {
244 } 247 }
245 } 248 }
246 249
  250 +if (!function_exists('getPreviousDaysDate')) {
  251 + /**
  252 + * 获取当前指定前几天日期,默认获取前三天日期
  253 + * @param int $day
  254 + * @return array
  255 + */
  256 + function getPreviousDaysDate(int $day = 3)
  257 + {
  258 + $days = [];
  259 + while ($day > 0) {
  260 + $days[] = date("Y-m-d", strtotime("-{$day} days"));
  261 + $day -= 1;
  262 + }
  263 + return $days;
  264 + }
  265 +}
  266 +
  267 +if (!function_exists('getPreviousMonthsDate')) {
  268 + /**
  269 + * 获取当前指定前几天日期,默认获取前三天日期
  270 + * @param int $month
  271 + * @return array
  272 + */
  273 + function getPreviousMonthsDate(int $month = 3)
  274 + {
  275 + $months = [];
  276 + while ($month > 0) {
  277 + $months[] = date("Y-m", strtotime("-{$month} months"));
  278 + $month -= 1;
  279 + }
  280 + return $months;
  281 + }
  282 +}
  283 +
  284 +if (!function_exists('getInquiryInformation')) {
  285 + /**
  286 + * 获取第三方询盘信息
  287 + * @return array|string
  288 + * @throws GuzzleException
  289 + */
  290 + function getInquiryInformation($domain, $sta_date)
  291 + {
  292 + $token = md5($domain . date("Y-m-d"));
  293 + $source = '1,3';
  294 + $url = "https://form.globalso.com/api/external-interface/country_con/15243d63ed5a5738?domain={$domain}&token={$token}&source={$source}&sta_date={$sta_date}";
  295 + $client = new Client(['verify' => false]);
  296 + $http = $client->get($url);
  297 + $data = [];
  298 + if ($http->getStatusCode() != 200) {
  299 + return $data;
  300 + }
  301 + $content = $http->getBody()->getContents();
  302 + $json = json_decode($content, true);
  303 + if ($json['status'] != 200) {
  304 + return $content;
  305 + }
  306 + $data['count'] = $json['data']['count'];
  307 + $data['lists'] = $json['data']['data'];
  308 + return $data;
  309 + }
  310 +}
  311 +
  312 +if (!function_exists('stringUnderlineLowercase')) {
  313 + /**
  314 + * 正则 - 名字转为小写并将空格转为下划线
  315 + * @param $name
  316 + * @return string
  317 + */
  318 + function stringUnderlineLowercase($name)
  319 + {
  320 + return trim(strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $name)));
  321 + }
  322 +}
@@ -3,9 +3,8 @@ @@ -3,9 +3,8 @@
3 namespace App\Http\Controllers\Bside; 3 namespace App\Http\Controllers\Bside;
4 4
5 5
6 -use App\Models\CustomerVisit\CustomerVisit;  
7 -use GuzzleHttp\Client;  
8 -use GuzzleHttp\Exception\GuzzleException; 6 +use App\Http\Logic\Bside\Statistics\StatisticsLogic;
  7 +use App\Models\Bside\Statistics\TrafficStatistics;
9 use Illuminate\Http\JsonResponse; 8 use Illuminate\Http\JsonResponse;
10 use Psr\Container\ContainerExceptionInterface; 9 use Psr\Container\ContainerExceptionInterface;
11 use Psr\Container\NotFoundExceptionInterface; 10 use Psr\Container\NotFoundExceptionInterface;
@@ -17,136 +16,80 @@ use Psr\Container\NotFoundExceptionInterface; @@ -17,136 +16,80 @@ use Psr\Container\NotFoundExceptionInterface;
17 */ 16 */
18 class StatisticsController extends BaseController 17 class StatisticsController extends BaseController
19 { 18 {
20 - private $customerVisit; 19 + public $statisticsLogic;
  20 +
  21 + public $trafficStatistics;
21 22
22 public function __construct() 23 public function __construct()
23 { 24 {
24 - $this->customerVisit = new CustomerVisit(); 25 + $this->statisticsLogic = new StatisticsLogic();
  26 + $this->trafficStatistics = new TrafficStatistics();
25 } 27 }
26 28
27 /** 29 /**
28 * 访问来源 30 * 访问来源
29 * @return JsonResponse 31 * @return JsonResponse
  32 + * @throws ContainerExceptionInterface
  33 + * @throws NotFoundExceptionInterface
30 */ 34 */
31 public function source() 35 public function source()
32 { 36 {
33 - $pv_count = $this->customerVisit->getMonthPVCount();  
34 - $ip_count = $this->customerVisit->getMonthIPCount();  
35 - $lists = $this->customerVisit->getUrlCount();  
36 - return $this->success(compact('pv_count', 'ip_count', 'lists')); 37 + $type = $this->trafficStatistics::TYPE_SOURCE;
  38 + $response = $this->statisticsLogic->getStatisticsData($type);
  39 + return $this->success($response);
37 } 40 }
38 41
39 /** 42 /**
40 * 地域分布 43 * 地域分布
41 * @return JsonResponse 44 * @return JsonResponse
  45 + * @throws ContainerExceptionInterface
  46 + * @throws NotFoundExceptionInterface
42 */ 47 */
43 public function distribution() 48 public function distribution()
44 { 49 {
45 - $pv_count = $this->customerVisit->getMonthPVCount();  
46 - $ip_count = $this->customerVisit->getMonthIPCount();  
47 - $lists = $this->customerVisit->getCountryCount();  
48 - return $this->success(compact('pv_count', 'ip_count', 'lists')); 50 + $type = $this->trafficStatistics::TYPE_DISTRIBUTION;
  51 + $response = $this->statisticsLogic->getStatisticsData($type);
  52 + return $this->success($response);
49 } 53 }
50 54
51 /** 55 /**
52 * 受访页面 56 * 受访页面
53 * @return JsonResponse 57 * @return JsonResponse
  58 + * @throws ContainerExceptionInterface
  59 + * @throws NotFoundExceptionInterface
54 */ 60 */
55 public function page() 61 public function page()
56 { 62 {
57 - $pv_count = $this->customerVisit->getMonthPVCount();  
58 - $ip_count = $this->customerVisit->getMonthIPCount();  
59 - $lists = $this->customerVisit->getPageCount();  
60 - return $this->success(compact('pv_count', 'ip_count', 'lists')); 63 + $type = $this->trafficStatistics::TYPE_PAGE;
  64 + $response = $this->statisticsLogic->getStatisticsData($type);
  65 + return $this->success($response);
61 } 66 }
62 67
63 /** 68 /**
64 * 访问终端 69 * 访问终端
65 * @return JsonResponse 70 * @return JsonResponse
  71 + * @throws ContainerExceptionInterface
  72 + * @throws NotFoundExceptionInterface
66 */ 73 */
67 public function terminal() 74 public function terminal()
68 { 75 {
69 - $pv_count = $this->customerVisit->getMonthPVCount();  
70 - $ip_count = $this->customerVisit->getMonthIPCount();  
71 - $pc_count = $this->customerVisit->getTerminalPcCount();  
72 - $mobile_count = $this->customerVisit->getTerminalMobileCount();  
73 - $lists = $this->customerVisit->getCountryCount();  
74 - return $this->success(compact('pv_count', 'ip_count', 'pc_count', 'mobile_count', 'lists')); 76 + $type = $this->trafficStatistics::TYPE_TERMINAL;
  77 + $response = $this->statisticsLogic->getStatisticsData($type);
  78 + return $this->success($response);
75 } 79 }
76 80
77 /** 81 /**
78 * 流量趋势 82 * 流量趋势
79 * @return JsonResponse 83 * @return JsonResponse
80 * @throws ContainerExceptionInterface 84 * @throws ContainerExceptionInterface
81 - * @throws GuzzleException  
82 * @throws NotFoundExceptionInterface 85 * @throws NotFoundExceptionInterface
83 */ 86 */
84 public function trend() 87 public function trend()
85 { 88 {
86 -// dd($this->customerVisit->getDayIPCount());  
87 - $pv_count = $this->customerVisit->getMonthPVCount();  
88 - $ip_count = $this->customerVisit->getMonthIPCount();  
89 - $inquiry = $this->getCache('inquiry_information');  
90 - $inquiry_count = $inquiry['count'] ?? 0;  
91 - $lists = $inquiry['lists'] ?? [];  
92 - return $this->success(compact('pv_count', 'ip_count', 'inquiry_count', 'lists'));  
93 - }  
94 -  
95 - /**  
96 - * 获取缓存  
97 - * @param string $key 缓存键名  
98 - * @param float|int $time 缓存时间,默认2小时  
99 - * @return mixed  
100 - * @throws ContainerExceptionInterface  
101 - * @throws GuzzleException  
102 - * @throws NotFoundExceptionInterface  
103 - */  
104 - public function getCache(string $key, $time = 60 * 60 * 2)  
105 - {  
106 - $domain = request()->getHttpHost(); //'www.wowstainless.com';  
107 - $sta_date = date('Y-m-01');  
108 - $key = $key . '_' . $this->stringUnderlineLowercase($domain) . '_' . str_replace('-', '_', $sta_date);  
109 - $value = cache()->get($key);  
110 - if (!$value) {  
111 - $value = $this->getInquiryInformation($domain, $sta_date);  
112 - cache()->put($key, $value, $time);  
113 - }  
114 - return $value; 89 + $type = $this->trafficStatistics::TYPE_TREND;
  90 + $response = $this->statisticsLogic->getStatisticsData($type);
  91 + return $this->success($response);
115 } 92 }
116 93
117 - /**  
118 - * 正则 - 名字转为小写并将空格转为下划线  
119 - * @param $name  
120 - * @return string  
121 - */  
122 - public function stringUnderlineLowercase($name)  
123 - {  
124 - return trim(strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $name)));  
125 - }  
126 94
127 - /**  
128 - * 获取第三方询盘信息  
129 - * @return array|string  
130 - * @throws GuzzleException  
131 - */  
132 - public function getInquiryInformation($domain, $sta_date)  
133 - {  
134 - $token = md5($domain . date("Y-m-d"));  
135 - $source = '1,3';  
136 - $url = "https://form.globalso.com/api/external-interface/country_con/15243d63ed5a5738?domain={$domain}&token={$token}&source={$source}&sta_date={$sta_date}";  
137 - $client = new Client(['verify' => false]);  
138 - $http = $client->get($url);  
139 - $data = [];  
140 - if ($http->getStatusCode() != 200) {  
141 - return $data;  
142 - }  
143 - $content = $http->getBody()->getContents();  
144 - $json = json_decode($content, true);  
145 - if ($json['status'] != 200) {  
146 - return $content;  
147 - }  
148 - $data['count'] = $json['data']['count'];  
149 - $data['lists'] = $json['data']['data'];  
150 - return $data;  
151 - }  
152 } 95 }
  1 +<?php
  2 +
  3 +namespace App\Http\Logic\Bside\Statistics;
  4 +
  5 +use App\Http\Logic\Bside\BaseLogic;
  6 +use App\Models\Bside\Statistics\TrafficStatistics;
  7 +use App\Models\CustomerVisit\CustomerVisit;
  8 +use GuzzleHttp\Exception\GuzzleException;
  9 +use Illuminate\Contracts\Cache\Repository;
  10 +use Illuminate\Support\Facades\DB;
  11 +use Psr\Container\ContainerExceptionInterface;
  12 +use Psr\Container\NotFoundExceptionInterface;
  13 +use Throwable;
  14 +
  15 +class StatisticsLogic extends BaseLogic
  16 +{
  17 + public $trafficStatistics;
  18 +
  19 + private $customerVisit;
  20 +
  21 + public $date;
  22 +
  23 + public $time = 60 * 60 * 2;
  24 +
  25 + public function __construct()
  26 + {
  27 + parent::__construct();
  28 + $this->customerVisit = new CustomerVisit();
  29 + $this->trafficStatistics = new TrafficStatistics();
  30 + try {
  31 + $this->date = request()->get('date') ?? null;
  32 + // 判断传入日期是否大于当月
  33 + if ($this->checkIsGreaterMonth($this->date)) {
  34 + $this->date = null;
  35 + }
  36 + // 判断传入日期是否是当月
  37 + if ($this->checkIsMonth($this->date)) {
  38 + $this->date = null;
  39 + }
  40 + } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
  41 + $this->date = null;
  42 + }
  43 + }
  44 +
  45 + /**
  46 + * @param $type
  47 + * @return array|Repository|mixed
  48 + * @throws ContainerExceptionInterface
  49 + * @throws NotFoundExceptionInterface|Throwable
  50 + */
  51 + public function getStatisticsData($type)
  52 + {
  53 + $function = debug_backtrace()[1]['function'];
  54 + // 判断是否查询当月数据
  55 + if (!is_null($this->date)) {
  56 + // 获取缓存名
  57 + $key = $this->cacheName($type, $this->date);
  58 + $response = cache()->get($key);
  59 + if (empty($response)) {
  60 + $dd = explode('-', $this->date);
  61 + $year = $dd[0] ?? null;
  62 + $month = str_pad($dd[1], 2, '0', STR_PAD_LEFT) ?? null;
  63 + // 获取当前数据是否存在
  64 + $data = $this->trafficStatistics->getData($type, $year, $month);
  65 + if (is_null($data)) {
  66 + $response = $this->$function();
  67 + // 存在则直接返回
  68 + $response_data = array_filter($response);
  69 + if ($response_data) {
  70 + // 不存在则请求接口获取数据并保存
  71 + $this->create($response_data);
  72 + }
  73 + } else {
  74 + $response = $data->toArray();
  75 + }
  76 + // 缓存数据
  77 + cache()->put($key, $response, $this->time);
  78 + }
  79 + } else {
  80 + $response = $this->$function();
  81 + }
  82 + return $response;
  83 + }
  84 +
  85 + /**
  86 + * @param $response
  87 + * @return bool
  88 + * @throws Throwable
  89 + */
  90 + public function create($response)
  91 + {
  92 + DB::beginTransaction();
  93 + $keys = array_flip($this->trafficStatistics::KEY_VALUE);
  94 + foreach ($response as $k => $v) {
  95 + if (is_array($v)) {
  96 + $v = json_encode($v);
  97 + }
  98 + $field = $keys[$k];
  99 + $this->trafficStatistics->$field = $v;
  100 + }
  101 + $isRes = $this->trafficStatistics->save();
  102 + if (!$isRes) {
  103 + DB::rollBack();
  104 + return false;
  105 + }
  106 + DB::commit();
  107 + return true;
  108 + }
  109 +
  110 + /**
  111 + * 访问来源
  112 + * @return array
  113 + */
  114 + public function source()
  115 + {
  116 + $response = $this->getPvIp();
  117 + $response['lists'] = $this->customerVisit->getUrlCount();
  118 + return $response;
  119 + }
  120 +
  121 + public function getPvIp()
  122 + {
  123 + $response['pv_count'] = $this->customerVisit->getMonthPVCount();
  124 + $response['ip_count'] = $this->customerVisit->getMonthIPCount();
  125 + return $response;
  126 + }
  127 +
  128 + /**
  129 + * 地域分布
  130 + * @return array
  131 + */
  132 + public function distribution()
  133 + {
  134 + $response = $this->getPvIp();
  135 + $response['lists'] = $this->customerVisit->getCountryCount();
  136 + return $response;
  137 + }
  138 +
  139 + /**
  140 + * 流量趋势
  141 + * @return array
  142 + * @throws ContainerExceptionInterface
  143 + * @throws GuzzleException
  144 + * @throws NotFoundExceptionInterface
  145 + */
  146 + public function trend()
  147 + {
  148 + $response = $this->getPvIp();
  149 + $inquiry = $this->getCache('inquiry_information', $this->date);
  150 + $response['in_count'] = $inquiry['count'] ?? 0;
  151 + $response['lists'] = $inquiry['lists'] ?? [];
  152 + return $response;
  153 + }
  154 +
  155 + /**
  156 + * 访问终端
  157 + * @return array
  158 + */
  159 + public function terminal()
  160 + {
  161 + $response = $this->getPvIp();
  162 + $response['pc_count'] = $this->customerVisit->getTerminalPcCount();
  163 + $response['mobile_count'] = $this->customerVisit->getTerminalMobileCount();
  164 + $response['lists'] = $this->customerVisit->getCountryCount();
  165 + return $response;
  166 + }
  167 +
  168 + /**
  169 + * 受访页面
  170 + * @return array
  171 + */
  172 + public function page()
  173 + {
  174 + $response = $this->getPvIp();
  175 + $response['lists'] = $this->customerVisit->getPageCount();
  176 + return $response;
  177 + }
  178 +
  179 + /**
  180 + * 获取缓存
  181 + * @param string $key 缓存键名
  182 + * @param string|null $date 日期 格式:Y-m
  183 + * @param float|int $time 缓存时间,默认2小时
  184 + * @return mixed
  185 + * @throws ContainerExceptionInterface
  186 + * @throws GuzzleException
  187 + * @throws NotFoundExceptionInterface
  188 + */
  189 + public function getCache(string $key, string $date = null, $time = 60 * 60 * 2)
  190 + {
  191 + $domain = request()->getHttpHost(); //'www.wowstainless.com';
  192 + $sta_date = !is_null($date) ? $date . '-01' : date('Y-m-01');
  193 + $key = $key . '_' . stringUnderlineLowercase($domain) . '_' . str_replace('-', '_', $sta_date);
  194 + $value = cache()->get($key);
  195 + if (!$value) {
  196 + $value = getInquiryInformation($domain, $sta_date);
  197 + cache()->put($key, $value, $time);
  198 + }
  199 + return $value;
  200 + }
  201 +
  202 + /**
  203 + * @param int $type
  204 + * @param string|null $date 日期 格式:Y-m-d
  205 + * @return string
  206 + */
  207 + public function cacheName(int $type = TrafficStatistics::TYPE_SOURCE, string $date = null)
  208 + {
  209 + return 'statistics_' . $type . '_' . str_replace('-', '_', $date);
  210 + }
  211 +
  212 + /**
  213 + * 判断传入日期是否是当日
  214 + * @param $date
  215 + * @return bool
  216 + */
  217 + public function checkIsDay($date)
  218 + {
  219 + // 传入日期的时间戳
  220 + $timestamp = strtotime($date);
  221 + // 当日的日期格式,如:2021-10-13
  222 + $today = date('Y-m-d');
  223 + // 传入日期的日期格式,如:2021-10-12
  224 + $date = date('Y-m-d', $timestamp);
  225 + // 判断传入日期是否是当日
  226 + return $date == $today;
  227 + }
  228 +
  229 + /**
  230 + * 判断传入日期是否大于当日
  231 + * @param $date
  232 + * @return bool
  233 + */
  234 + public function checkIsGreaterDay($date)
  235 + {
  236 + // 传入日期的时间戳
  237 + $timestamp = strtotime($date);
  238 + // 当前日期的时间戳
  239 + $now = strtotime(date('Y-m-d'));
  240 + // 判断传入日期是否大于当前日期
  241 + return $timestamp > $now;
  242 + }
  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 +}
@@ -6,10 +6,37 @@ use App\Models\Base; @@ -6,10 +6,37 @@ use App\Models\Base;
6 6
7 /** 7 /**
8 * Class DomainInfo 8 * Class DomainInfo
  9 + *
9 * @package App\Models\Aside\Domain 10 * @package App\Models\Aside\Domain
10 * @Author YiYuan-LIn 11 * @Author YiYuan-LIn
11 - * @Date: 2019/5/16 12 + * @Date : 2019/5/16
12 * 域名信息模型 13 * 域名信息模型
  14 + * @property int $id
  15 + * @property string|null $domain 域名
  16 + * @property string $belong_to 域名归属 1 - 公司 2 - 客户
  17 + * @property string $status 域名状态 0 - 正常 1 - 关闭
  18 + * @property string|null $domain_start_time 域名开始时间
  19 + * @property string|null $domain_end_time 域名结束时间
  20 + * @property string|null $certificate_start_time 证书开始时间
  21 + * @property string|null $certificate_end_time 证书结束时间
  22 + * @property \Illuminate\Support\Carbon|null $created_at
  23 + * @property \Illuminate\Support\Carbon|null $updated_at
  24 + * @property int|null $deleted 软删除 0 - 正常 1 - 软删除
  25 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfo newModelQuery()
  26 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfo newQuery()
  27 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfo query()
  28 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfo whereBelongTo($value)
  29 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfo whereCertificateEndTime($value)
  30 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfo whereCertificateStartTime($value)
  31 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfo whereCreatedAt($value)
  32 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfo whereDeleted($value)
  33 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfo whereDomain($value)
  34 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfo whereDomainEndTime($value)
  35 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfo whereDomainStartTime($value)
  36 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfo whereId($value)
  37 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfo whereStatus($value)
  38 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfo whereUpdatedAt($value)
  39 + * @mixin \Eloquent
13 */ 40 */
14 class DomainInfo extends Base 41 class DomainInfo extends Base
15 { 42 {
@@ -4,6 +4,36 @@ namespace App\Models\Aside\Domain; @@ -4,6 +4,36 @@ namespace App\Models\Aside\Domain;
4 4
5 use Illuminate\Database\Eloquent\Model; 5 use Illuminate\Database\Eloquent\Model;
6 6
  7 +/**
  8 + * App\Models\Aside\Domain\DomainInfoLog
  9 + *
  10 + * @property int $id
  11 + * @property int|null $user_id 操作用户
  12 + * @property string|null $action 用户操作 - 增删改查
  13 + * @property string|null $original 初始数据
  14 + * @property string|null $revised 修改后的数据
  15 + * @property string|null $ip 用户IP
  16 + * @property string|null $url
  17 + * @property string|null $method 请求类型
  18 + * @property string|null $remarks 备注
  19 + * @property \Illuminate\Support\Carbon|null $created_at
  20 + * @property \Illuminate\Support\Carbon|null $updated_at
  21 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfoLog newModelQuery()
  22 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfoLog newQuery()
  23 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfoLog query()
  24 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfoLog whereAction($value)
  25 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfoLog whereCreatedAt($value)
  26 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfoLog whereId($value)
  27 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfoLog whereIp($value)
  28 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfoLog whereMethod($value)
  29 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfoLog whereOriginal($value)
  30 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfoLog whereRemarks($value)
  31 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfoLog whereRevised($value)
  32 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfoLog whereUpdatedAt($value)
  33 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfoLog whereUrl($value)
  34 + * @method static \Illuminate\Database\Eloquent\Builder|DomainInfoLog whereUserId($value)
  35 + * @mixin \Eloquent
  36 + */
7 class DomainInfoLog extends Model 37 class DomainInfoLog extends Model
8 { 38 {
9 protected $table = 'gl_domain_info_log'; 39 protected $table = 'gl_domain_info_log';
@@ -3,9 +3,50 @@ @@ -3,9 +3,50 @@
3 namespace App\Models\Bside\Statistics; 3 namespace App\Models\Bside\Statistics;
4 4
5 use App\Models\Base; 5 use App\Models\Base;
  6 +use Illuminate\Database\Eloquent\Builder;
  7 +use Illuminate\Database\Eloquent\Collection;
  8 +use Illuminate\Database\Eloquent\Model;
6 9
7 /** 10 /**
8 - * 流量统计 11 + * App\Models\Bside\Statistics\TrafficStatistics
  12 + *
  13 + * @property int $id
  14 + * @property int|null $type 类型:
  15 + * 1 - 访问来源
  16 + * 2 - 地域分布
  17 + * 3 - 受访页面
  18 + * 4 - 访问终端
  19 + * 5 - 流量趋势
  20 + * @property int|null $year 年
  21 + * @property int|null $month 月
  22 + * @property string|null $top 访问来源|国家|页面TOP15 - 百分比 - json格式
  23 + * @property int|null $innum 询盘量
  24 + * @property float|null $conversion 询盘转化率
  25 + * @property int|null $pvnum 浏览量
  26 + * @property int|null $ipnum 访客量
  27 + * @property int|null $pcnum PC端访问量
  28 + * @property int|null $mbnum 移动端访问量
  29 + * @property \Illuminate\Support\Carbon|null $created_at
  30 + * @property \Illuminate\Support\Carbon|null $updated_at
  31 + * @method static Builder|TrafficStatistics newModelQuery()
  32 + * @method static Builder|TrafficStatistics newQuery()
  33 + * @method static Builder|TrafficStatistics query()
  34 + * @method static Builder|TrafficStatistics whereConversion($value)
  35 + * @method static Builder|TrafficStatistics whereCreatedAt($value)
  36 + * @method static Builder|TrafficStatistics whereId($value)
  37 + * @method static Builder|TrafficStatistics whereInnum($value)
  38 + * @method static Builder|TrafficStatistics whereIpnum($value)
  39 + * @method static Builder|TrafficStatistics whereMbnum($value)
  40 + * @method static Builder|TrafficStatistics whereMonth($value)
  41 + * @method static Builder|TrafficStatistics wherePageview($value)
  42 + * @method static Builder|TrafficStatistics wherePcnum($value)
  43 + * @method static Builder|TrafficStatistics wherePvnum($value)
  44 + * @method static Builder|TrafficStatistics whereTop($value)
  45 + * @method static Builder|TrafficStatistics whereType($value)
  46 + * @method static Builder|TrafficStatistics whereUpdatedAt($value)
  47 + * @method static Builder|TrafficStatistics whereUserSessions($value)
  48 + * @method static Builder|TrafficStatistics whereYear($value)
  49 + * @mixin \Eloquent
9 */ 50 */
10 class TrafficStatistics extends Base 51 class TrafficStatistics extends Base
11 { 52 {
@@ -21,4 +62,121 @@ class TrafficStatistics extends Base @@ -21,4 +62,121 @@ class TrafficStatistics extends Base
21 const TYPE_TERMINAL = 4; 62 const TYPE_TERMINAL = 4;
22 /** @var int 流量趋势 */ 63 /** @var int 流量趋势 */
23 const TYPE_TREND = 5; 64 const TYPE_TREND = 5;
  65 + /** @var string[] 类型对应字段 */
  66 + const FIELDS = [
  67 + self::TYPE_SOURCE => ['pvnum', 'ipnum', 'top'],
  68 + self::TYPE_DISTRIBUTION => ['pvnum', 'ipnum', 'top'],
  69 + self::TYPE_PAGE => ['pvnum', 'ipnum', 'top'],
  70 + self::TYPE_TERMINAL => ['pvnum', 'ipnum', 'top', 'pcnum', 'mbnum'],
  71 + self::TYPE_TREND => ['pvnum', 'ipnum', 'top', 'innum'],
  72 + ];
  73 + /** @var string[] 数据库字段对应返回字段 */
  74 + const KEY_VALUE = [
  75 + 'pvnum' => 'pv_count',
  76 + 'ipnum' => 'ip_count',
  77 + 'pcnum' => 'pc_count',
  78 + 'mbnum' => 'mobile_count',
  79 + 'innum' => 'in_count',
  80 + 'top' => 'lists'
  81 + ];
  82 +
  83 + /**
  84 + * 根据类型获取字段
  85 + * @param int $type
  86 + * @return string[]
  87 + */
  88 + public function selectFields(int $type = self::TYPE_SOURCE)
  89 + {
  90 + return self::FIELDS[$type] ?? [];
  91 + }
  92 +
  93 + /**
  94 + * 根据类型获取月份列表数据
  95 + * @param int $type
  96 + * @param $date
  97 + * @return TrafficStatistics[]|Builder[]|Collection
  98 + */
  99 + public function getMonthsLists(int $type = self::TYPE_SOURCE, $date = null)
  100 + {
  101 + $query = $this->query()->where('type', $type);
  102 + if ($date != null) {
  103 + if (is_array($date)) {
  104 + $years = $months = [];
  105 + foreach ($date as $value) {
  106 + $dd = explode('-', $value);
  107 + $year = $dd[0];
  108 + $month = $dd[1];
  109 + if (!in_array($year, $years)) {
  110 + $years[] = $year;
  111 + }
  112 + if (!in_array($month, $months)) {
  113 + $months[] = $month;
  114 + }
  115 + }
  116 + $query->whereIn('year', $years)
  117 + ->whereIn('month', $months);
  118 + } else {
  119 + $dd = explode('-', $date);
  120 + $year = $dd[0];
  121 + $month = $dd[1];
  122 + $query->where('year', $year)
  123 + ->where('month', $month);
  124 + }
  125 + } else {
  126 + $query->where('year', date('Y'))
  127 + ->where('month', date('m'));
  128 + }
  129 + return $query->get();
  130 + }
  131 +
  132 + /**
  133 + * 根据类型获取单条月份数据
  134 + * @param int $type
  135 + * @param $year
  136 + * @param $month
  137 + * @return TrafficStatistics|Builder|Model|object|null
  138 + */
  139 + public function getMonth(int $type = self::TYPE_SOURCE, $year = null, $month = null)
  140 + {
  141 + $year = $year ?? date('Y');
  142 + $month = $month ?? date('m');
  143 + return $this->query()->where('type', $type)
  144 + ->where('year', $year)
  145 + ->where('month', $month)
  146 + ->first();
  147 + }
  148 +
  149 + /**
  150 + * 根据类型获取数据
  151 + * @param int $type
  152 + * @param null $year
  153 + * @param null $month
  154 + * @return TrafficStatistics|Builder|Model|object|null
  155 + */
  156 + public function getData(int $type = self::TYPE_SOURCE, $year = null, $month = null)
  157 + {
  158 + $key_value = self::KEY_VALUE;
  159 + $field_arr = [];
  160 + foreach ($this->selectFields($type) as $field) {
  161 + $field_arr[] = "{$field} as {$key_value[$field]}";
  162 + }
  163 + $query = $this->query();
  164 + if ($field_arr) {
  165 + $query->selectRaw(implode(',', $field_arr));
  166 + }
  167 + return $query->where('type', $type)
  168 + ->where('year', $year)
  169 + ->where('month', $month)
  170 + ->first();
  171 + }
  172 +
  173 + /**
  174 + * 格式化数据
  175 + * @param $value
  176 + * @return mixed
  177 + */
  178 + public function getListsAttribute($value)
  179 + {
  180 + return json_decode($value);
  181 + }
24 } 182 }
@@ -3,11 +3,62 @@ @@ -3,11 +3,62 @@
3 namespace App\Models\Bside\Statistics; 3 namespace App\Models\Bside\Statistics;
4 4
5 use App\Models\Base; 5 use App\Models\Base;
  6 +use Illuminate\Database\Eloquent\Builder;
  7 +use Illuminate\Database\Eloquent\Collection;
  8 +use Illuminate\Database\Eloquent\Model;
6 9
7 /** 10 /**
8 - * 流量统计 11 + * App\Models\Bside\Statistics\TrafficTrends
  12 + *
  13 + * @property int $id
  14 + * @property string|null $day 统计当天日期
  15 + * @property int|null $pvnum 当天的访问次数PV
  16 + * @property int|null $ipnum 当天的独立访问IP数量
  17 + * @property \Illuminate\Support\Carbon|null $created_at
  18 + * @property \Illuminate\Support\Carbon|null $updated_at
  19 + * @method static Builder|TrafficTrends newModelQuery()
  20 + * @method static Builder|TrafficTrends newQuery()
  21 + * @method static Builder|TrafficTrends query()
  22 + * @method static Builder|TrafficTrends whereCreatedAt($value)
  23 + * @method static Builder|TrafficTrends whereDay($value)
  24 + * @method static Builder|TrafficTrends whereId($value)
  25 + * @method static Builder|TrafficTrends whereIpnum($value)
  26 + * @method static Builder|TrafficTrends wherePvnum($value)
  27 + * @method static Builder|TrafficTrends whereUpdatedAt($value)
  28 + * @mixin \Eloquent
9 */ 29 */
10 class TrafficTrends extends Base 30 class TrafficTrends extends Base
11 { 31 {
12 protected $table = 'gl_traffic_trends'; 32 protected $table = 'gl_traffic_trends';
  33 +
  34 + /**
  35 + * * 根据时间获取数据
  36 + * @param array|string $date
  37 + * @return TrafficTrends[]|Builder[]|Collection
  38 + */
  39 + public function getDaysLists($date = null)
  40 + {
  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 + }
  48 + } else {
  49 + $query->whereYear('day', '=', date("Y"))
  50 + ->whereMonth('day', '=', date("m"))
  51 + ->whereDay('day', '=', date("d"));
  52 + }
  53 + return $query->get();
  54 + }
  55 +
  56 + /**
  57 + * @param string|null $date
  58 + * @return TrafficTrends|Builder|Model|object|null
  59 + */
  60 + public function getDay(string $date = null)
  61 + {
  62 + return $this->query()->where('day', $date ?? date('Y-m-d'))->first();
  63 + }
13 } 64 }
@@ -3,9 +3,43 @@ @@ -3,9 +3,43 @@
3 namespace App\Models\CustomerVisit; 3 namespace App\Models\CustomerVisit;
4 4
5 use App\Models\Base; 5 use App\Models\Base;
  6 +use Illuminate\Database\Eloquent\Builder;
6 use Illuminate\Database\Eloquent\Collection; 7 use Illuminate\Database\Eloquent\Collection;
7 use Illuminate\Database\Eloquent\Factories\HasFactory; 8 use Illuminate\Database\Eloquent\Factories\HasFactory;
8 9
  10 +/**
  11 + * App\Models\CustomerVisit\CustomerVisit
  12 + *
  13 + * @property int $id ID
  14 + * @property string $url 客户访问URL
  15 + * @property string $referrer_url 访问来源
  16 + * @property int $device_port 设备端口 1:PC端,2:移动端
  17 + * @property string $country 国家
  18 + * @property string $city 地区
  19 + * @property string $ip 访问ip
  20 + * @property int $depth 访问深度
  21 + * @property string $domain 域名
  22 + * @property \Illuminate\Support\Carbon $created_at 访问时间
  23 + * @property \Illuminate\Support\Carbon $updated_at 更新时间
  24 + * @property string $updated_date 统计日期
  25 + * @method static \Database\Factories\CustomerVisit\CustomerVisitFactory factory(...$parameters)
  26 + * @method static Builder|CustomerVisit newModelQuery()
  27 + * @method static Builder|CustomerVisit newQuery()
  28 + * @method static Builder|CustomerVisit query()
  29 + * @method static Builder|CustomerVisit whereCity($value)
  30 + * @method static Builder|CustomerVisit whereCountry($value)
  31 + * @method static Builder|CustomerVisit whereCreatedAt($value)
  32 + * @method static Builder|CustomerVisit whereDepth($value)
  33 + * @method static Builder|CustomerVisit whereDevicePort($value)
  34 + * @method static Builder|CustomerVisit whereDomain($value)
  35 + * @method static Builder|CustomerVisit whereId($value)
  36 + * @method static Builder|CustomerVisit whereIp($value)
  37 + * @method static Builder|CustomerVisit whereReferrerUrl($value)
  38 + * @method static Builder|CustomerVisit whereUpdatedAt($value)
  39 + * @method static Builder|CustomerVisit whereUpdatedDate($value)
  40 + * @method static Builder|CustomerVisit whereUrl($value)
  41 + * @mixin \Eloquent
  42 + */
9 class CustomerVisit extends Base 43 class CustomerVisit extends Base
10 { 44 {
11 use HasFactory; 45 use HasFactory;
@@ -19,51 +53,102 @@ class CustomerVisit extends Base @@ -19,51 +53,102 @@ class CustomerVisit extends Base
19 /** @var int 移动端 */ 53 /** @var int 移动端 */
20 const TERMINAL_MOBILE = 2; 54 const TERMINAL_MOBILE = 2;
21 55
22 - public function queryField($field, $action = 'count', $whereDay = false) 56 + /**
  57 + * @param string $field 统计字段
  58 + * @param string $action 统计方法 count|sum
  59 + * @param bool $isDay 是否按天查询
  60 + * @param string|null $date 日期 格式:Y-m-d
  61 + * @return int
  62 + */
  63 + public function queryField(string $field, string $action = 'count', bool $isDay = false, string $date = null)
23 { 64 {
24 - $query = $this->query()  
25 - ->selectRaw("{$action}({$field}) as count")  
26 - ->whereYear('updated_date', '=', date("Y"))  
27 - ->whereMonth('updated_date', '=', date("m"));  
28 - if ($whereDay) {  
29 - $query->whereDay('updated_date', '=', date("d")); 65 + $query = $this->query()->selectRaw("{$action}({$field}) as count");
  66 + if ($date) {
  67 + $dd = explode('-', $date);
  68 + $year = $dd[0] ?? null;
  69 + $month = $dd[1] ?? null;
  70 + $day = $dd[2] ?? null;
  71 + if (!is_null($year)) {
  72 + $query->whereYear('updated_date', '=', $year);
  73 + }
  74 + if (!is_null($month)) {
  75 + $query->whereMonth('updated_date', '=', $month);
  76 + }
  77 + if (!is_null($day)) {
  78 + $query->whereDay('updated_date', '=', $day);
  79 + }
  80 + } else {
  81 + $query->whereYear('updated_date', '=', date("Y"))
  82 + ->whereMonth('updated_date', '=', date("m"));
  83 + if ($isDay) {
  84 + $query->whereDay('updated_date', '=', date("d"));
  85 + }
30 } 86 }
  87 +
31 return (int)$query->value('count') ?? 0; 88 return (int)$query->value('count') ?? 0;
32 } 89 }
33 90
34 91
35 - public function queryTerminal($field, $action = self::TERMINAL_PC) 92 + /**
  93 + * @param string $field 统计字段
  94 + * @param int $action 终端类型
  95 + * @param string|null $date 日期 格式:Y-m
  96 + * @return int
  97 + */
  98 + public function queryTerminal(string $field, string $date = null, int $action = self::TERMINAL_PC)
36 { 99 {
37 - return (int)$this->query()  
38 - ->selectRaw("count(*) as count")  
39 - ->where($field, '=', $action)  
40 - ->whereYear('updated_date', '=', date("Y"))  
41 - ->whereMonth('updated_date', '=', date("m"))  
42 - ->value('count') ?? 0; 100 + $query = $this->query()
  101 + ->selectRaw("count(*) as count")
  102 + ->where($field, '=', $action);
  103 + $this->extracted($date, $query);
  104 + return (int)$query->value('count') ?? 0;
43 } 105 }
44 106
45 - public function getRankingData($field, $limit = self::LIMIT) 107 + /**
  108 + * @param string $field 查询字段
  109 + * @param string|null $date 日期 格式:Y-m-d
  110 + * @param int $limit 查询条数
  111 + * @return Builder[]|Collection|\Illuminate\Support\Collection
  112 + */
  113 + public function getRankingData(string $field, string $date = null, int $limit = self::LIMIT)
46 { 114 {
47 - return $this->query()  
48 - ->selectRaw('count(*) as count, ' . $field . ' as request')  
49 - ->whereYear('updated_date', '=', date("Y"))  
50 - ->whereMonth('updated_date', '=', date("m"))  
51 - ->groupBy($field)  
52 - ->orderBy('count', 'desc')  
53 - ->limit($limit)  
54 - ->get(); 115 + $query = $this->query()
  116 + ->selectRaw('count(*) as count, ' . $field . ' as request');
  117 + $this->extracted($date, $query);
  118 + return $query->groupBy($field)
  119 + ->orderBy('count', 'desc')
  120 + ->limit($limit)
  121 + ->get();
55 } 122 }
56 123
57 - public function getSum($field) 124 + /**
  125 + * 获取相加总数
  126 + * @param string $field
  127 + * @param string|null $date 日期 格式:Y-m-d
  128 + * @return int
  129 + */
  130 + public function getSum(string $field, string $date = null)
58 { 131 {
59 - return $this->queryField($field, 'sum'); 132 + return $this->queryField($field, 'sum', false, $date);
60 } 133 }
61 134
62 - public function getCount($field) 135 + /**
  136 + * 获取总数
  137 + * @param string $field
  138 + * @param string|null $date 日期 格式:Y-m-d
  139 + * @return int
  140 + */
  141 + public function getCount(string $field, string $date = null)
63 { 142 {
64 - return $this->queryField($field); 143 + return $this->queryField($field, 'count', false, $date);
65 } 144 }
66 145
  146 + /**
  147 + * 获取列表和总数
  148 + * @param Collection $data
  149 + * @param int $limit
  150 + * @return array
  151 + */
67 public function returnLists(Collection $data, int $limit = self::LIMIT) 152 public function returnLists(Collection $data, int $limit = self::LIMIT)
68 { 153 {
69 $lists = $data->toArray(); 154 $lists = $data->toArray();
@@ -74,85 +159,117 @@ class CustomerVisit extends Base @@ -74,85 +159,117 @@ class CustomerVisit extends Base
74 159
75 /** 160 /**
76 * 查询当月流量的浏览量 161 * 查询当月流量的浏览量
  162 + * @param string|null $data 日期 格式:Y-m-d
77 * @return int 163 * @return int
78 */ 164 */
79 - public function getMonthPVCount() 165 + public function getMonthPVCount(string $data = null)
80 { 166 {
81 - return $this->getSum('depth'); 167 + return $this->getSum('depth', $data);
82 } 168 }
83 169
84 /** 170 /**
85 * 查询当月流量的访客量 171 * 查询当月流量的访客量
  172 + * @param string|null $data 日期 格式:Y-m-d
86 * @return int 173 * @return int
87 */ 174 */
88 - public function getMonthIPCount() 175 + public function getMonthIPCount(string $data = null)
89 { 176 {
90 - return $this->getCount('ip'); 177 + return $this->getCount('ip', $data);
91 } 178 }
92 179
93 /** 180 /**
94 * 查询当月TOP15访问来源 181 * 查询当月TOP15访问来源
95 - * @param int $limit 182 + * @param string|null $date 日期 格式:Y-m-d
  183 + * @param int $limit 查询条数
96 * @return array 184 * @return array
97 */ 185 */
98 - public function getUrlCount(int $limit = self::LIMIT) 186 + public function getUrlCount(string $date = null, int $limit = self::LIMIT)
99 { 187 {
100 - return $this->getRankingData('referrer_url', $limit); 188 + return $this->getRankingData('referrer_url', $date, $limit);
101 } 189 }
102 190
103 /** 191 /**
104 * 查询当月TOP15访问国家来源 192 * 查询当月TOP15访问国家来源
  193 + * @param string|null $date 日期 格式:Y-m
105 * @param int $limit 194 * @param int $limit
106 * @return array 195 * @return array
107 */ 196 */
108 - public function getCountryCount(int $limit = self::LIMIT) 197 + public function getCountryCount(string $date = null, int $limit = self::LIMIT)
109 { 198 {
110 - return $this->getRankingData('country', $limit); 199 + return $this->getRankingData('country', $date, $limit);
111 } 200 }
112 201
113 /** 202 /**
114 * 查询当月TOP15受访页面 203 * 查询当月TOP15受访页面
  204 + * @param string|null $date 日期 格式:Y-m
115 * @param int $limit 205 * @param int $limit
116 * @return array 206 * @return array
117 */ 207 */
118 - public function getPageCount(int $limit = self::LIMIT) 208 + public function getPageCount(string $date = null, int $limit = self::LIMIT)
119 { 209 {
120 - return $this->getRankingData('url', $limit); 210 + return $this->getRankingData('url', $date, $limit);
121 } 211 }
122 212
123 /** 213 /**
124 * 查询当月TOP15访问终端 214 * 查询当月TOP15访问终端
  215 + * @param string|null $date 日期 格式:Y-m-d
125 * @return int 216 * @return int
126 */ 217 */
127 - public function getTerminalPcCount() 218 + public function getTerminalPcCount(string $date = null)
128 { 219 {
129 - return $this->queryTerminal('device_port'); 220 + return $this->queryTerminal('device_port', $date);
130 } 221 }
131 222
132 /** 223 /**
133 * 查询当月TOP15访问终端 224 * 查询当月TOP15访问终端
  225 + * @param string|null $date 日期 格式:Y-m-d
134 * @return int 226 * @return int
135 */ 227 */
136 - public function getTerminalMobileCount() 228 + public function getTerminalMobileCount(string $date = null)
137 { 229 {
138 - return $this->queryTerminal('device_port', self::TERMINAL_MOBILE); 230 + return $this->queryTerminal('device_port', $date, self::TERMINAL_MOBILE);
139 } 231 }
140 232
141 /** 233 /**
142 * 查询当日流量的浏览量 234 * 查询当日流量的浏览量
  235 + * @param string|null $date 日期 格式:Y-m-d
143 * @return int 236 * @return int
144 */ 237 */
145 - public function getDayPVCount() 238 + public function getDayPVCount(string $date = null)
146 { 239 {
147 - return $this->queryField('depth', 'sum', true); 240 + return $this->queryField('depth', 'sum', true, $date);
148 } 241 }
149 242
150 /** 243 /**
151 * 查询当日流量的访客量 244 * 查询当日流量的访客量
  245 + * @param string|null $date 日期 格式:Y-m-d
152 * @return int 246 * @return int
153 */ 247 */
154 - public function getDayIPCount() 248 + public function getDayIPCount(string $date = null)
155 { 249 {
156 - return $this->queryField('ip', 'count', true); 250 + return $this->queryField('ip', 'count', true, $date);
  251 + }
  252 +
  253 + /**
  254 + * @param $date
  255 + * @param $query
  256 + * @return void
  257 + */
  258 + public function extracted($date, $query)
  259 + {
  260 + if (!is_null($date)) {
  261 + $dd = explode('-', $date);
  262 + $year = $dd[0] ?? null;
  263 + $month = $dd[1] ?? null;
  264 + if (!is_null($year)) {
  265 + $query->whereYear('updated_date', '=', $year);
  266 + }
  267 + if (!is_null($month)) {
  268 + $query->whereMonth('updated_date', '=', $month);
  269 + }
  270 + } else {
  271 + $query->whereYear('updated_date', '=', date("Y"))
  272 + ->whereMonth('updated_date', '=', date("m"));
  273 + }
157 } 274 }
158 } 275 }
@@ -4,6 +4,36 @@ namespace App\Models\Devops; @@ -4,6 +4,36 @@ namespace App\Models\Devops;
4 4
5 use App\Models\Base; 5 use App\Models\Base;
6 6
  7 +/**
  8 + * App\Models\Devops\ServerInformation
  9 + *
  10 + * @property int $id
  11 + * @property string $type 服务器类型
  12 + * @property string|null $ip 服务器ip
  13 + * @property string|null $title 服务器标题
  14 + * @property string $belong_to 服务器归属 1 - 公司 2 - 客户
  15 + * @property string|null $sshpass SSH 密码
  16 + * @property int|null $ports SSH 端口
  17 + * @property string|null $other 其他信息 json格式
  18 + * @property \Illuminate\Support\Carbon|null $created_at
  19 + * @property \Illuminate\Support\Carbon|null $updated_at
  20 + * @property int|null $deleted 软删除 0 - 正常 1 - 软删除
  21 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformation newModelQuery()
  22 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformation newQuery()
  23 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformation query()
  24 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformation whereBelongTo($value)
  25 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformation whereCreatedAt($value)
  26 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformation whereDeleted($value)
  27 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformation whereId($value)
  28 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformation whereIp($value)
  29 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformation whereOther($value)
  30 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformation wherePorts($value)
  31 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformation whereSshpass($value)
  32 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformation whereTitle($value)
  33 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformation whereType($value)
  34 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformation whereUpdatedAt($value)
  35 + * @mixin \Eloquent
  36 + */
7 class ServerInformation extends Base 37 class ServerInformation extends Base
8 { 38 {
9 39
@@ -4,6 +4,36 @@ namespace App\Models\Devops; @@ -4,6 +4,36 @@ namespace App\Models\Devops;
4 4
5 use App\Models\Base; 5 use App\Models\Base;
6 6
  7 +/**
  8 + * App\Models\Devops\ServerInformationLog
  9 + *
  10 + * @property int $id
  11 + * @property int|null $user_id 操作用户
  12 + * @property string|null $action 用户操作 - 增删改查
  13 + * @property string|null $original 初始数据
  14 + * @property string|null $revised 修改后的数据
  15 + * @property string|null $ip 用户IP
  16 + * @property string|null $url
  17 + * @property string|null $method 请求类型
  18 + * @property string|null $remarks 备注
  19 + * @property \Illuminate\Support\Carbon|null $created_at
  20 + * @property \Illuminate\Support\Carbon|null $updated_at
  21 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformationLog newModelQuery()
  22 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformationLog newQuery()
  23 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformationLog query()
  24 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformationLog whereAction($value)
  25 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformationLog whereCreatedAt($value)
  26 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformationLog whereId($value)
  27 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformationLog whereIp($value)
  28 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformationLog whereMethod($value)
  29 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformationLog whereOriginal($value)
  30 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformationLog whereRemarks($value)
  31 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformationLog whereRevised($value)
  32 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformationLog whereUpdatedAt($value)
  33 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformationLog whereUrl($value)
  34 + * @method static \Illuminate\Database\Eloquent\Builder|ServerInformationLog whereUserId($value)
  35 + * @mixin \Eloquent
  36 + */
7 class ServerInformationLog extends Base 37 class ServerInformationLog extends Base
8 { 38 {
9 protected $table = 'gl_server_information_log'; 39 protected $table = 'gl_server_information_log';