lsky-pro/app/Utils.php

196 lines
5.7 KiB
PHP
Raw Normal View History

2021-12-15 22:44:49 +08:00
<?php
namespace App;
use App\Enums\ConfigKey;
use App\Models\Config;
2022-02-13 01:13:27 +08:00
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
2022-03-10 12:22:32 +08:00
use Illuminate\Support\Facades\Log;
2021-12-15 22:44:49 +08:00
class Utils
{
2022-03-10 12:22:32 +08:00
public static function e(\Throwable $e, $message = '', $level = 'error')
{
Log::{$level}($message, [
'file' => $e->getFile(),
'line' => $e->getLine(),
'message' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
}
2022-02-11 13:48:22 +08:00
/**
* 获取头像地址
*
* @param $email
* @param int $s
* @param string $d
* @param string $r
* @return string
*/
public static function getAvatar($email, int $s = 96, string $d = 'mp', string $r = 'g'): string
{
2022-04-24 08:07:58 +08:00
$url = 'https://cravatar.cn/avatar/';
2022-02-21 08:10:56 +08:00
$url .= md5(strtolower(trim($email)));
$url .= "?s=$s&d=$d&r=$r";
2022-02-11 13:48:22 +08:00
return $url;
}
/**
* 获取系统配置,获取全部配置时将返回
*
2022-01-23 15:48:45 +08:00
* @param string $name
* @param mixed|null $default
*
* @return mixed
*/
public static function config(string $name = '', mixed $default = null): mixed
2021-12-15 22:44:49 +08:00
{
/** @var Collection $configs */
2021-12-17 23:39:22 +08:00
$configs = Cache::rememberForever('configs', function () {
return Config::query()->pluck('value', 'name')->transform(function ($value, $key) {
switch ($key) {
case ConfigKey::IsAllowGuestUpload:
case ConfigKey::IsEnableGallery:
2022-02-14 14:04:19 +08:00
case ConfigKey::IsEnableApi:
case ConfigKey::IsEnableRegistration:
2022-01-14 14:28:58 +08:00
case ConfigKey::IsUserNeedVerify:
$value = (bool) $value;
break;
2022-02-14 11:36:19 +08:00
case ConfigKey::Mail:
case ConfigKey::Group:
$value = collect(json_decode($value, true));
break;
case ConfigKey::UserInitialCapacity:
$value = sprintf('%.2f', $value);
break;
default:
}
return $value;
});
});
return '' === $name ? $configs : $configs->get($name, $default);
2021-12-15 22:44:49 +08:00
}
2021-12-18 16:12:58 +08:00
2022-02-13 01:13:27 +08:00
/**
* 生成连续日期.
* @param string $start 开始日期
* @param string $end 结束日期
* @param string $unit day=month=year=
* @return array
*/
public static function makeDateRange(string $start, string $end, string $unit = 'day'): array
{
$array = [];
$format = ['day' => 'Y-m-d', 'month' => 'Y-m', 'year' => 'Y'][$unit] ?? 'Y-m-d';
Carbon::create($start)->range($end, 1, $unit)->forEach(function (Carbon $item) use (&$array, $format) {
$array[] = $item->format($format);
});
return $array;
}
2021-12-18 16:12:58 +08:00
/**
* 转换字段单位
*
2022-01-23 15:48:45 +08:00
* @param int|float $size 字节b
2021-12-18 16:12:58 +08:00
* @return string
*/
public static function formatSize(int|float $size): string
{
if ($size <= 0) {
return "0.00 Bytes";
}
$unit = ['', 'K', 'M', 'G', 'T', 'P'];
$base = 1024;
$i = floor(log($size, $base));
$n = count($unit);
if ($i >= $n) {
$i = $n - 1;
}
2022-01-23 15:48:45 +08:00
return sprintf("%.2f", $size / pow($base, $i)).' '.$unit[$i].'B';
}
2022-02-17 11:49:47 +08:00
/**
* 格式化数字
*
* @param int|string $n 数字
* @param int $precision 精度
2022-03-13 03:19:00 +08:00
* @return int|string
2022-02-17 11:49:47 +08:00
*/
2022-03-13 03:15:39 +08:00
public static function shortenNumber(int|string $n, int $precision = 1): int|string
2022-02-17 11:49:47 +08:00
{
if ($n < 1e+3) {
return number_format($n);
} else if ($n < 1e+6) {
return number_format($n / 1e+3, $precision) . 'k';
} else if ($n < 1e+9) {
return number_format($n / 1e+6, $precision) . 'm';
} else if ($n < 1e+12) {
return number_format($n / 1e+9, $precision) . 'b';
}
return $n;
}
2022-01-23 15:48:45 +08:00
/**
* 递归过滤数组元素
*
* @param array $array
* @param callable|null $callback
* @param int $mode
* @return array
*/
public static function filter(array $array, callable $callback = null, int $mode = 0): array
{
foreach ($array as &$value) {
if (is_array($value)) {
2022-05-07 09:56:55 +08:00
$value = self::filter($value, $callback, $mode);
2022-01-23 15:48:45 +08:00
}
}
return array_filter($array, $callback, $mode);
2021-12-18 16:12:58 +08:00
}
2022-01-24 15:55:06 +08:00
/**
* 格式化配置,设置默认配置以及将字符串数字转换为数字
*
2022-02-11 13:48:22 +08:00
* @param array $defaults 默认配置
* @param array $configs 新配置
2022-01-24 15:55:06 +08:00
* @return array
*/
public static function parseConfigs(array $defaults, array $configs): array
{
2022-03-06 17:25:06 +08:00
array_walk_recursive($configs, function (&$item) {
2022-01-24 15:55:06 +08:00
if (ctype_digit($item)) {
$item += 0;
}
2022-03-07 11:19:12 +08:00
if (is_null($item)) {
unset($item);
}
2022-01-24 15:55:06 +08:00
});
2022-03-06 17:25:06 +08:00
return self::array_merge_recursive_distinct($defaults, $configs);
2022-01-24 15:55:06 +08:00
}
2022-03-06 17:22:19 +08:00
/**
* @param array<int|string, mixed> $array1
* @param array<int|string, mixed> $array2
*
* @return array<int|string, mixed>
*/
private static function array_merge_recursive_distinct(array $array1, array &$array2): array
{
$merged = $array1;
foreach ($array2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key]) && ! array_is_list($value)) {
$merged[$key] = self::array_merge_recursive_distinct($merged[$key], $value);
} else {
$merged[$key] = $value;
}
}
return $merged;
}
2021-12-15 22:44:49 +08:00
}