StatisticsController.php
4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace App\Http\Controllers\Bside;
use App\Models\CustomerVisit\CustomerVisit;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
* 流量统计
* Class StatisticsController
* @package App\Http\Controllers\Bside
*/
class StatisticsController extends BaseController
{
private $customerVisit;
public function __construct()
{
$this->customerVisit = new CustomerVisit();
}
/**
* 访问来源
* @return JsonResponse
*/
public function source()
{
$pv_count = $this->customerVisit->getMonthPVCount();
$ip_count = $this->customerVisit->getMonthIPCount();
$lists = $this->customerVisit->getUrlCount();
return $this->success(compact('pv_count', 'ip_count', 'lists'));
}
/**
* 地域分布
* @return JsonResponse
*/
public function distribution()
{
$pv_count = $this->customerVisit->getMonthPVCount();
$ip_count = $this->customerVisit->getMonthIPCount();
$lists = $this->customerVisit->getCountryCount();
return $this->success(compact('pv_count', 'ip_count', 'lists'));
}
/**
* 受访页面
* @return JsonResponse
*/
public function page()
{
$pv_count = $this->customerVisit->getMonthPVCount();
$ip_count = $this->customerVisit->getMonthIPCount();
$lists = $this->customerVisit->getPageCount();
return $this->success(compact('pv_count', 'ip_count', 'lists'));
}
/**
* 访问终端
* @return JsonResponse
*/
public function terminal()
{
$pv_count = $this->customerVisit->getMonthPVCount();
$ip_count = $this->customerVisit->getMonthIPCount();
$pc_count = $this->customerVisit->getTerminalPcCount();
$mobile_count = $this->customerVisit->getTerminalMobileCount();
$lists = $this->customerVisit->getCountryCount();
return $this->success(compact('pv_count', 'ip_count', 'pc_count', 'mobile_count', 'lists'));
}
/**
* 流量趋势
* @return JsonResponse
* @throws ContainerExceptionInterface
* @throws GuzzleException
* @throws NotFoundExceptionInterface
*/
public function trend()
{
// dd($this->customerVisit->getDayIPCount());
$pv_count = $this->customerVisit->getMonthPVCount();
$ip_count = $this->customerVisit->getMonthIPCount();
$inquiry = $this->getCache('inquiry_information');
$inquiry_count = $inquiry['count'] ?? 0;
$lists = $inquiry['lists'] ?? [];
return $this->success(compact('pv_count', 'ip_count', 'inquiry_count', 'lists'));
}
/**
* 获取缓存
* @param string $key 缓存键名
* @param float|int $time 缓存时间,默认2小时
* @return mixed
* @throws ContainerExceptionInterface
* @throws GuzzleException
* @throws NotFoundExceptionInterface
*/
public function getCache(string $key, $time = 60 * 60 * 2)
{
$domain = request()->getHttpHost(); //'www.wowstainless.com';
$sta_date = date('Y-m-01');
$key = $key . '_' . $this->stringUnderlineLowercase($domain) . '_' . str_replace('-', '_', $sta_date);
$value = cache()->get($key);
if (!$value) {
$value = $this->getInquiryInformation($domain, $sta_date);
cache()->put($key, $value, $time);
}
return $value;
}
/**
* 正则 - 名字转为小写并将空格转为下划线
* @param $name
* @return string
*/
public function stringUnderlineLowercase($name)
{
return trim(strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $name)));
}
/**
* 获取第三方询盘信息
* @return array|string
* @throws GuzzleException
*/
public function getInquiryInformation($domain, $sta_date)
{
$token = md5($domain . date("Y-m-d"));
$source = '1,3';
$url = "https://form.globalso.com/api/external-interface/country_con/15243d63ed5a5738?domain={$domain}&token={$token}&source={$source}&sta_date={$sta_date}";
$client = new Client(['verify' => false]);
$http = $client->get($url);
$data = [];
if ($http->getStatusCode() != 200) {
return $data;
}
$content = $http->getBody()->getContents();
$json = json_decode($content, true);
if ($json['status'] != 200) {
return $content;
}
$data['count'] = $json['data']['count'];
$data['lists'] = $json['data']['data'];
return $data;
}
}