停车场管理系统
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.
 
 

125 lines
3.2 KiB

<?php
if (!function_exists('generate_tree')) {
function generate_tree(
array $flatArray,
int $parentId = 0,
array $existsArr = []
): array {
$tree = [];
foreach ($flatArray as $item) {
if ($item['parent_id'] === $parentId) {
$node = $item;
if ($existsArr) {
$node['is_show'] = in_array($item['id'], $existsArr) ? 1
: 0;
}
$node['children'] = generate_tree(
$flatArray,
$item['id'],
$existsArr
);
$tree[] = $node;
}
}
return $tree;
}
}
if (!function_exists('get_datetime')) {
function get_datetime(string $type = 'datetime', int $times = 0): string
{
if (!$times) {
$times = time();
}
$format = match ($type) {
'date' => 'Y-m-d',
'datetime' => 'Y-m-d H:i:s',
'date_time' => 'Y-m-d H:i'
};
return date($format, $times);
}
}
if (!function_exists('get_select_data')) {
function get_select_data(
array $data = [],
bool $is_all = false,
string $str1 = 'label',
string $str2 = 'value'
): array {
$newData = [];
if ($is_all) {
$newData[] = [$str1 => __('admin.all'), $str2 => ''];
}
foreach ($data as $key => $value) {
$newData[] = [
$str1 => $value,
$str2 => $key
];
}
return $newData;
}
}
if (!function_exists('get_ratio')) {
function get_ratio($max, $min, $is_unit = true): string
{
$ratio = 0;
if ($max > 0 && $min > 0) {
$ratio = round(($min / $max) * 100, 2);
}
if ($is_unit) {
$ratio .= '%';
}
return $ratio;
}
}
if (!function_exists('get_time_difference')) {
function get_time_difference(string $targetTime): array
{
try {
$target = new DateTime($targetTime);
$now = new DateTime();
// 计算差值
$interval = $now->diff($target);
$totalDays = $interval->days;
$hours = $interval->h;
$minutes = $interval->i;
$seconds = $interval->s;
// 判断是过去还是未来
$isFuture = $now < $target;
return [
'days' => $totalDays,
'hours' => $hours,
'minutes' => $minutes,
'seconds' => $seconds,
'is_future' => $isFuture,
'formatted' => sprintf(
"%s%d天 %02d时 %02d分 %02d秒",
$isFuture ? '' : '-',
$totalDays,
$hours,
$minutes,
$seconds
)
];
} catch (Exception $e) {
return [
'error' => '时间格式错误: ' . $e->getMessage()
];
}
}
}
if (!function_exists('get_image_url')) {
function get_image_url(string $image_url): string
{
return $image_url ? env('APP_URL') . $image_url : '';
}
}