IpIpService.php 3.0 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
{
李光春's avatar
李光春 已提交
34 35 36 37
    /**
     * @var IpIpReader|null
     */
    public $reader;
李光春's avatar
李光春 已提交
38

39 40 41 42
    /**
     * IP数据库文件存放位置
     * @var mixed
     */
李光春's avatar
李光春 已提交
43
    private $ipPath;
44

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

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

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

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