IpIpService.php 2.9 KB
Newer Older
李光春's avatar
李光春 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
李光春's avatar
李光春 已提交
14 15 16
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
李光春's avatar
李光春 已提交
17 18 19
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------

李光春's avatar
李光春 已提交
20
namespace DtApp\ThinkLibrary\service\Ip;
李光春's avatar
李光春 已提交
21

22
use DtApp\ThinkLibrary\exception\DtaException;
李光春's avatar
李光春 已提交
23
use DtApp\ThinkLibrary\Service;
李光春's avatar
李光春 已提交
24
use Exception;
李光春's avatar
李光春 已提交
25
use think\App;
李光春's avatar
李光春 已提交
26 27 28 29 30 31

/**
 * IP  - IPIP
 * Class IpIpService
 * @package DtApp\ThinkLibrary\service\ip
 */
李光春's avatar
李光春 已提交
32
class IpIpService extends Service
李光春's avatar
李光春 已提交
33 34 35
{
    public $reader = null;

36 37 38 39 40 41
    /**
     * IP数据库文件存放位置
     * @var mixed
     */
    private $ipPath = '';

李光春's avatar
李光春 已提交
42 43 44 45 46
    /**
     * IpIpService constructor.
     * @param App $app
     * @throws Exception
     */
李光春's avatar
李光春 已提交
47
    public function __construct(App $app)
李光春's avatar
李光春 已提交
48
    {
李光春's avatar
李光春 已提交
49
        $this->ipPath = config('dtapp.ip_path', '');
李光春's avatar
李光春 已提交
50 51 52
        if (empty($this->ipPath)) {
            throw new DtaException('请检查配置文件是否配置了IP数据库文件存放位置');
        }
53
        $this->reader = new IpIpReader($this->ipPath . 'ipipfree.ipdb');
李光春's avatar
李光春 已提交
54
        parent::__construct($app);
李光春's avatar
李光春 已提交
55 56 57 58 59 60 61 62 63
    }

    /**
     * @param $ip
     * @param $language
     * @return array|NULL
     */
    public function getFind(string $ip = '', string $language = 'CN')
    {
李光春's avatar
李光春 已提交
64 65 66
        if (empty($ip)) {
            $ip = get_ip();
        }
李光春's avatar
李光春 已提交
67 68 69 70 71 72 73 74 75 76
        return $this->reader->find($ip, $language);
    }

    /**
     * @param $ip
     * @param $language
     * @return array|false|null
     */
    public function getFindMap(string $ip = '', string $language = 'CN')
    {
李光春's avatar
李光春 已提交
77 78 79
        if (empty($ip)) {
            $ip = get_ip();
        }
李光春's avatar
李光春 已提交
80 81 82 83 84 85 86 87 88 89
        return $this->reader->findMap($ip, $language);
    }

    /**
     * @param $ip
     * @param $language
     * @return IpIpDistrictInfo|null
     */
    public function getFindInfo(string $ip = '', string $language = 'CN')
    {
李光春's avatar
李光春 已提交
90 91 92
        if (empty($ip)) {
            $ip = get_ip();
        }
李光春's avatar
李光春 已提交
93
        $map = $this->getFindMap($ip, $language);
李光春's avatar
李光春 已提交
94 95 96
        if (NULL === $map) {
            return NUll;
        }
李光春's avatar
李光春 已提交
97 98 99
        return new IpIpDistrictInfo($map);
    }
}