You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

600 lines
17 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/**
* Created by songweizong.
* Date: 2017/10/7
* Time: 11:15
*/
/**
* 数组以键值为索引
* @param $array
* @param $k
* @param bool|false $multi_values
* @return array
*/
function array_by_key($array, $k, $multi_values = false)
{
$result = [];
foreach ($array as $item) {
if ($multi_values) {
$result[$item[$k]][] = $item;
} else {
$result[$item[$k]] = $item;
}
}
return $result;
}
/**
* 数组转tree
* @param $array
* @param $key
* @param $parent_key
* @param $val
* @return array
*/
function array2tree($array, $key = "id", $parent_key = "pid", $val = "0", $level = 0)
{
$return = array();
foreach ($array as $item) {
!isset($item["title"]) && isset($item["name"]) ? $item["title"] = $item["name"] : "";
if ($item[$parent_key] == $val) {
$item["level"] = $level;
$item["children"] = array2tree($array, $key, $parent_key, $item[$key], $level + 1);
if (count($item["children"]) > 0 && !isset($item["folder"])) {
$item["folder"] = true;
}
$return[] = $item;
}
}
return $return;
}
/**
* tree 转一维数组
* @param $array
* @return array
*/
function flatten_tree($array, $key = "children")
{
$result = [];
foreach ($array as $item) {
$result[] = $item;
if (count($item[$key]) > 0) {
$result = array_merge($result, flatten_tree($item[$key], $key));
}
}
return $result;
}
/**
* 根据pid找寻所有的上层节点
* @param $array
* @param $val
* @param string $parent_key
* @param array $result
* @return array
*/
function get_pid($array, $val, $parent_key = "pid", $result = [])
{
$array = array_by_key($array, "id");
if (!isset($array[$val])) {
return $result;
}
if ($array[$val][$parent_key] == 0 || !isset($array[$array[$val][$parent_key]])) {
return $result;
} else {
$result[] = $array[$val][$parent_key];
return get_pid($array, $array[$val][$parent_key], $parent_key, $result);
}
}
/**
* 根据平行数组递归查找子节点
* @param $array
* @param $key
* @param $parent_key
* @param $val
* @param array $return
* @return array
*/
function find_children($array, $key, $parent_key, $val, $return = [])
{
foreach ($array as $item) {
if ($item[$parent_key] == $val) {
$return[] = $item;
$return = find_children($array, $key, $parent_key, $item[$key], $return);
}
}
return $return;
}
/**
* 18位身份证转年龄
* @param $IDnumber
* @return bool|string
*/
function get_age_by_chinese_id($IDnumber)
{
$year = (int)substr($IDnumber, 6, 4);
return date("Y") - $year;
}
/**
*
* @param $num
* @param $lower
* @return float|int|mixed|string
*/
function number2chinese($num, $lower = false)
{
$c1 = "零壹贰叁肆伍陆柒捌玖";
$c2 = "分角元拾佰仟万拾佰仟亿";
if ($lower) {
$c1 = "零一二三四五六七八九";
$c2 = "分角元十佰仟万拾佰仟亿";
}
$num = round($num, 2);
$num = $num * 100;
if (strlen($num) > 11) {
return $num;
}
$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) {
$m = substr($c, $j, 6);
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;
}
if (substr($c, strlen($c) - 3, 3) == '零') {
$c = substr($c, 0, strlen($c) - 3);
}
if (empty($c)) {
return "";
} else {
return str_replace("", "", $c);
}
}
/**
* 友好的时间显示
*
* @param int $sTime 待显示的时间
* @param string $type 类型. normal | mohu | full | ymd | other
* @param string $alt 已失效
* @return string
*/
function friendly_date($sTime, $type = 'normal', $alt = 'false')
{
if (!$sTime)
return '';
//sTime=源时间cTime=当前时间dTime=时间差
$sTime = strtotime($sTime);
$cTime = time();
$dTime = $cTime - $sTime;
$after_or_before = "";
$dDay = intval(date("z", $cTime)) - intval(date("z", $sTime));
$dYear = intval(date("Y", $cTime)) - intval(date("Y", $sTime));
if ($sTime > $cTime) {
$dTime = $sTime - $cTime;
$after_or_before = "";
$dDay = intval(date("z", $sTime)) - intval(date("z", $cTime));
$dYear = intval(date("Y", $sTime)) - intval(date("Y", $cTime));
}
//normaln秒前n分钟前n小时前日期
if ($type == 'normal') {
if ($dTime < 60) {
if ($dTime < 10) {
return '刚刚'; //by yangjs
} else {
return intval(floor($dTime / 10) * 10) . "{$after_or_before}";
}
} elseif ($dTime < 3600) {
return intval($dTime / 60) . "分钟{$after_or_before}";
//今天的数据.年份相同.日期相同.
} elseif ($dYear == 0 && $dDay == 0) {
//return intval($dTime/3600)."小时前";
return '今天' . date('H:i', $sTime);
} elseif ($dYear == 0) {
return date("m月d日 H:i", $sTime);
} else {
return date("Y-m-d H:i", $sTime);
}
} elseif ($type == 'mohu') {
if ($dTime < 60) {
return $dTime . "{$after_or_before}";
} elseif ($dTime < 3600) {
return intval($dTime / 60) . "分钟{$after_or_before}";
} elseif ($dTime >= 3600 && $dDay == 0) {
return intval($dTime / 3600) . "小时{$after_or_before}";
} elseif ($dDay > 0 && $dDay <= 7) {
return intval($dDay) . "{$after_or_before}";
} elseif ($dDay > 7 && $dDay <= 30) {
return intval($dDay / 7) . "{$after_or_before}";
} elseif ($dDay > 30) {
return intval($dDay / 30) . "个月{$after_or_before}";
}
//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 . "{$after_or_before}";
} elseif ($dTime < 3600) {
return intval($dTime / 60) . "分钟{$after_or_before}";
} elseif ($dTime >= 3600 && $dDay == 0) {
return intval($dTime / 3600) . "小时{$after_or_before}";
} elseif ($dYear == 0) {
return date("Y-m-d H:i:s", $sTime);
} else {
return date("Y-m-d H:i:s", $sTime);
}
}
}
/**
* 多行文本转为radio选项
* @param $text
* @param string $value
* @param bool $block
* @return string
*/
function multiline2radio($text, $value = "", $block = false)
{
$res = '<div>';
$text = explode("\r\n", $text);
$block = $block === true ? "d-block" : "";
for ($i = 0; $i < count($text); $i++) {
$checked = "";
if ($value === (string)$text[$i]) {
$checked = "checked";
}
if ((string)$text[$i] == "splitter") {
$res .= "<hr class='m-t-0 m-b-10'>";
} else {
$res .= '<div class="checkbox-color checkbox-default ' . $block . '"><input type="checkbox" ' . $checked . '><label for="">' . $text[$i] . '</label></div>';
}
}
$res .= "</div>";
return $res;
}
function multiline2html($text)
{
$html = str_replace(["\r\n", "\r", "\n"], "<br>", $text);
return $html;
}
/**
* 小时格式字符转秒数
* @param $hour
* @return mixed
*/
function hour2second($hour)
{
$hour = explode(":", $hour);
$second = $hour[0] * 3600 + $hour[1] * 60 + $hour[2];
return $second;
}
/**
* 秒数转小数格式
* @param $second
* @return mixed
*/
function second2hour($second)
{
$hours = floor($second / 3600);
$minutes = floor(($second - $hours * 3600) / 60);
$seconds = floor($second - $hours * 3600 - $minutes * 60);
return sprintf("%02s", $hours) . ":" . sprintf("%02s", $minutes) . ":" . sprintf("%02s", $seconds);
}
function curl($url, $post = array(), $arr_return = true)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
$return_str = curl_exec($curl);
curl_close($curl);
if ($arr_return) {
return json_decode($return_str, true);
} else {
return $return_str;
}
}
function is_wechat()
{
return strpos($_SERVER["HTTP_USER_AGENT"], "MicroMessenger");
}
function is_mobile()
{
// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) {
return TRUE;
}
// 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
if (isset ($_SERVER['HTTP_VIA'])) {
return stristr($_SERVER['HTTP_VIA'], "wap") ? TRUE : FALSE;// 找不到为flase,否则为TRUE
}
// 判断手机发送的客户端标志,兼容性有待提高
if (isset ($_SERVER['HTTP_USER_AGENT'])) {
$clientkeywords = array(
'mobile',
'nokia',
'sony',
'ericsson',
'mot',
'samsung',
'htc',
'sgh',
'lg',
'sharp',
'sie-',
'philips',
'panasonic',
'alcatel',
'lenovo',
'iphone',
'ipod',
'blackberry',
'meizu',
'android',
'netfront',
'symbian',
'ucweb',
'windowsce',
'palm',
'operamini',
'operamobi',
'openwave',
'nexusone',
'cldc',
'midp',
'wap'
);
// 从HTTP_USER_AGENT中查找手机浏览器的关键字
if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
return TRUE;
}
}
if (isset ($_SERVER['HTTP_ACCEPT'])) { // 协议法,因为有可能不准确,放到最后判断
// 如果只支持wml并且不支持html那一定是移动设备
// 如果支持wml和html但是wml在html之前则是移动设备
if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== FALSE) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === FALSE || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
return TRUE;
}
}
return FALSE;
}
function sms($to, $vars = [], $template_id, $belongs_type = null, $belongs_id = null)
{
$sms_app_id = env('SMS_APPID');
$sms_app_key = env('SMS_APPKEY');
is_array($vars) ? $vars = json_encode($vars) : '';
$data = "appid=" . $sms_app_id . "&signature=" . $sms_app_key . "&project=" . $template_id . "&vars=" . $vars . "&to=" . $to;
$url = "https://api.submail.cn/message/xsend.json";
$return = curl($url, $data);
$sms['mobile'] = $to;
$sms['vars'] = $vars;
$sms['template_id'] = $template_id;
$sms['ip'] = request()->getClientIp();
$sms['status'] = $return['status'];
$sms['belongs_type'] = $belongs_type;
$sms['belongs_id'] = $belongs_id;
if ($return['status'] != "success") {
$sms['result_code'] = $return['code'];
$sms['result_msg'] = $return['msg'];
}
$sms['created_at'] = date("Y-m-d H:i:s");
(new \App\Models\Sms())->insert($sms);
return $return['status'] == "success";
}
function get_http_type()
{
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
return $http_type;
}
function flash_message($msg, $warning = false)
{
session()->flash("flash_message", $msg);
}
function checkCondition($value1, $operator, $value2)
{
switch ($operator) {
case "eq":
return $value1 == $value2;
break;
case "neq":
return $value1 != $value2;
break;
default:
return false;
}
}
function excel_date_to_date($value)
{
$min_days = 25569;
$max_days = 99999;
if (is_numeric($value) && $value > $min_days && $value < $max_days) {
return gmdate('Y-m-d', ($value - $min_days) * 86400);
} else {
$value = str_replace([".", "", "/", "", ""], "-", $value);
$value = str_replace("", "", $value);
return $value;
}
}
/**
* 友好的时间显示
*
* @param int $sTime 待显示的时间
* @param string $type 类型. normal | mohu | full | ymd | other
* @param string $alt 已失效
* @return string
*/
function friendly_date2($sTime, $cTime = false, $type = 'mohu', $show_after_or_before = false)
{
if (!$sTime)
return '';
//sTime=源时间cTime=当前时间dTime=时间差
$sTime = strtotime($sTime);
$cTime ? $cTime = strtotime($cTime) : time();
$dTime = $cTime - $sTime;
$after_or_before = "";
$dDay = intval(date("z", $cTime)) - intval(date("z", $sTime));
$dYear = intval(date("Y", $cTime)) - intval(date("Y", $sTime));
if ($sTime > $cTime) {
$dTime = $sTime - $cTime;
$after_or_before = "";
$dDay = intval(date("z", $sTime)) - intval(date("z", $cTime));
$dYear = intval(date("Y", $sTime)) - intval(date("Y", $cTime));
}
$show_after_or_before ? "" : $after_or_before = "";
//normaln秒前n分钟前n小时前日期
if ($type == 'normal') {
if ($dTime < 60) {
if ($dTime < 10) {
return '刚刚'; //by yangjs
} else {
return intval(floor($dTime / 10) * 10) . "{$after_or_before}";
}
} elseif ($dTime < 3600) {
return intval($dTime / 60) . "分钟{$after_or_before}";
//今天的数据.年份相同.日期相同.
} elseif ($dYear == 0 && $dDay == 0) {
//return intval($dTime/3600)."小时前";
return '今天' . date('H:i', $sTime);
} elseif ($dYear == 0) {
return date("m月d日 H:i", $sTime);
} else {
return date("Y-m-d H:i", $sTime);
}
} elseif ($type == 'mohu') {
if ($dTime < 60) {
return $dTime . "{$after_or_before}";
} elseif ($dTime < 3600) {
return intval($dTime / 60) . "分钟{$after_or_before}";
} elseif ($dTime >= 3600 && $dDay == 0) {
return intval($dTime / 3600) . "小时{$after_or_before}";
} elseif ($dDay > 0 && $dDay <= 7) {
return intval($dDay) . "{$after_or_before}";
} elseif ($dDay > 7 && $dDay <= 30) {
return intval($dDay / 7) . "{$after_or_before}";
} elseif ($dDay > 30) {
return intval($dDay / 30) . "个月{$after_or_before}";
}
//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 . "{$after_or_before}";
} elseif ($dTime < 3600) {
return intval($dTime / 60) . "分钟{$after_or_before}";
} elseif ($dTime >= 3600 && $dDay == 0) {
return intval($dTime / 3600) . "小时{$after_or_before}";
} elseif ($dYear == 0) {
return date("Y-m-d H:i:s", $sTime);
} else {
return date("Y-m-d H:i:s", $sTime);
}
}
}
function is_work_day($date, $special_workday = [], $special_holiday = [])
{
if (gettype($date) != "integer") {
$date = strtotime($date);
}
foreach ($special_workday as &$item) {
$item = strtotime($item);
}
foreach ($special_holiday as &$item) {
$item = strtotime($item);
}
if (in_array($date, $special_workday)) {
return true;
}
if (in_array($date, $special_holiday)) {
return false;
}
return in_array(date("w", $date), [1, 2, 3, 4, 5]);
}
function add_work_day($date, $workdays, $return_timestamp = false)
{
$date = strtotime("+1 day", strtotime(date("Y-m-d", strtotime($date))));
$special_workday = (new \App\Models\Configs())->getConfig("SPECIAL_WORKDAY", true);
$special_holiday = (new \App\Models\Configs())->getConfig("SPECIAL_HOLIDAY", true);
while ($workdays > 0) {
$date = strtotime("+1 day", $date);
if (is_work_day($date, $special_workday, $special_holiday)) {
$workdays--;
}
}
return $return_timestamp ? $date : date("Y-m-d", $date);
}
function float_equals($a, $b, $epsilon = 0.00001)
{
return abs($a - $b) < $epsilon;
}