IpIpService.php 2.5 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

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

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

李光春's avatar
李光春 已提交
35 36 37 38 39
    /**
     * IpIpService constructor.
     * @param App $app
     * @throws Exception
     */
李光春's avatar
李光春 已提交
40
    public function __construct(App $app)
李光春's avatar
李光春 已提交
41 42
    {
        $this->reader = new IpIpReader(__DIR__ . '/bin/ipipfree.ipdb');
李光春's avatar
李光春 已提交
43
        parent::__construct($app);
李光春's avatar
李光春 已提交
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
    }

    /**
     * @param $ip
     * @param $language
     * @return array|NULL
     */
    public function getFind(string $ip = '', string $language = 'CN')
    {
        if (empty($ip)) $ip = get_ip();
        return $this->reader->find($ip, $language);
    }

    /**
     * @param $ip
     * @param $language
     * @return array|false|null
     */
    public function getFindMap(string $ip = '', string $language = 'CN')
    {
        if (empty($ip)) $ip = get_ip();
        return $this->reader->findMap($ip, $language);
    }

    /**
     * @param $ip
     * @param $language
     * @return IpIpDistrictInfo|null
     */
    public function getFindInfo(string $ip = '', string $language = 'CN')
    {
        if (empty($ip)) $ip = get_ip();
        $map = $this->getFindMap($ip, $language);
        if (NULL === $map) return NUll;
        return new IpIpDistrictInfo($map);
    }
}