2021-12-15 22:44:49 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2021-12-16 11:26:09 +08:00
|
|
|
use App\Enums\ConfigKey;
|
2021-12-16 11:02:33 +08:00
|
|
|
use App\Models\Config;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
2021-12-15 22:44:49 +08:00
|
|
|
class Utils
|
|
|
|
{
|
2021-12-16 11:02:33 +08:00
|
|
|
/**
|
2021-12-16 11:26:09 +08:00
|
|
|
* 获取系统配置,获取全部配置时将返回
|
2021-12-16 11:02:33 +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
|
|
|
{
|
2021-12-16 11:02:33 +08:00
|
|
|
/** @var Collection $configs */
|
|
|
|
$configs = Cache::remember('configs', 86400, function () {
|
2021-12-16 11:26:09 +08:00
|
|
|
return Config::query()->pluck('value', 'name')->transform(function ($value, $key) {
|
|
|
|
switch ($key) {
|
|
|
|
case ConfigKey::IsAllowGuestUpload:
|
|
|
|
case ConfigKey::IsEnableGallery:
|
|
|
|
case ConfigKey::IsEnableRegistration:
|
|
|
|
$value = (bool) $value;
|
|
|
|
break;
|
|
|
|
case ConfigKey::MailConfigs:
|
2021-12-17 14:28:16 +08:00
|
|
|
case ConfigKey::GuestGroupConfigs:
|
2021-12-16 11:26:09 +08:00
|
|
|
$value = collect(json_decode($value, true));
|
|
|
|
break;
|
|
|
|
case ConfigKey::UserInitialCapacity:
|
|
|
|
$value = sprintf('%.2f', $value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return $value;
|
2021-12-16 11:02:33 +08:00
|
|
|
});
|
|
|
|
});
|
2021-12-16 11:26:09 +08:00
|
|
|
return '' === $name ? $configs : $configs->get($name, $default);
|
2021-12-15 22:44:49 +08:00
|
|
|
}
|
|
|
|
}
|