common.php 4.9 KB
Newer Older
李光春's avatar
test  
李光春 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?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
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------

李光春's avatar
李光春 已提交
17 18
use DtApp\ThinkLibrary\cache\Mysql;
use DtApp\ThinkLibrary\exception\CacheException;
李光春's avatar
李光春 已提交
19
use DtApp\ThinkLibrary\exception\IpException;
李光春's avatar
李光春 已提交
20
use DtApp\ThinkLibrary\service\Ip\QqWryService;
21
use DtApp\ThinkLibrary\service\SystemService;
李光春's avatar
李光春 已提交
22 23 24
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
李光春's avatar
test  
李光春 已提交
25 26

if (!function_exists('get_ip_info')) {
李光春's avatar
李光春 已提交
27

李光春's avatar
test  
李光春 已提交
28 29 30
    /**
     * 获取请求IP信息
     * @param string $ip
李光春's avatar
李光春 已提交
31
     * @return mixed|null
李光春's avatar
test  
李光春 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44
     * @throws IpException
     */
    function get_ip_info($ip = '')
    {
        if (empty($ip)) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                //为了兼容百度的CDN,所以转成数组
                $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                $ip = $arr[0];
            } else {
                $ip = $_SERVER['REMOTE_ADDR'];
            }
        }
李光春's avatar
李光春 已提交
45
        return QqWryService::instance()->getLocation($ip);
李光春's avatar
test  
李光春 已提交
46 47 48 49
    }
}

if (!function_exists('get_ip')) {
李光春's avatar
李光春 已提交
50

李光春's avatar
test  
李光春 已提交
51 52 53 54 55 56
    /**
     * 获取请求IP
     * @return string
     */
    function get_ip()
    {
李光春's avatar
李光春 已提交
57
        $ip = '0.0.0.0';
李光春's avatar
test  
李光春 已提交
58 59
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            //为了兼容百度的CDN,所以转成数组
李光春's avatar
李光春 已提交
60
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
李光春's avatar
李光春 已提交
61 62 63 64
            $pos = array_search('unknown', $arr);
            if (false !== $pos) unset($arr[$pos]);
            $ip = trim($arr[0]);
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
李光春's avatar
李光春 已提交
65
            $ip = $_SERVER['HTTP_CLIENT_IP'];
李光春's avatar
李光春 已提交
66
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
李光春's avatar
李光春 已提交
67
            $ip = $_SERVER['REMOTE_ADDR'];
李光春's avatar
test  
李光春 已提交
68 69 70 71
        }
        return $ip;
    }
}
李光春's avatar
李光春 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

if (!function_exists('getEnid')) {

    /**
     * 10进制转化36进制
     * https://blog.csdn.net/zhangchb/article/details/78855083
     * @param int $format
     * @return mixed|string
     */
    function getEnid($format = 8)
    {
        $dic = array(
            0 => '0', 1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5', 6 => '6', 7 => '7', 8 => '8', 9 => '9',
            10 => 'A', 11 => 'B', 12 => 'C', 13 => 'D', 14 => 'E', 15 => 'F', 16 => 'G', 17 => 'H', 18 => 'I',
            19 => 'J', 20 => 'K', 21 => 'L', 22 => 'M', 23 => 'N', 24 => 'O', 25 => 'P', 26 => 'Q', 27 => 'R',
            28 => 'S', 29 => 'T', 30 => 'U', 31 => 'V', 32 => 'W', 33 => 'X', 34 => 'Y', 35 => 'Z'
        );
        $int = session('user.id');
        $arr = array();
        $loop = true;
        while ($loop) {
            $arr[] = $dic[bcmod($int, 36)];
            $int = floor(bcdiv($int, 36));
            if ($int == 0) $loop = false;
        }
        array_pad($arr, $format, $dic[0]);
        return implode('', array_reverse($arr));
    }
}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
if (!function_exists('uri')) {
    /**
     * 生成最短 URL 地址
     * @param string $url 路由地址
     * @param array $vars PATH 变量
     * @param boolean|string $suffix 后缀
     * @param boolean|string $domain 域名
     * @param boolean|string $fillSuffix 补上后缀
     * @return string
     */
    function uri($url = '', array $vars = [], $suffix = true, $domain = false, $fillSuffix = false)
    {
        return SystemService::instance()->uri($url, $vars, $suffix, $domain, $fillSuffix);
    }
}
李光春's avatar
李光春 已提交
117 118 119 120 121

if (!function_exists('dtacache')) {
    /**
     * 缓存
     * @param string $name
李光春's avatar
李光春 已提交
122
     * @param array $value
李光春's avatar
李光春 已提交
123 124 125
     * @param int $expire
     * @return bool|int|string
     * @throws CacheException
李光春's avatar
李光春 已提交
126 127 128
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
李光春's avatar
李光春 已提交
129 130 131 132 133
     */
    function dtacache($name = '', $value = [], $expire = 6000)
    {
        $myc = new Mysql();
        if (empty($value)) {
李光春's avatar
李光春 已提交
134
            return $myc->name($name)
李光春's avatar
李光春 已提交
135 136
                ->get();
        } else {
李光春's avatar
李光春 已提交
137 138
            if (empty($myc->name($name)
                ->get())) {
李光春's avatar
李光春 已提交
139 140 141 142 143 144 145 146
                $myc->name($name)
                    ->expire($expire)
                    ->set($value);
            } else {
                $myc->name($name)
                    ->expire($expire)
                    ->update($value);
            }
李光春's avatar
李光春 已提交
147 148 149 150 151
            return $myc->name($name)
                ->get();
        }
    }
}