/**
* 系统加密方法
* @param string $data 要加密的字符串
* @param string $key 加密密钥
* @return string
* @author Forest <senlin6@126.com>
*/
function system_encrypt($data, $key = '') {
$key = md5(empty($key) ? config('common.admin_user_encrypt_string') : $key);
$salt = substr($key, 0, 6);
return md5($data.$salt);
}
/**
* 设置登录的token - 唯一性的
* @param string $phone
* @return string
*/
function setToken($phone = '') {
$str = md5(uniqid(md5(microtime(true)), true));
$str = sha1($str.$phone);
return $str;
}
/**
* 获取
* @param $model
* @param $field
*/
function getUniqueNum($model,$field,$len = 6,$string = false,$prefix = '')
{
do{
$randNum = makeRand($len,$string);
$randNum = $prefix ? $prefix.$randNum : $randNum;
}while ($model->where($field,$randNum)->find());
return $randNum;
}
/**
* 生成随机位数
* @param integer $len 长度
* @return string
*/
function makeRand($len = 6,$string = false)
{
if($string) {
$seed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
} else {
$seed = '0123456789';
}
return substr(str_shuffle(str_repeat($seed, $len)), 0, $len);
}
/**
* 生成随机数
* @param number $length
* @return number
*/
function generateNumber($length = 6){
return rand(pow(10, ($length - 1)), pow(10, $length) - 1);
}
/**
* 生成随机字符串
* @param number $length
* @param string $chars
* @return string
*/
function generateString($length = 6, $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')
{
$chars = str_split($chars);
$chars = array_map(function($i) use($chars) {
return $chars[$i];
}, array_rand($chars, $length));
return implode($chars);
}
/**
* 把返回的数据集转换成Tree
* @param array $list 要转换的数据集
* @param string $pid parent标记字段
* @param string $level level标记字段
* @return array
*/
function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) {
// 创建Tree
$tree = array();
if(is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$pk]] =& $list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] =& $list[$key];
}else{
if (isset($refer[$parentId])) {
$parent =& $refer[$parentId];
$parent[$child][] =& $list[$key];
}
}
}
}
return $tree;
}
/**
* @return float
* 获取毫秒
*/
function getMillisecond() {
list($t1, $t2) = explode(' ', microtime());
return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
}
/**
* 检查$pos(推荐位的值)是否包含指定推荐位$contain
* @param number $pos 推荐位的值
* @param number $contain 指定推荐位
* @return boolean true 包含 , false 不包含
* @author huajie <banhuajie@163.com>
*/
function check_document_position($pos = 0, $contain = 0){
if(empty($pos) || empty($contain)){
return false;
}
//将两个参数进行按位与运算,不为0则表示$contain属于$pos
$res = $pos & $contain;
if($res !== 0){
return true;
}else{
return false;
}
}
/**
* 保留小数点后两位
* @param $num
* @return float|int
*/
function generateDecimal($num,$scale = 2){
return number_format($num,$scale,'.','');
// return bcdiv(floor(bcmul($num,100)),100,$scale);
}
function geturl($url){
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2); // 跳过证书检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
$tmpInfo = curl_exec($curl); //返回api的json对象
//关闭URL请求
curl_close($curl);
return json_decode($tmpInfo,true);
}
function posturl($url,$data){
$data = json_encode($data);
$headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return json_decode($output,true);
}
/**
* curl请求指定url (post)
* @param $url
* @param array $data
* @return mixed
*/
function curlPost($url, $data = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* curl请求指定url (get)
* @param $url
* @param array $data
* @return mixed
*/
function curl($url, $data = [])
{
// 处理get数据
if (!empty($data)) {
$url = $url . '?' . http_build_query($data);
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
/**
* 根据出生日期计算年龄
* @param $birthday
* @return bool|int|mixed|string
*/
function birthday($birthday){
$age = strtotime($birthday);
if($age === false){
return false;
}
list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age));
$now = strtotime("now");
list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now));
$age = $y2 - $y1;
if((int)($m2.$d2) < (int)($m1.$d1))
$age -= 1;
return $age;
}
// 秒转时间字符串
function secondToStr($second){
$time = time()-$second;
if($time <= 0){
return '0秒前';
}
$d = floor($time / (3600*24));
$h = floor(($time % (3600*24)) / 3600);
$m = floor((($time % (3600*24)) % 3600) / 60);
$s = floor((($time % (3600*24)) % 3600) % 60);
if($d>'0'){
if($d<30){
return $d.'天前';
}else{
return date('Y-m-d H:i:s',$second);
}
}else{
if($h!='0'){
return $h.'小时前';
}elseif($m!='0'){
return $m.'分钟前';
}elseif($s!='0'){
return $s.'秒前';
}
}
}
/**
* 根据生日计算星座
*/
function constellation($birthday)
{
// 传入$birthday格式如:2018-05-06
$month = intval(substr($birthday, 5, 2));
$day = intval(substr($birthday, 8, 2));
if ($month < 1 || $month > 12 || $day < 1 || $day > 31)
{
return NULL;
}
$signs = array(
array('20' => '水瓶座'),
array('19' => '双鱼座'),
array('21' => '白羊座'),
array('20' => '金牛座'),
array('21' => '双子座'),
array('22' => '巨蟹座'),
array('23' => '狮子座'),
array('23' => '处女座'),
array('23' => '天秤座'),
array('24' => '天蝎座'),
array('22' => '射手座'),
array('22' => '摩羯座')
);
list($start, $name) = [key($signs[$month - 1]), current($signs[$month - 1])];
if ($day < $start)
{
$vo = $signs[($month - 2 < 0) ? 11 : $month - 2];
list($start, $name) = [key($vo), current($vo)];
}
return $name;
}
/**
* 计算两组经纬度坐标 之间的距离
* params :lat1 纬度1; lng1 经度1; lat2 纬度2; lng2 经度2; len_type (1:m or 2:km);
* return m or km
*/
function getDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
{
$pi = pi();
$earth_radius = 6378.137;//地球半径
$radLat1 = $lat1 * $pi / 180.0;
$radLat2 = $lat2 * $pi / 180.0;
$a = $radLat1 - $radLat2;
$b = ($lng1 * $pi / 180.0) - ($lng2 * $pi / 180.0);
$s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
$s = $s * $earth_radius;
$s = round($s * 1000);
if ($len_type > 1)
{
$s /= 1000;
}
return round($s, $decimal);
}
/**
* 求两个已知经纬度之间的距离,单位为m/km
* @param $lng1
* @param $lat1
* @param $lng2
* @param $lat2
* @return float|int
*/
function getDistance2($lng1, $lat1, $lng2, $lat2) {
// 将角度转为狐度
$radLat1 = deg2rad($lat1); //deg2rad()函数将角度转换为弧度
$radLat2 = deg2rad($lat2);
$radLng1 = deg2rad($lng1);
$radLng2 = deg2rad($lng2);
$a = $radLat1 - $radLat2;
$b = $radLng1 - $radLng2;
$s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
if($s<1000){
$s = bcdiv($s,1,0).'m';
}else if($s < 600000){
$s = bcdiv($s,1000,1).'km';
}else{
$s = '>600km';
}
return $s;
}
/**
* 隐藏敏感字符
* @param $value
* @return string
*/
function substr_cut($value)
{
$strlen = mb_strlen($value, 'utf-8');
if ($strlen <= 1) return $value;
$firstStr = mb_substr($value, 0, 1, 'utf-8');
$lastStr = mb_substr($value, -1, 1, 'utf-8');
return $strlen == 2 ? $firstStr . str_repeat('*', $strlen - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
}
/**
* 驼峰命名转下划线命名
* @param $str
* @return string
*/
function toUnderScore($str)
{
$dstr = preg_replace_callback('/([A-Z]+)/', function ($matchs) {
return '_' . strtolower($matchs[0]);
}, $str);
return trim(preg_replace('/_{2,}/', '_', $dstr), '_');
}
/**
* 多维数组合并
* @param $array1
* @param $array2
* @return array
*/
function array_merge_multiple($array1, $array2)
{
$merge = $array1 + $array2;
$data = [];
foreach ($merge as $key => $val) {
if (
isset($array1[$key])
&& is_array($array1[$key])
&& isset($array2[$key])
&& is_array($array2[$key])
) {
$data[$key] = array_merge_multiple($array1[$key], $array2[$key]);
} else {
$data[$key] = isset($array2[$key]) ? $array2[$key] : $array1[$key];
}
}
return $data;
}
/**
* 二维数组排序
* @param $arr
* @param $keys
* @param bool $desc
* @return mixed
*/
function array_sort($arr, $keys, $desc = false)
{
$key_value = $new_array = array();
foreach ($arr as $k => $v) {
$key_value[$k] = $v[$keys];
}
if ($desc) {
arsort($key_value);
} else {
asort($key_value);
}
reset($key_value);
foreach ($key_value as $k => $v) {
$new_array[$k] = $arr[$k];
}
return $new_array;
}
/**
* 过滤emoji表情
* @param $text
* @return null|string|string[]
*/
function filter_emoji($text)
{
// 此处的preg_replace用于过滤emoji表情
// 如需支持emoji表情, 需将mysql的编码改为utf8mb4
return preg_replace('/[\xf0-\xf7].{3}/', '', $text);
}
/**
* 获取全局唯一标识符
* @param bool $trim
* @return string
*/
function getGuidV4($trim = true)
{
// Windows
if (function_exists('com_create_guid') === true) {
$charid = com_create_guid();
return $trim == true ? trim($charid, '{}') : $charid;
}
// OSX/Linux
if (function_exists('openssl_random_pseudo_bytes') === true) {
$data = openssl_random_pseudo_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
// Fallback (PHP 4.2+)
mt_srand((double)microtime() * 10000);
$charid = strtolower(md5(uniqid(rand(), true)));
$hyphen = chr(45); // "-"
$lbrace = $trim ? "" : chr(123); // "{"
$rbrace = $trim ? "" : chr(125); // "}"
$guidv4 = $lbrace .
substr($charid, 0, 8) . $hyphen .
substr($charid, 8, 4) . $hyphen .
substr($charid, 12, 4) . $hyphen .
substr($charid, 16, 4) . $hyphen .
substr($charid, 20, 12) .
$rbrace;
return $guidv4;
}
/**
* json 转换true,false,数字转成vue可直接用的
*/
function jsonRecursive(&$array)
{
foreach ($array as $key => $value) {
if (is_array($value)) {
jsonRecursive($array[$key]);
} else {
if($value == 'true'){
$array[$key] = true;
} else if($value == 'false'){
$array[$key] = false;
} else if(is_numeric($value)){
if(is_int($value + 0)){
$array[$key] = intval($value);
}else if(is_float($value + 0)){
$array[$key] = floatval($value);
}
}
}
}
}
暂无评论