modified setting

This commit is contained in:
Edward Yang 2022-07-12 20:32:53 +08:00
parent ecb09c87cb
commit 860f718459
3 changed files with 55 additions and 16 deletions

View File

@ -19,6 +19,32 @@ function setting($key, $default = null)
return config("bk.{$key}", $default);
}
/**
* 获取系统 settings
*
* @param $group
* @param $key
* @param null $default
* @return mixed
*/
function system_setting($group, $key, $default = null)
{
return config("bk.{$group}.{$key}", $default);
}
/**
* 获取后台设置到 settings 表的值
*
* @param $plugin
* @param $key
* @param null $default
* @return mixed
*/
function plugin_setting($plugin, $key, $default = null)
{
return config("bk.{$plugin}.{$key}", $default);
}
/**
* 获取后台管理前缀名称, 默认为 admin
*/
@ -37,22 +63,7 @@ function admin_name(): string
*/
function load_settings()
{
$settings = Setting::all(['type', 'space', 'name', 'value', 'json'])
->groupBy('space');
$result = [];
foreach ($settings as $space => $groupSettings) {
$space = $space ?: 'system';
foreach ($groupSettings as $groupSetting) {
$name = $groupSetting->name;
$value = $groupSetting->value;
if ($groupSetting->json) {
$result[$space][$name] = json_decode($value, true);
} else {
$result[$space][$name] = $value;
}
}
}
$result = \Beike\Repositories\SettingRepo::getGroupedSettings();
config(['bk' => $result]);
}

View File

@ -9,6 +9,8 @@ class Setting extends Model
{
use HasFactory;
const TYPES = ['system', 'plugin'];
protected $table = 'settings';
protected $fillable = ['type', 'space', 'name', 'value', 'json'];
}

View File

@ -16,6 +16,32 @@ use Beike\Models\Setting;
class SettingRepo
{
/**
* 按照类型分组获取设置
*/
public static function getGroupedSettings(): array
{
$settings = Setting::all(['type', 'space', 'name', 'value', 'json']);
$result = [];
foreach ($settings as $setting) {
$type = $setting->type;
if (!in_array($type, Setting::TYPES)) {
continue;
}
$space = $setting->space;
$name = $setting->name;
$value = $setting->value;
if ($setting->json) {
$result[$type][$space][$name] = json_decode($value, true);
} else {
$result[$type][$space][$name] = $value;
}
}
return $result;
}
/**
* 获取插件默认字段
*