获取/设置应用设置
parent
48ed0bbc6a
commit
26fe2ecd73
|
|
@ -11,6 +11,11 @@ class Setting extends Model
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
use HasDateTimeFormatter;
|
use HasDateTimeFormatter;
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'key' => 'string',
|
||||||
|
'value' => 'json',
|
||||||
|
];
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'key', 'value', 'remarks',
|
'key', 'value', 'remarks',
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use App\Services\SettingService;
|
||||||
use EasyWeChat\Factory as EasyWeChatFactory;
|
use EasyWeChat\Factory as EasyWeChatFactory;
|
||||||
use EasyWeChat\Payment\Application as EasyWeChatPaymentApplication;
|
use EasyWeChat\Payment\Application as EasyWeChatPaymentApplication;
|
||||||
use Illuminate\Database\Eloquent\Relations\Relation;
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||||
|
|
@ -22,6 +23,7 @@ class AppServiceProvider extends ServiceProvider
|
||||||
$this->registerEasySms();
|
$this->registerEasySms();
|
||||||
$this->registerEasyWeChat();
|
$this->registerEasyWeChat();
|
||||||
$this->registerRequestRealIp();
|
$this->registerRequestRealIp();
|
||||||
|
$this->registerSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -76,4 +78,16 @@ class AppServiceProvider extends ServiceProvider
|
||||||
return $this->ip();
|
return $this->ip();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册应用设置服务
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function registerSettings()
|
||||||
|
{
|
||||||
|
$this->app->singleton(SettingService::class, function ($app) {
|
||||||
|
return new SettingService($app['cache.store']);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,27 +1,27 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Models\Setting;
|
use App\Services\SettingService;
|
||||||
|
|
||||||
if (!function_exists('app_settings')) {
|
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)
|
function app_settings($key = null, $default = null)
|
||||||
{
|
{
|
||||||
// app('config')->set($key);
|
|
||||||
// dd(123456);
|
|
||||||
if (is_null($key)) {
|
if (is_null($key)) {
|
||||||
return $default;
|
return app(SettingService::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_array($key)) {
|
if (is_array($key)) {
|
||||||
foreach ($key as $i => $value) {
|
return app(SettingService::class)->set($key);
|
||||||
Setting::updateOrCreate([
|
|
||||||
'key' => $i,
|
|
||||||
], [
|
|
||||||
'value' => $value,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
return $key;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Setting::where('key', $key)->value('value', $default);
|
return app(SettingService::class)->get($key, $default);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue