common.php 3.7 KB
Newer Older
李光春's avatar
test  
李光春 已提交
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
test  
李光春 已提交
17 18 19
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------

李光春's avatar
李光春 已提交
20
use DtApp\ThinkLibrary\cache\Mysql;
李光春's avatar
李光春 已提交
21
use DtApp\ThinkLibrary\exception\DtaException;
李光春's avatar
李光春 已提交
22
use DtApp\ThinkLibrary\service\QqWryService;
23
use DtApp\ThinkLibrary\service\SystemService;
李光春's avatar
李光春 已提交
24
use think\db\exception\DbException;
李光春's avatar
test  
李光春 已提交
25

李光春's avatar
李光春 已提交
26 27 28
/**
 * 定义当前版本
 */
29
const VERSION = '6.0.106';
李光春's avatar
李光春 已提交
30

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

if (!function_exists('get_ip')) {
    /**
     * 获取请求IP
     * @return string
     */
    function get_ip()
    {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            //为了兼容百度的CDN,所以转成数组
李光春's avatar
李光春 已提交
62
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
李光春's avatar
李光春 已提交
63
            return $arr[0];
李光春's avatar
李光春 已提交
64 65 66
        } else {
            return $_SERVER['REMOTE_ADDR'];
        }
李光春's avatar
test  
李光春 已提交
67 68
    }
}
李光春's avatar
李光春 已提交
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
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
李光春 已提交
85 86 87 88 89

if (!function_exists('dtacache')) {
    /**
     * 缓存
     * @param string $name
李光春's avatar
李光春 已提交
90
     * @param array $value
李光春's avatar
李光春 已提交
91 92
     * @param int $expire
     * @return bool|int|string
李光春's avatar
李光春 已提交
93
     * @throws DbException
李光春's avatar
李光春 已提交
94
     * @throws DtaException
李光春's avatar
李光春 已提交
95 96 97 98 99
     */
    function dtacache($name = '', $value = [], $expire = 6000)
    {
        $myc = new Mysql();
        if (empty($value)) {
李光春's avatar
李光春 已提交
100
            return $myc->name($name)
李光春's avatar
李光春 已提交
101 102
                ->get();
        } else {
李光春's avatar
李光春 已提交
103 104
            if (empty($myc->name($name)
                ->get())) {
李光春's avatar
李光春 已提交
105 106 107 108 109 110 111 112
                $myc->name($name)
                    ->expire($expire)
                    ->set($value);
            } else {
                $myc->name($name)
                    ->expire($expire)
                    ->update($value);
            }
李光春's avatar
李光春 已提交
113 114 115 116 117
            return $myc->name($name)
                ->get();
        }
    }
}