| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 流年 <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- // 应用公共文件
- // +---------------------------------------------------------------------+
- // | 数组函数
- // +---------------------------------------------------------------------+
- 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 "<pre>";
- 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 大象笔记<zhongwei.sun2008在gmail.com>
- * @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;
- }
- }
|