// +---------------------------------------------------------------------- // 应用公共文件 // +---------------------------------------------------------------------+ // | 数组函数 // +---------------------------------------------------------------------+ if (!function_exists('sys_config')) { /** * 数组转换为对象 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/3/16 10:17 * * @param string $name 要查找的配置名称 * @param string $group 要查找的配置组名称 * @return mixed */ function sys_config($name = "", $group= "app") { // 直接配置 OR 读取数库 return config($group . '.' . $name); // return cache('system_config')[$group][$name]; } } if (!function_exists('get_annex_url')) { /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/3/20 0:09 * * @param string $url * @param string $driver * @return mixed|string */ function get_annex_url($url, $driver = 'local') { // return ($driver == 'qiniu' ? 'http://q6tf36mtr.bkt.clouddn.com/' // : 'http://img.gxrrj.cn') . $url; return site_url().'/uploads'.$url; } } if (!function_exists('filter_annex_url')) { /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/17 0:09 * * @param string $url * @param string $replace * @param string $driver * @return mixed|string */ function filter_annex_url($url,$replace = "", $driver = 'local') { // return str_replace(($driver == 'qiniu' ? 'http://q6tf36mtr.bkt.clouddn.com/' // : 'http://img.gxrrj.cn'), $replace , '/uploads' . $url); return site_url().$url; } } // +---------------------------------------------------------------------+ // | 数组函数 // +---------------------------------------------------------------------+ if (!function_exists('arr2tree')) { /** * 把返回的数据集转换成Tree * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/3/16 10:07 * * @param array $arr 传入数组 * @param string $pk 主键 * @param string $pid 父键 * @param string $child 子集 * @param int $root 根 * @return array|bool */ function arr2tree($arr = [], $pk = 'id', $pid = 'pid', $child = '_child', $root = 0) { $tree = []; if (!is_array($arr)) return false; // 创建基于主键的数组引用 $refer = []; foreach ($arr as $key => $data) { $refer[$data[$pk]] =& $arr[$key]; } foreach ($arr as $key => $data) { // 判断是否存在parent $parentId = $data[$pid]; if ($root == $parentId) { $tree[] =& $arr[$key]; } else if (isset($refer[$parentId])) { is_object($refer[$parentId]) && $refer[$parentId] = $refer[$parentId]->toArray(); $parent =& $refer[$parentId]; $parent[$child][] =& $arr[$key]; } } return $tree; } } if (!function_exists('parse_attr')) { /** * 分析数组及枚举类型配置值 格式 a:名称1,b:名称2 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/3/16 10:17 * * @param string $string 字符串 key:value * @return array|array[]|false|string[] */ function parse_attr($string) { $array = preg_split('/[,;\r\n]+/', trim($string, ",;\r\n")); $value = []; if (strpos($string, ':')) { foreach ($array as $val) { list($k, $v) = explode(':', $val); $value[$k] = $v; } } else $value = $array; return $value; } } if (!function_exists('arr2obj')) { /** * 数组转换为对象 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/3/16 10:17 * * @param array $arr 要转换的数组 * @return object|null */ function arr2obj($arr) { if (gettype($arr) != 'array') { return null; } foreach ($arr as $k => $v) { if (gettype($v) == 'array' || getType($v) == 'object') { $arr[$k] = (object)arr2obj($v); } } return (object)$arr; } } if (!function_exists('arr2str')) { /** * 数组转换为字符串,主要用于把分隔符调整到第二个参数 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/3/16 10:20 * * @param array $arr 要连接的数组 * @param string $glue 分割符 * @return string 返回字符串 */ function arr2str($arr, $glue = ',') { return implode($glue, $arr); } } // +---------------------------------------------------------------------+ // | 字符串函数 // +---------------------------------------------------------------------+ if (!function_exists('get_rand_char')) { /** * 获取随机字符 * * @param string $length 长度 * @param bool $strtoup 是否大写 * @return null|string */ function get_rand_char($length = '32', $strtoup = false) { $str = null; $strPol = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ23456789abcdefghijklmnopqrstuvwxyz'; for ($i = 0; $i < $length; $i++) { $str .= $strPol[rand(0, strlen($strPol) - 1)]; } return $strtoup ? strtoupper($str) : $str; } } if (!function_exists('get_micro_time')) { /** * 获取毫秒级13位时间戳 * * @return string */ function get_micro_time() { list($t1, $t2) = explode(' ', microtime()); return $t2 . ceil($t1 * 1000); } } if (!function_exists('str2arr')) { /** * 字符串转换为数组,主要用于把分隔符调整到第二个参数 * * @param string $str 要分割的字符串 * @param string $glue 分割符 * * @return array */ function str2arr($str, $glue = ',') { return explode($glue, preg_replace('/[ ]/', '', $str)); } } if (!function_exists('obj2arr')) { /** * 对象转数组 * * @param object $obj 对象 * @return array */ function obj2arr($obj) { $obj = (array)$obj; foreach ($obj as $k => $v) { if (gettype($v) == 'resource') { return null; } if (gettype($v) == 'object' || gettype($v) == 'array') { $obj[$k] = (array)obj2arr($v); } } return $obj; } } if (!function_exists('str_unique')) { /** * 唯一字符串 * * @param string $str 要唯一的字符串 * @return string */ function str_unique($str) { // 先转数组排序 -- 去重 $arr = array_unique(str2arr($str)); asort($arr); // 转字符串去空格 return trim(arr2str($arr),','); } } // +---------------------------------------------------------------------+ // | 其他函数 // +---------------------------------------------------------------------+ if (!function_exists('enjson')) { /** * JSON ENCODE * * @param array $arr 数据组 * @return string */ function enjson($arr) { return json_encode($arr, JSON_UNESCAPED_UNICODE); } } if (!function_exists('dejson')) { /** * JSON DECODE * * @param string $str 字符串 * @return array */ function dejson($str) { return json_decode($str, true); } } if (!function_exists('get_user_agent')){ /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/3/16 13:07 * * @param string $match * @return string */ function get_user_agent($match = 'os') { // 请求agent $user_agent = $_SERVER['HTTP_USER_AGENT']; // 返回值 $out = "Unknown"; if (!empty($user_agent)) { if ($match == 'os'){ if (preg_match('/win/i', $user_agent)) { $out = 'Windows'; } else if (preg_match('/mac/i', $user_agent)) { $out = 'MAC'; } else if (preg_match('/linux/i', $user_agent)) { $out = 'Linux'; } else if (preg_match('/unix/i', $user_agent)) { $out = 'Unix'; } else if (preg_match('/bsd/i', $user_agent)) { $out = 'BSD'; } else { $out = 'Other'; } }else{ if (preg_match('/MSIE/i', $user_agent)) { $out = 'MSIE'; } else if (preg_match('/Firefox/i', $user_agent)) { $out = 'Firefox'; } else if (preg_match('/Chrome/i', $user_agent)) { $out = 'Chrome'; } else if (preg_match('/Safari/i', $user_agent)) { $out = 'Safari'; } else if (preg_match('/Opera/i', $user_agent)) { $out = 'Opera'; } else { $out = 'Other'; } } } return $out; } } if (!function_exists('get_file_ext')) { /** * 取得文件后缀 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/3/19 23:31 * * @param string $path * @return mixed|string */ function get_file_ext($path = '') { if(!empty(pathinfo($path))){ return isset(pathinfo($path)['extension']) ? pathinfo($path)['extension'] : 'none'; } return 'none'; } } /********2020-06-11 新增**********/ if (!function_exists('make_mcard_id')) { /** * 获取唯一ID * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/6/11 14:33 * * @param string $url * @return mixed|string */ function make_mcard_id() { $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $rand = $code[rand(0, 25)] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -2) . substr(microtime(), 2, 8) . sprintf('%02d', rand(0, 99)); for ( $a = md5($rand, true), $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV', $d = '', $f = 0; $f < 10; $g = ord($a[$f]), $d .= $s[($g ^ ord($a[$f + 5])) - $g & 0x1F], $f++ ) ; return $d; } } if (!function_exists('get_order_no')) { /** * 获取订单号 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/1 14:20 * * @return string */ function get_order_no() { return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8); } } if (!function_exists('curl_post')) { /** * CURL POST * * @param string $url post请求地址 * @param array $params * * @return mixed */ function curl_post($url, array $params = array()) { $data_string = json_encode($params); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' ) ); $data = curl_exec($ch); curl_close($ch); return ($data); } } if (!function_exists('curl_post_raw')) { /** * CURL POST * * @param string $url post请求地址 * @param array $rawData * * @return mixed */ function curl_post_raw($url, $rawData) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: text' ) ); $data = curl_exec($ch); curl_close($ch); return ($data); } } if (!function_exists('curl_get')) { /** * CURL GET * * @param string $url get请求地址 * @param int $httpCode 返回状态码 * * @return mixed */ function curl_get($url, &$httpCode = 0) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不做证书校验,部署在linux环境下请改为true curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); $file_contents = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return $file_contents; } } if(!function_exists('push_socket_data')){ /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/8/27 16:46 * * @param string $type * @param $data */ function push_socket_data($type = 'goods', $data = ''){ curl_post('http://127.0.0.1:2121?type=' . $type . '&content=' . enjson($data)); } } if(!function_exists('p')){ /** * * @param string $data * @param bool $die * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/8/27 16:46 * */ function p($data = '', $die = false){ echo "
";
        print_r($data);
        if ($die) {
            die();
        }
    }
}

if (!function_exists('site_url')) {
    /**
     * @desc 返回URL协议和域名
     * @link https://www.sunzhongwei.com/php-request-for-domain-name-and-agreement?from=sidebar_new
     * @author 大象笔记
     * @return string
     * @date 2020/04/14
     */
    function site_url() {
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $domainName = $_SERVER['HTTP_HOST'];

        return $protocol . $domainName;
    }
}