From 9bd446e82261670ec0ceddf9f49648bedf9e8313 Mon Sep 17 00:00:00 2001 From: WispX Date: Thu, 16 Dec 2021 11:26:09 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E8=8E=B7=E5=8F=96=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=B9=E6=B3=95=EF=BC=8C=E7=BC=93=E5=AD=98=201=20?= =?UTF-8?q?=E5=A4=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Controller.php | 1 + app/Service/UploadService.php | 2 +- app/Utils.php | 23 +++++++++++++++++++---- tests/Unit/UtilTest.php | 9 +++++++-- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 1f4292d8..f7c51dec 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -19,6 +19,7 @@ class Controller extends BaseController public function upload(Request $request, UploadService $service): array { try { + $service->store($request); } catch (UploadException $e) { return $this->error($e->getMessage()); } catch (\Throwable $e) { diff --git a/app/Service/UploadService.php b/app/Service/UploadService.php index f90acbd5..b1c902be 100644 --- a/app/Service/UploadService.php +++ b/app/Service/UploadService.php @@ -8,7 +8,7 @@ use Illuminate\Http\Request; class UploadService { - public function store(Request $request, ?User $user)//: Image + public function store(Request $request, ?User $user = null)//: Image { // TODO 如果关闭了游客上传,返回404 // TODO 检测IP是否超出上传限制 diff --git a/app/Utils.php b/app/Utils.php index 0e2c9d3f..996bfd7f 100644 --- a/app/Utils.php +++ b/app/Utils.php @@ -2,6 +2,7 @@ namespace App; +use App\Enums\ConfigKey; use App\Models\Config; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Cache; @@ -9,7 +10,7 @@ use Illuminate\Support\Facades\Cache; class Utils { /** - * 获取系统配置 + * 获取系统配置,获取全部配置时将返回 * * @param string $name * @param mixed|null $default @@ -20,10 +21,24 @@ class Utils { /** @var Collection $configs */ $configs = Cache::remember('configs', 86400, function () { - return Config::query()->pluck('value', 'name')->transform(function ($value) { - return json_decode($value, true) ?: $value; + 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: + $value = collect(json_decode($value, true)); + break; + case ConfigKey::UserInitialCapacity: + $value = sprintf('%.2f', $value); + break; + default: + } + return $value; }); }); - return $configs->get($name, $default); + return '' === $name ? $configs : $configs->get($name, $default); } } diff --git a/tests/Unit/UtilTest.php b/tests/Unit/UtilTest.php index b0fab4e0..8868e0c8 100644 --- a/tests/Unit/UtilTest.php +++ b/tests/Unit/UtilTest.php @@ -4,6 +4,7 @@ namespace Tests\Unit; use App\Enums\ConfigKey; use App\Utils; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Cache; use Tests\TestCase; @@ -18,7 +19,7 @@ class UtilTest extends TestCase { Cache::forget('configs'); - if (is_array(Utils::config())) { + if (Utils::config() instanceof Collection) { $this->assertTrue(true); } @@ -26,12 +27,16 @@ class UtilTest extends TestCase $this->assertTrue(true); } - if (is_array(Utils::config(ConfigKey::MailConfigs))) { + if (Utils::config(ConfigKey::MailConfigs) instanceof Collection) { $this->assertTrue(true); } if (is_array(Utils::config(ConfigKey::MailConfigs.'.mailers'))) { $this->assertTrue(true); } + + if (is_bool(Utils::config(ConfigKey::IsAllowGuestUpload))) { + $this->assertTrue(true); + } } }