Files
2022-11-10 16:59:43 +08:00

1187 lines
38 KiB
PHP
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
* 加载缓存工具
* @param string $type
* @param array $config
* @return bool
*/
if (!function_exists('load_cache')) {
function &load_cache($type = 'redis', $config = array())
{
$CI = &get_instance();
$CI->load->driver('cache');
$cache = null;
switch ($type) {
case 'redis':
// $cache = $CI->cache->redis;
// break;
$CI->load->library('myredis');
$CI->myredis->init($config);
return $CI->myredis;
case 'file':
$cache = $CI->cache->file;
break;
default:
$cache = $CI->cache->memcached;
break;
}
return $cache;
}
}
/**
* 接口加密
* @param $string
* @param string $operation
* @param string $key
* @param int $expiry
* @return string
*/
if (!function_exists('liche_authcode')) {
function liche_authcode($string, $operation = 'DECODE', $key = '123456', $expiry = 0)
{
$ckey_length = 4;
$key = md5($key ? $key : self::KEY);
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
$cryptkey = $keya . md5($keya . $keyc);
$key_length = strlen($cryptkey);
$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
$string_length = strlen($string);
$result = '';
$box = range(0, 255);
$rndkey = array();
for ($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
for ($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for ($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}
if ($operation == 'DECODE') {
if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
} else {
return $keyc . str_replace('=', '', base64_encode($result));
}
}
}
/**
* sql日志
* @param $string
* @param $filename
* @return bool
*/
if (!function_exists('debug_sql')) {
function debug_sql($logmsg = '')
{
$dbs = array();
$CI = &get_instance();
foreach (get_object_vars($CI) as $CI_object) {
if (is_object($CI_object)) {
$class_name = get_class($CI_object);
if (stristr($class_name, 'model')) {
$logmsg .= '[' . $class_name . '] ';
}
}
}
if (class_exists('HD_Model') && !empty(HD_Model::$dbs)) {
$num = 0;
foreach (HD_Model::$dbs as $db_object) {
$dbs['count'] = count($db_object->queries);
if ($dbs['count'] > 0) {
for ($i = 0; $i < $dbs['count']; $i++) {
$dbs['sql'] .= "\n" . "$num => " . str_replace(array("\r", "\n", "\r\n"), array(' ', ' ', ' '), $db_object->queries[$i]);
$dbs['sql'] .= " | time " . $db_object->query_times[$i];
$num++;
}
}
}
}
$CI->benchmark->mark('total_execution_time_end');
$totaltime = $CI->benchmark->elapsed_time("total_execution_time_start", "total_execution_time_end");
if ($dbs) {
$logmsg .= "|totaltime:" . $totaltime . "|sqlcount:" . $dbs['count'] . "|" . $dbs['sql'];
}
debug_log($logmsg . "\n", 'mysql.log');
}
}
/**
* debug日志
* @param $string
* @param $filename
* @param $sub_dir "子目录"
* @return bool
*/
if (!function_exists('debug_log')) {
function debug_log($string, $filename = 'log.txt', $sub_dir = '')
{
$log_dir = APPPATH . 'logs/';
if (!file_exists($log_dir)) {
return false;
}
$log_dir = $log_dir . date('Y/m/d');
if (!file_exists($log_dir)) {
$oldumask = umask(0);
mkdir($log_dir, 0777, true);
umask($oldumask);
}
if ($sub_dir) {
$log_dir .= "/{$sub_dir}";
if (!file_exists($log_dir)) {
$oldumask = umask(0);
mkdir($log_dir, 0777, true);
umask($oldumask);
}
}
$log_dir = $log_dir . '/';
if (!$filename) {
$filename = $log_dir;
} else {
$filename = $log_dir . $filename;
}
if (!file_exists($filename)) {
$oldumask = umask(0);
touch($filename);
chmod($filename, 0666);
umask($oldumask);
}
$handle = fopen($filename, 'a+');
if (!is_string($string)) {
$string = var_export($string, TRUE);
}
list($msec, $sec) = explode(' ', microtime());
fwrite($handle, date('H:i:s') . ' ' . floatval($msec) * 1000 . PHP_EOL . $string . PHP_EOL);
fclose($handle);
return true;
}
}
/**
* 检查手机号码是否合法,合法返回true.
* @param string $mobile
* @return bool
*/
if (!function_exists('mobile_valid')) {
function mobile_valid($mobile = '')
{
return preg_match("/^1\d{10}$/", $mobile);
}
}
/**
* 校验ua
* @return string
*/
if (!function_exists('checkua')) {
function checkua()
{
if (is_mobile()) {
if (strstr($_SERVER['HTTP_USER_AGENT'], 'wxwork')) {
return 'wxwork';//企业微信
} else if (strstr($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')) {
return 'wx';//微信
} else if (strstr($_SERVER['HTTP_USER_AGENT'], 'microfish')) {
return 'xy';
} else {
return 'wap';
}
} else {
return 'pc';
}
}
}
if (!function_exists('is_mobile')) {
function is_mobile()
{
$regex_match = "/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";
$regex_match .= "htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
$regex_match .= "blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
$regex_match .= "symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
$regex_match .= "jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
$regex_match .= ")/i";
return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));
}
}
/**
* 二维数组跟据某个字段按另一个数组的顺序排序
* @param $src_array
* @param $sort_key
* @param $sort_array
* @return array
*/
if (!function_exists('array_sort_by_array')) {
function array_sort_by_array($src_array, $sort_key, $sort_array)
{
$sort_weight = array_flip($sort_array);
$result = array();
foreach ($src_array as $item) {
if (isset($sort_weight[$item[$sort_key]])) {
$result[$sort_weight[$item[$sort_key]]] = $item;
}
}
ksort($result);
return array_values($result);
}
}
/**
* 一维数据数组生成数据树
* @param array $list 数据列表
* @param string $id ID Key
* @param string $pid 父ID Key
* @param string $path
* @param string $ppath
* @return array
*/
if (!function_exists('arr2table')) {
function arr2table(array $list, $id = 'id', $pid = 'pid', $path = 'path', $ppath = '')
{
$tree = array();
foreach (arr2tree($list, $id, $pid) as $attr) {
$attr[$path] = "{$ppath}-{$attr[$id]}";
$attr['sub'] = isset($attr['sub']) ? $attr['sub'] : array();
$attr['spt'] = substr_count($ppath, '-');
$attr['spl'] = str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;├&nbsp;", $attr['spt']);
$sub = $attr['sub'];
unset($attr['sub']);
$tree[] = $attr;
if (!empty($sub)) {
$tree = array_merge($tree, arr2table($sub, $id, $pid, $path, $attr[$path]));
}
}
return $tree;
}
}
/**
* 一维数据数组生成数据树
* @param array $list 数据列表
* @param string $id 父ID Key
* @param string $pid ID Key
* @param string $son 定义子数据Key
* @return array
*/
if (!function_exists('arr2tree')) {
function arr2tree($list, $id = 'id', $pid = 'pid', $son = 'sub')
{
list($tree, $map) = array(array(), array());
foreach ($list as $item) {
$map[$item[$id]] = $item;
}
foreach ($list as $item) {
if (isset($item[$pid]) && isset($map[$item[$pid]])) {
$map[$item[$pid]][$son][] = &$map[$item[$id]];
} else {
$tree[] = &$map[$item[$id]];
}
}
unset($map);
return $tree;
}
}
/**
* 获取数据树子ID
* @param array $list 数据列表
* @param int $id 起始ID
* @param string $key 子Key
* @param string $pkey 父Key
* @return array
*/
if (!function_exists('array_sub_ids')) {
function array_sub_ids($list, $id = 0, $key = 'id', $pkey = 'pid')
{
$ids = array(intval($id));
foreach ($list as $v) {
if (intval($v[$pkey]) > 0 && intval($v[$pkey]) === intval($id)) {
$ids = array_merge($ids, array_sub_ids($list, intval($v[$key]), $key, $pkey));
}
}
return $ids;
}
}
/*
* 分页
*/
if (!function_exists('page_view')) {
//
function page_view($config)
{
$CI = &get_instance();
$CI->load->view('pager', $config);
}
}
/**
* 加载分页js脚本
*/
if (!function_exists('page_script')) {
//
function page_script($config)
{
$CI = &get_instance();
$CI->load->view('pager_script', $config);
}
}
/**
* 加载页面
* @param $url
*/
if (!function_exists('load_view')) {
function load_view($url)
{
$CI = &get_instance();
$CI->load->view($url);
}
}
/*
* 获取ip
*/
if (!function_exists('get_client_ip')) {
function get_client_ip($type = 0, $show_port = false)
{
$type = $type ? 1 : 0;
static $ip = NULL;
if ($ip !== NULL) {
return $ip[$type];
}
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$pos = array_search('unknown', $arr);
if (false !== $pos) {
unset($arr[$pos]);
}
$ip = trim($arr[0]);
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
// IP地址合法验证
$long = sprintf("%u", ip2long($ip));
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
if ($show_port) {
return $ip[$type] . ":" . $_SERVER['REMOTE_PORT'];
}
return $ip[$type];
}
}
/**
* 阿里短信
*/
if (!function_exists('send_sms')) {
function send_sms($mobile, $code, $sign = '狸车')
{
require_once COMMPATH . '/third_party/alisms/alisms.php';
$template = 'SMS_218630210';
$alisms = new AliSms();
$alisms->sendSms($mobile, array('code' => $code), $template, $sign);
return true;
}
}
if (!function_exists('send_alisms')) {
function send_alisms($json)
{
require_once COMMPATH . '/third_party/alisms/alisms.php';
if (!$json['mobile'] || !$json['template']) {
return false;
}
$mobile = $json['mobile'];
$template = $json['template'];
$sign = $json['sign'] ? $json['sign'] : '狸车宝';
$templateParam = $json['param'];
$alisms = new AliSms();
$alisms->sendSms($mobile, $templateParam, $template, $sign);
return true;
}
}
/**
* 北京亿美短信
*/
if (!function_exists('b2m_send_sms')) {
function b2m_send_sms($mobile, $content)
{
require_once COMMPATH . '/third_party/b2m/sms.php';
$resobj = SendSMS($mobile, $content);
$resobj->ciphertext = "";
return $resobj ? $resobj->plaintext : false;
}
}
/**
* 北京亿美短信
*/
if (!function_exists('b2m_send_batch_sms')) {
function b2m_send_batch_sms($mobiles, $content)
{
require_once COMMPATH . '/third_party/b2m/sms.php';
$resobj = SendBatchSMS($mobiles, $content);
$resobj->ciphertext = "";
return $resobj ? $resobj->plaintext : false;
}
}
/*
* 随机码
*/
if (!function_exists('getNonceStr')) {
/**
* 随机字母数字串
* @param int $length (长度)
* @return string
*/
function getNonceStr($length = 32)
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
}
if (!function_exists('getNonceStrnum')) {
/**
* 随机数字串
* @param int $length (长度)
* @return string
*/
function getNonceStrnum($length = 32)
{
$chars = "0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
}
/**
* 返回七牛缩略图地址
* @param $url
* @param $width
* @param int $height
* @param string $type 存储空间 默认img (img,video)
* @param string $storage
* @return string
*/
if (!function_exists('build_qiniu_image_url')) {
function build_qiniu_image_url($url = '', $width = 0, $height = 0, $type = '')
{
$thumb = $url;
if (!preg_match('/^(https?:)?\/\//', $url)) {
$CI = &get_instance();
$config = array();
$type && $config['type'] = $type;
if ($type == 'video') {
$CI->load->library('qiniu', $config, 'qiniu_video');
$thumb = $CI->qiniu_video->getBaseUriQiniu() . $url;
} else {
$CI->load->library('qiniu', $config);
$thumb = $CI->qiniu->getBaseUriQiniu() . $url;
}
}
if ($width && $height) {
$connect = strpos($thumb, '?') ? '&' : '?';//判断url是否存在?号
$thumb .= $connect . "imageView2/1/w/{$width}/h/{$height}/interlace/1";//等比缩放,居中裁剪
} elseif ($width) {
$connect = strpos($thumb, '?') ? '&' : '?';//判断url是否存在?号
$thumb .= $connect . "imageView2/2/w/{$width}/h/{$height}/interlace/1";//等比缩放,不裁剪
}
//有些保存完整url的替换域名,支持小鱼域名,不替换
if ('xiaoyu' != $type) {
$thumb = str_replace("qimg.shequ.xiaoyu.com", "qimg.haodian.cn", $thumb);
}
return $thumb;
}
}
/**
* 返回七牛视频
* @param $url (视频地址)
* @param $frame (指定第几帧作为视频照片)
* @param string $type 存储空间 默认img (img,video)
* @return array (url, img)
*/
if (!function_exists('build_qiniu_video')) {
function build_qiniu_video($url, $frame = 0, $type = '')
{
if (!$url) {
return array();
}
$src = $url . '?vframe/jpg/offset/' . $frame;
$url = build_qiniu_image_url($url, 0, 0, $type);
$src = build_qiniu_image_url($src, 0, 0, $type);
return array($url, $src);
}
}
if (!function_exists('build_app_url')) {
/**
* 获取完整的url,前端page打头的是小程序页面加‘/’
* @param $url
* @return string
*/
function build_app_url($url)
{
if ('page' == substr($url, 0, 4)) {
$url = '/' . $url;
}
return $url;
}
}
/**
* 手机隐藏信息
* @param string $mobile 手机号
* @return string 隐藏后的手机号
*
*/
if (!function_exists('mobile_asterisk')) {
function mobile_asterisk($mobile)
{
$mobile_asterisk = substr($mobile, 0, 3) . "****" . substr($mobile, 7, 4);
return $mobile_asterisk;
}
}
/**
* 姓名隐藏信息
* @param string $mobile 姓名
* @return string 隐藏后的姓名
*
*/
if (!function_exists('name_asterisk')) {
function name_asterisk($str)
{
//判断是否包含中文字符
if (preg_match("/[\x{4e00}-\x{9fa5}]+/u", $str)) {
//按照中文字符计算长度
$len = mb_strlen($str, 'UTF-8');
//echo '中文';
if ($len >= 3) {
//三个字符或三个字符以上掐头取尾,中间用*代替
$str = mb_substr($str, 0, 1, 'UTF-8') . '**';
} elseif ($len == 2) {
//两个字符
$str = mb_substr($str, 0, 1, 'UTF-8') . '*';
}
} else {
//按照英文字串计算长度
$len = strlen($str);
//echo 'English';
if ($len >= 3) {
//三个字符或三个字符以上掐头取尾,中间用*代替
$str = substr($str, 0, 1) . '**';
} elseif ($len == 2) {
//两个字符
$str = substr($str, 0, 1) . '*';
}
}
return $str;
}
}
/**
*数字金额转换成中文大写金额的函数
*String Int $num 要转换的小写数字或小写字符串
*return 大写字母
*小数位为两位
**/
if (!function_exists('num_to_rmb')) {
function num_to_rmb($num)
{
$c1 = "零壹贰叁肆伍陆柒捌玖";
$c2 = "分角元拾佰仟万拾佰仟亿";
//精确到分后面就不要了,所以只留两个小数位
$num = round($num, 2);
//将数字转化为整数
$num = $num * 100;
if (strlen($num) > 10) {
return "金额太大,请检查";
}
$i = 0;
$c = "";
while (1) {
if ($i == 0) {
//获取最后一位数字
$n = substr($num, strlen($num) - 1, 1);
} else {
$n = $num % 10;
}
//每次将最后一位数字转化为中文
$p1 = substr($c1, 3 * $n, 3);
$p2 = substr($c2, 3 * $i, 3);
if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
$c = $p1 . $p2 . $c;
} else {
$c = $p1 . $c;
}
$i = $i + 1;
//去掉数字最后一位了
$num = $num / 10;
$num = (int)$num;
//结束循环
if ($num == 0) {
break;
}
}
$j = 0;
$slen = strlen($c);
while ($j < $slen) {
//utf8一个汉字相当3个字符
$m = substr($c, $j, 6);
//处理数字中很多0的情况,每次循环去掉一个汉字“零”
if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
$left = substr($c, 0, $j);
$right = substr($c, $j + 3);
$c = $left . $right;
$j = $j - 3;
$slen = $slen - 3;
}
$j = $j + 3;
}
//这个是为了去掉类似23.0中最后一个“零”字
if (substr($c, strlen($c) - 3, 3) == '零') {
$c = substr($c, 0, strlen($c) - 3);
}
//将处理的汉字加上“整”
if (empty($c)) {
return "零元整";
} else {
return $c . "";
}
}
}
/**
* 友好的时间显示
*
* @param int $sTime 待显示的时间
* @param string $type 类型. normal | mohu | full | ymd | other
* @return string
*/
if (!function_exists('friendly_date')) {
function friendly_date($sTime, $type = 'normal',$d_type = '')
{
if (!$sTime) return '';
//sTime=源时间,cTime=当前时间,dTime=时间差
$cTime = time();
$dTime = $cTime - $sTime;
$dDay = intval(date("z", $cTime)) - intval(date("z", $sTime));
//$dDay = intval($dTime/3600/24);
$dYear = intval(date("Y", $cTime)) - intval(date("Y", $sTime));
//normal:n秒前,n分钟前,n小时前,日期
if ($type == 'normal') {
if ($dTime < 60) {
if ($dTime < 10) {
return '刚刚'; //by yangjs
} else {
return intval(floor($dTime / 10) * 10) . "秒前";
}
} elseif ($dTime < 3600) {
return intval($dTime / 60) . "分钟前";
//今天的数据.年份相同.日期相同.
} elseif ($dYear == 0 && $dDay == 0) {
//return intval($dTime/3600)."小时前";
return '今天' . date('H:i', $sTime);
} elseif ($dYear == 0) {
return $d_type ? date("m.d H:i", $sTime) : date("m月d日 H:i", $sTime) ;
} else {
return $d_type ? date("Y.m.d H:i", $sTime) : date("Y-m-d H:i", $sTime) ;
}
} elseif ($type == 'mohu') {
if ($dTime < 60) {
return $dTime . "秒前";
} elseif ($dTime < 3600) {
return intval($dTime / 60) . "分钟前";
} elseif ($dTime >= 3600 && $dDay == 0) {
return intval($dTime / 3600) . "小时前";
} elseif ($dDay > 0 && $dDay <= 7) {
return intval($dDay) . "天前";
} elseif ($dDay > 7 && $dDay <= 30) {
return intval($dDay / 7) . '周前';
} elseif ($dDay > 30) {
return intval($dDay / 30) . '个月前';
}
//full: Y-m-d , H:i:s
} elseif ($type == 'full') {
return date("Y-m-d , H:i:s", $sTime);
} elseif ($type == 'ymd') {
return date("Y-m-d", $sTime);
} else {
if ($dTime < 60) {
return $dTime . "秒前";
} elseif ($dTime < 3600) {
return intval($dTime / 60) . "分钟前";
} elseif ($dTime >= 3600 && $dDay == 0) {
return intval($dTime / 3600) . "小时前";
} elseif ($dYear == 0) {
return date("Y-m-d H:i:s", $sTime);
} else {
return date("Y-m-d H:i:s", $sTime);
}
}
return '';
}
}
/**
* Notes:求两个日期之间相差的天数
* Created on: 2021/01/05 18:20
* Created by: dengbw
* @param $day1
* @param $day2
* @return number
*/
if (!function_exists('diff_between_twoDays')) {
function diff_between_twoDays($day1, $day2)
{
$second1 = strtotime($day1);
$second2 = strtotime($day2);
if ($second1 < $second2) {
$tmp = $second2;
$second2 = $second1;
$second1 = $tmp;
}
$day = (($second1 - $second2) / 86400);
return $day;
}
}
if (!function_exists('status_verify')) {
//
function status_verify($status)
{
if ($status || $status == '0') {
return true;
} else return false;
}
}
/**
* Notes:商品图片
* Created on: 2020/3/12 16:57
* Created by: dengbw
* @param $json
* @param int $width
* @param int $height
* @return string
*/
if (!function_exists('item_img')) {
function item_img($json, $width = 0, $height = 0)
{
$item_img = '';
$imgs = json_decode($json, true);
if ($imgs) {
if ($imgs['cover']) {
$item_img = $imgs['cover'];//商品封面
} elseif ($imgs['banner']) {
$item_img = $imgs['banner'][0];//商品图第一张
}
$item_img = build_qiniu_image_url($item_img, $width, $height);
}
return $item_img;
}
}
/**
* POST 请求
* @param string $url
* @param array $post_data
* @return string content
*/
if (!function_exists('http_post_com')) {
function http_post_com($url, $post_data)
{
if (empty($post_data)) {
$post_data = array('1' => 1);
}
if (empty($url)) {
return false;
}
$postUrl = $url;
$curlPost = http_build_query($post_data);//处理数组
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch);//运行curl
curl_close($ch);
$data = trim($data, chr(239) . chr(187) . chr(191));
$data = json_decode($data, true);
return $data;
}
}
/**
* Notes:格式化数字
* Created on: 2020/7/1 16:17
* Created by: dengbw
* @param $number
* @param int $decimals
* @param string $pr
* @return string
*/
if (!function_exists('number_format_com')) {
function number_format_com($number, $decimals = 2, $pr = '¥')
{
$number = $number ? $number : 0.00;
return $pr . number_format($number, $decimals, '.', '');
}
}
/**
* 获取当前协议类型
* @return string
* @author:xxb
* @time:2021/03/02
*/
if (!function_exists('server_http')) {
function server_http()
{
if (false !== strpos($_SERVER['HTTP_HOST'], 'dev')) { //dev 测试
$http = 'http://';
} elseif (false !== strpos($_SERVER['HTTP_HOST'], 'test')) {//test 测试
$http = 'https://';
} else { // 正式
$http = 'https://';
}
return $http;
}
}
/**
* Notes:获取url地址
* Created on: 2020/11/2 15:27
* Created by: dengbw
* @param string $type
* @return string
*/
if (!function_exists('http_host_com')) {
function http_host_com($type = 'api')
{
$url = '';
if (false !== strpos($_SERVER['HTTP_HOST'], 'dev')) { //dev 测试
if ($type == 'api') {
$url = 'https://liche-api-dev.xiaoyu.com';
} else if ($type == 'home') {
$url = "https://liche-dev.xiaoyu.com";
} else if ($type == 'admin') {
$url = "http://liche-admin.dev.xiaoyu.com";
}
} elseif (false !== strpos($_SERVER['HTTP_HOST'], 'test')) {//test 测试
if ($type == 'api') {
$url = 'api.lc.haodian.cn';
} else if ($type == 'home') {
$url = "https://www-test.liche.cn";
} else if ($type == 'admin') {
$url = "https://admin.test.liche.cn";
}
} else { // 正式
if ($type == 'api') {
$url = 'https://api.liche.cn';
} else if ($type == 'home') {
$url = "https://www.liche.cn";
} else if ($type == 'admin') {
$url = "https://admin.liche.cn";
}
}
return $url;
}
}
/**
* Notes:二维数组根据某个键排序
* Created on: 2020/11/06 18:20
* Created by: dengbw
* @param $arrays
* @param $sort_key //通过这个字段排序
* @param int $sort_order //SORT_DESC表示降序排列,SORT_STRING表示设置'sort_key'字段的比较以字符串方式进行
* @param int $sort_type
* @return bool
*/
if (!function_exists('sort_array')) {
function sort_array($arrays, $sort_key, $sort_order = SORT_ASC, $sort_type = SORT_NUMERIC)
{
$key_arrays = array();
if (is_array($arrays)) {
foreach ($arrays as $array) {
if (is_array($array)) {
$key_arrays[] = $array[$sort_key];
} else {
return false;
}
}
} else {
return false;
}
array_multisort($key_arrays, $sort_order, $sort_type, $arrays);
return $arrays;
}
}
/*
* 随机获取用户名
*/
if (!function_exists('rand_name')) {
function rand_name()
{
$fam_arr = array(
'赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚', '卫', '蒋',
'沈', '韩', '杨', '朱', '秦', '尤', '许', '何', '吕', '施', '张', '孔', '曹', '严', '华', '金', '魏',
'陶', '姜', '戚', '谢', '邹', '喻', '柏', '水', '窦', '章', '云', '苏', '潘', '葛', '奚', '范', '彭',
'郎', '鲁', '韦', '昌', '马', '苗', '凤', '花', '方', '任', '袁', '柳', '鲍', '史', '唐', '费', '薛',
'雷', '贺', '倪', '汤', '滕', '殷', '罗', '毕', '郝', '安', '常', '傅', '卞', '齐', '元', '顾', '孟',
'平', '黄', '穆', '萧', '尹', '姚', '邵', '湛', '汪', '祁', '毛', '狄', '米', '伏', '成', '戴', '谈',
'宋', '茅', '庞', '熊', '纪', '舒', '屈', '项', '祝', '董', '梁', '杜', '阮', '蓝', '闵', '季', '贾',
'路', '娄', '江', '童', '颜', '郭', '梅', '盛', '林', '钟', '徐', '邱', '骆', '高', '夏', '蔡', '田',
'樊', '胡', '凌', '霍', '虞', '万', '支', '柯', '管', '卢', '莫', '柯', '房', '裘', '缪', '解', '应',
'宗', '丁', '宣', '邓', '单', '杭', '洪', '包', '诸', '左', '石', '崔', '吉', '龚', '程', '嵇', '邢',
'裴', '陆', '荣', '翁', '荀', '于', '惠', '甄', '曲', '封', '储', '仲', '伊', '宁', '仇', '甘', '武',
'符', '刘', '景', '詹', '龙', '叶', '幸', '司', '黎', '溥', '印', '怀', '蒲', '邰', '从', '索', '赖',
'卓', '屠', '池', '乔', '胥', '闻', '莘', '党', '翟', '谭', '贡', '劳', '逄', '姬', '申', '扶', '堵',
'冉', '宰', '雍', '桑', '寿', '通', '燕', '浦', '尚', '农', '温', '别', '庄', '晏', '柴', '瞿', '阎',
'连', '习', '容', '向', '古', '易', '廖', '庾', '终', '步', '都', '耿', '满', '弘', '匡', '国', '文',
'寇', '广', '禄', '阙', '东', '欧', '利', '师', '巩', '聂', '关', '荆', '司马', '上官', '欧阳', '夏侯',
'诸葛', '闻人', '东方', '赫连', '皇甫', '尉迟', '公羊', '澹台', '公冶', '宗政', '濮阳', '淳于', '单于',
'太叔', '申屠', '公孙', '仲孙', '轩辕', '令狐', '徐离', '宇文', '长孙', '慕容', '司徒', '司空'
);
$name_arr = array(
'伟', '刚', '勇', '毅', '俊', '峰', '强', '军', '平', '保', '东', '文', '辉', '力', '明', '永', '健', '世', '广', '志', '义',
'兴', '良', '海', '山', '仁', '波', '宁', '贵', '福', '生', '龙', '元', '全', '国', '胜', '学', '祥', '才', '发', '武', '新',
'利', '清', '飞', '彬', '富', '顺', '信', '子', '杰', '涛', '昌', '成', '康', '星', '光', '天', '达', '安', '岩', '中', '茂',
'进', '林', '有', '坚', '和', '彪', '博', '诚', '先', '敬', '震', '振', '壮', '会', '思', '群', '豪', '心', '邦', '承', '乐',
'绍', '功', '松', '善', '厚', '庆', '磊', '民', '友', '裕', '河', '哲', '江', '超', '浩', '亮', '政', '谦', '亨', '奇', '固',
'之', '轮', '翰', '朗', '伯', '宏', '言', '若', '鸣', '朋', '斌', '梁', '栋', '维', '启', '克', '伦', '翔', '旭', '鹏', '泽',
'晨', '辰', '士', '以', '建', '家', '致', '树', '炎', '德', '行', '时', '泰', '盛', '雄', '琛', '钧', '冠', '策', '腾', '楠',
'榕', '风', '航', '弘', '秀', '娟', '英', '华', '慧', '巧', '美', '娜', '静', '淑', '惠', '珠', '翠', '雅', '芝', '玉', '萍',
'红', '娥', '玲', '芬', '芳', '燕', '彩', '春', '菊', '兰', '凤', '洁', '梅', '琳', '素', '云', '莲', '真', '环', '雪', '荣',
'爱', '妹', '霞', '香', '月', '莺', '媛', '艳', '瑞', '凡', '佳', '嘉', '琼', '勤', '珍', '贞', '莉', '桂', '娣', '叶', '璧',
'璐', '娅', '琦', '晶', '妍', '茜', '秋', '珊', '莎', '锦', '黛', '青', '倩', '婷', '姣', '婉', '娴', '瑾', '颖', '露', '瑶',
'怡', '婵', '雁', '蓓', '纨', '仪', '荷', '丹', '蓉', '眉', '君', '琴', '蕊', '薇', '菁', '梦', '岚', '苑', '婕', '馨', '瑗',
'琰', '韵', '融', '园', '艺', '咏', '卿', '聪', '澜', '纯', '毓', '悦', '昭', '冰', '爽', '琬', '茗', '羽', '希', '欣', '飘',
'育', '滢', '馥', '筠', '柔', '竹', '霭', '凝', '晓', '欢', '霄', '枫', '芸', '菲', '寒', '伊', '亚', '宜', '可', '姬', '舒',
'影', '荔', '枝', '丽', '阳', '妮', '宝', '贝', '初', '程', '梵', '罡', '恒', '鸿', '桦', '骅', '剑', '娇', '纪', '宽', '苛',
'灵', '玛', '媚', '琪', '晴', '容', '睿', '烁', '堂', '唯', '威', '韦', '雯', '苇', '萱', '阅', '彦', '宇', '雨', '洋', '忠',
'宗', '曼', '紫', '逸', '贤', '蝶', '菡', '绿', '蓝', '儿', '翠', '烟', '小', '轩'
);
$fam_count = count($fam_arr);
$name_count = count($name_arr);
$fam = $fam_arr[rand(0, $fam_count - 1)];
$name = $name_arr[rand(0, $name_count - 1)];
return "{$fam}*{$name}";
}
}
if (!function_exists('html_text')) {
/**
* 提取html文字
* @param $html
* @return mixed|string
*/
function html_text($html)
{
//把HTML实体标签转换为字符
$html_string = htmlspecialchars_decode($html);
//将空格回车替换成空
$content = str_replace(array(" ", "\n", "\r"), "", $html_string);
//函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
$content = strip_tags($content);
//去除转义字符
$pattern = "/&[^;]+;/";
$content = preg_replace($pattern, "", $content);
return $content;
}
}
if (!function_exists('formart_bank_number')) {
/**
* 处理银行卡号每四位一个空格
* @param $str
* @return string
*/
function formart_bank_number($str)
{
if (!$str) {
return $str;
}
//通过正则,每四位截取到数组中
preg_match('/([\d]{4})([\d]{4})([\d]{4})([\d]{4})([\d]{0,})?/', $str, $match);
unset($match[0]); //去除掉第一个键,因为第一个键是银行卡号的完整卡号
return implode(' ', $match);
}
}
if (!function_exists('convert_url_query')) {
/**
* 将字符串参数变为数组
* @param $query
* @return array array()
*/
function convert_url_query($query)
{
$queryParts = explode('&', $query);
$params = array();
foreach ($queryParts as $param) {
$item = explode('=', $param);
$params[$item[0]] = $item[1];
}
return $params;
}
}
if (!function_exists('create_contract_no')) {
/**
* 生成合同编号
* int $city_id
* int $id 合同id
* int $type 合同类型 (0整车合同 1代理协议 2确定信息 3交接信息)
*/
function create_contract_no($city_id = 350200, $id, $type = 0)
{
return $city_id . $type . $id . date('Ymd') . sprintf("%06d", rand(1, 999999));
}
}
if (!function_exists('getMonth')) {
function getMonth($season)
{
$times = array();
$firstday = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y')));
$sm = date('m', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y'))) + 1;
$seconday = date("Y-{$sm}-01");
$lastday = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date('Y'))), date('Y')));
return [$firstday, $seconday, $lastday];
}
}
if (!function_exists('myTrim')) {
/**
* Notes:过滤所有的空白字符(空格、全角空格、换行等)
* Created on: 2022/2/10 10:43
* Created by: dengbw
* @param $str
* @return string|string[]
*/
function myTrim($str)
{
if ($str) {
$search = array(" ", " ", "\n", "\r", "\t");
$replace = array("", "", "", "", "");
$str = str_replace($search, $replace, $str);
}
return $str;
}
}
/**
* 判断是否正式环境
*/
if (!function_exists('is_product')) {
function is_product()
{
if (false !== strpos($_SERVER['HTTP_HOST'], 'dev') || false !== strpos($_SERVER['HTTP_HOST'], 'test')) { //dev 测试
return false;
} else { // 正式
return true;
}
}
}
/**
* 判断文件是否图片
*/
if (!function_exists('is_img')) {
function is_img($file_name)
{
$file_name_arr = explode('.', $file_name);
$ext = end($file_name_arr);
$filetype = ['jpg', 'jpeg', 'gif', 'bmp', 'png'];
if (in_array($ext, $filetype)) {
return true;
} else {
return false;
}
}
}