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

248 lines
6.6 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',
'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_select_child_data')) {
function get_select_child_data(
array $data = [],
bool $is_all = false,
string $str1 = 'label',
string $str2 = 'value'
): array {
$newData = [];
if ($is_all) {
$newData[] = [$str1 => __('admin.all'), $str2 => ''];
}
$child = $data['children'] ?? [];
foreach ($data as $key => $value) {
if ($key == 'children') {
continue;
}
$arr = [
$str1 => $value,
$str2 => $key
];
if (isset($child[$key])) {
foreach ($child[$key] as $childKey => $childValue) {
$arr['children'][] = [
$str1 => $childValue,
$str2 => $childKey
];
}
}
$newData[] = $arr;
}
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_time_difference_str')) {
function get_time_difference_str($date): string
{
$value = '';
$difference = get_time_difference($date);
if (!isset($difference['error'])) {
if (!empty($difference['days'])) {
$value .= $difference['days'] . __('controller.parking_space.days');
}
if (!empty($difference['hours']) || !empty($value)) {
$value .= $difference['hours'] . __('controller.parking_space.hours');
}
if (!empty($difference['minutes']) || !empty($value)) {
$value .= $difference['minutes'] . __('controller.parking_space.minutes');
}
if (!empty($difference['seconds']) || !empty($value)) {
$value .= $difference['seconds'] . __('controller.parking_space.seconds');
}
}
return $value;
}
}
if (!function_exists('get_image_url')) {
function get_image_url($image_url): string
{
return $image_url ? env('APP_URL') . $image_url : '';
}
}
if (!function_exists('set_space_underline')) {
function set_space_underline($str): string
{
return str_replace(' ', '_', $str);
}
}
// 返回年月所有天
if (!function_exists('get_year_month_days')) {
function get_year_month_days($year_month): array
{
$year_month_times = strtotime($year_month);
if (!$year_month_times) {
return [];
}
$monthTimes = strtotime('+1 month', $year_month_times);
$monthEndDay = date('d', strtotime('-1 day', $monthTimes));
$dayArr = [];
for ($i = 1; $i <= $monthEndDay; $i++) {
$dayArr[] = strlen($i) > 1 ? $i : '0' . $i;
}
return $dayArr;
}
}
if (!function_exists('option_time')) {
function option_time($time): array
{
$str = '';
if ($time) {
$str = 'AM';
$end_str = 'PM';
$arr = explode(':', $time);
if ($arr[0] > 12) {
$time = ($arr[0] - 12) . ':' . $arr[1];
$str = $end_str;
}
}
return [
'time' => $time,
'str' => $str
];
}
}
if (!function_exists('__validation')) {
function __validation($str): string
{
return __('validation.' . $str);
}
}
if (!function_exists('__service')) {
function __service($str): string
{
return __('service.' . $str);
}
}
if (!function_exists('__exports')) {
function __exports($str): string
{
return __('exports.' . $str);
}
}