ServerController.php 1.3 KB
<?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,
        ]);
    }
}