ServerController.php
1.3 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
<?php
/**
* 服务类
* Author: xiaojie
* DateTime: 2018/12/03 18:01
*/
namespace app\portal\controller;
use app\portal\model\StatisticsModel;
use cmf\controller\HomeBaseController;
use think\Config;
class ServerController extends HomeBaseController
{
/**
* 腾讯根据ip获取当前位置信息
* @param $ip 当前ip地址
* @return bool
*/
public function getAddress($ip)
{
$key = Config::get('TENGXUN_KEY');
$url = 'https://apis.map.qq.com/ws/location/v1/ip?ip=' . $ip . '&key=' .$key;
$data = json_decode(file_get_contents($url),true);
if($data['status'] !== 0){
return false;
}
return $data['result']['ad_info']['province'];
}
/**
* 访问数据统计
*/
public function statistics()
{
$statisticsModel = new StatisticsModel();
$ip = get_client_ip();
$address = $this->getAddress($ip);
if($address){
$status = 1;
}else{
$address = '用户拒绝授权地理位置';
$status = 0;
}
$statisticsModel->insert([
'ip' => $ip,
'address' => $address,
'create_time' => time(),
'create_date' => date('Y-m-d',time()),
'status' => $status,
]);
}
}