common.php 3.6 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
test  
李光春 已提交
24

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

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

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

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

if (!function_exists('dtacache')) {
    /**
     * 缓存
     * @param string $name
李光春's avatar
李光春 已提交
88
     * @param array $value
李光春's avatar
李光春 已提交
89 90
     * @param int $expire
     * @return bool|int|string
李光春's avatar
李光春 已提交
91
     * @throws DtaException
李光春's avatar
李光春 已提交
92
     * @throws \think\db\exception\DbException
李光春's avatar
李光春 已提交
93 94 95 96 97
     */
    function dtacache($name = '', $value = [], $expire = 6000)
    {
        $myc = new Mysql();
        if (empty($value)) {
李光春's avatar
李光春 已提交
98
            return $myc->name($name)
李光春's avatar
李光春 已提交
99
                ->get();
李光春's avatar
李光春 已提交
100 101 102 103 104 105 106 107
        }

        $judge = $myc->name($name)
            ->get();
        if (empty($judge)) {
            $myc->name($name)
                ->expire($expire)
                ->set($value);
李光春's avatar
李光春 已提交
108
        } else {
李光春's avatar
李光春 已提交
109 110 111
            $myc->name($name)
                ->expire($expire)
                ->update($value);
李光春's avatar
李光春 已提交
112
        }
李光春's avatar
李光春 已提交
113 114
        return $myc->name($name)
            ->get();
李光春's avatar
李光春 已提交
115 116
    }
}