6
0
Fork 0

获取/设置应用设置

release
李静 2021-12-25 17:05:46 +08:00
parent 48ed0bbc6a
commit 26fe2ecd73
4 changed files with 146 additions and 14 deletions

View File

@ -11,6 +11,11 @@ class Setting extends Model
use HasFactory;
use HasDateTimeFormatter;
protected $casts = [
'key' => 'string',
'value' => 'json',
];
protected $fillable = [
'key', 'value', 'remarks',
];

View File

@ -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']);
});
}
}

View File

@ -0,0 +1,113 @@
<?php
namespace App\Services;
use App\Models\Setting;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class SettingService
{
/**
* @var array
*/
protected $items = [];
/**
* @var integer
*/
protected $ttl = 1800;
/**
* @param \Illuminate\Contracts\Cache\Repository $cache
*/
public function __construct(
protected Cache $cache,
) {
}
/**
* @param array|string $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null)
{
if (is_array($key)) {
return $this->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}";
}
}

View File

@ -1,27 +1,27 @@
<?php
use App\Models\Setting;
use App\Services\SettingService;
if (! function_exists('app_settings')) {
/**
* 获取/设置应用设置项
*
* @param array|string $key
* @param mixed $default
* @return mixed
*
* @return mixed|\App\Services\SettingService
*/
function app_settings($key = null, $default = null)
{
// app('config')->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);
}
}