From 26fe2ecd731290e0f3321df4eef752c46b3e506e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Sat, 25 Dec 2021 17:05:46 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96/=E8=AE=BE=E7=BD=AE=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/Setting.php | 5 ++ app/Providers/AppServiceProvider.php | 14 ++++ app/Services/SettingService.php | 113 +++++++++++++++++++++++++++ app/helpers.php | 28 +++---- 4 files changed, 146 insertions(+), 14 deletions(-) create mode 100644 app/Services/SettingService.php diff --git a/app/Models/Setting.php b/app/Models/Setting.php index 742dc07c..6e238e2a 100644 --- a/app/Models/Setting.php +++ b/app/Models/Setting.php @@ -11,6 +11,11 @@ class Setting extends Model use HasFactory; use HasDateTimeFormatter; + protected $casts = [ + 'key' => 'string', + 'value' => 'json', + ]; + protected $fillable = [ 'key', 'value', 'remarks', ]; diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 087e3800..11ccc8f5 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use App\Services\SettingService; use EasyWeChat\Factory as EasyWeChatFactory; use EasyWeChat\Payment\Application as EasyWeChatPaymentApplication; use Illuminate\Database\Eloquent\Relations\Relation; @@ -22,6 +23,7 @@ class AppServiceProvider extends ServiceProvider $this->registerEasySms(); $this->registerEasyWeChat(); $this->registerRequestRealIp(); + $this->registerSettings(); } /** @@ -76,4 +78,16 @@ class AppServiceProvider extends ServiceProvider return $this->ip(); }); } + + /** + * 注册应用设置服务 + * + * @return void + */ + protected function registerSettings() + { + $this->app->singleton(SettingService::class, function ($app) { + return new SettingService($app['cache.store']); + }); + } } diff --git a/app/Services/SettingService.php b/app/Services/SettingService.php new file mode 100644 index 00000000..51c3874a --- /dev/null +++ b/app/Services/SettingService.php @@ -0,0 +1,113 @@ +getMany($key); + } + + if (! array_key_exists($key, $this->items)) { + try { + $this->items[$key] = $this->cache->remember($this->cacheKey($key), $this->ttl, function () use ($key) { + $settings = Setting::where('key', $key)->firstOrFail(); + + return $settings->value; + }); + } catch (ModelNotFoundException $e) { + return $default; + } + } + + return $this->items[$key]; + } + + /** + * @param array $keys + * @return array + */ + public function getMany(array $keys) + { + $settings = []; + + foreach ($keys as $key => $default) { + if (is_numeric($key)) { + [$key, $default] = [$default, null]; + } + + $settings[$key] = $this->get($key, $default); + } + + return $settings; + } + + /** + * @param array|string $key + * @param mixed $value + * @return void + */ + public function set($key, $value = null) + { + $keys = is_array($key) ? $key : [$key => $value]; + + foreach ($keys as $key => $value) { + Setting::updateOrCreate([ + 'key' => $key, + ], [ + 'value' => $value, + ]); + + $this->cleanCache($key); + } + } + + /** + * @param string $key + * @return void + */ + protected function cleanCache(string $key): void + { + unset($this->items[$key]); + + $this->cache->forget($this->cacheKey($key)); + } + + /** + * @param string $key + * @return string + */ + protected function cacheKey(string $key): string + { + return "settings.{$key}"; + } +} diff --git a/app/helpers.php b/app/helpers.php index a1d42f80..3ae4585c 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -1,27 +1,27 @@ set($key); - // dd(123456); if (is_null($key)) { - return $default; + return app(SettingService::class); } if (is_array($key)) { - foreach ($key as $i => $value) { - Setting::updateOrCreate([ - 'key' => $i, - ], [ - 'value' => $value, - ]); - } - return $key; + return app(SettingService::class)->set($key); } - return Setting::where('key', $key)->value('value', $default); + return app(SettingService::class)->get($key, $default); } }