改进组设置

This commit is contained in:
Wisp X 2022-01-24 15:55:06 +08:00
parent 2ea34bcb77
commit ed91764fd5
3 changed files with 71 additions and 53 deletions

View File

@ -21,27 +21,9 @@ class GroupController extends Controller
public function __construct()
{
\Illuminate\Support\Facades\View::share([
'default' => collect(config('convention.app.'.ConfigKey::GroupConfigs)),
'positions' => [
'top-left' => '左上角',
'top' => '上中',
'top-right' => '右上角',
'left' => '左边',
'center' => '中间',
'right' => '右边',
'bottom-left' => '左下角',
'bottom' => '下中',
'bottom-right' => '右下角',
'tiled' => '平铺',
],
'scanAliyunScenes' => [
'porn' => '智能鉴黄',
'terrorism' => '暴恐涉政',
'ad' => '图文违规',
'qrcode' => '二维码',
'live' => '不良场景',
'logo' => 'Logo',
]
'default' => Group::getDefaultConfigs(),
'positions' => Group::POSITIONS,
'scanAliyunScenes' => Group::SCENES,
]);
}
@ -83,9 +65,12 @@ class GroupController extends Controller
public function update(GroupRequest $request): Response
{
if ($request->route('id') == 0) {
Config::query()->where('name', ConfigKey::GroupConfigs)->update([
'value' => collect(Group::parseConfigs($request->validated('configs'))),
]);
$configs = Utils::parseConfigs(Group::getDefaultConfigs()->toArray(), $request->validated('configs'));
if (! Config::query()->where('name', ConfigKey::GroupConfigs)->update([
'value' => collect($configs)->toJson(),
])) {
return $this->error('保存失败');
}
// 删除配置缓存
Cache::forget('configs');
} else {

View File

@ -4,7 +4,9 @@ namespace App\Models;
use App\Enums\ConfigKey;
use App\Utils;
use Barryvdh\Debugbar\Facades\Debugbar;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
@ -36,6 +38,45 @@ class Group extends Model
'configs' => 'collection'
];
const POSITIONS = [
'top-left' => '左上角',
'top' => '上中',
'top-right' => '右上角',
'left' => '左边',
'center' => '中间',
'right' => '右边',
'bottom-left' => '左下角',
'bottom' => '下中',
'bottom-right' => '右下角',
'tiled' => '平铺',
];
const SCENES = [
'porn' => '智能鉴黄',
'terrorism' => '暴恐涉政',
'ad' => '图文违规',
'qrcode' => '二维码',
'live' => '不良场景',
'logo' => 'Logo',
];
protected static function booted()
{
static::saving(function (self $group) {
$group->configs = Utils::parseConfigs(self::getDefaultConfigs()->toArray(), $group->configs->toArray());
});
}
/**
* 获取组默认配置
*
* @return Collection
*/
public static function getDefaultConfigs(): Collection
{
return collect(config('convention.app.'.ConfigKey::GroupConfigs));
}
/**
* 获取访客组默认配置
*
@ -46,35 +87,6 @@ class Group extends Model
return Utils::config(ConfigKey::GroupConfigs);
}
/**
* 格式化配置,设置默认配置以及将字符串数字转换为数字
*
* @param $configs
* @return array
*/
public static function parseConfigs($configs): array
{
if ($configs instanceof Collection) {
$configs = $configs->toArray();
}
$array = array_replace_recursive(config('convention.app.'.ConfigKey::GroupConfigs), Utils::filter($configs));
array_walk_recursive($array, function (&$item) {
if (ctype_digit($item)) {
$item += 0;
}
if (is_null($item)) {
unset($item);
}
});
return $array;
}
public function setConfigsAttribute($value)
{
$this->attributes['configs'] = json_encode(self::parseConfigs($value), JSON_UNESCAPED_UNICODE);
}
public function users(): HasMany
{
return $this->hasMany(User::class, 'group_id', 'id');

View File

@ -83,4 +83,25 @@ class Utils
}
return array_filter($array, $callback, $mode);
}
/**
* 格式化配置,设置默认配置以及将字符串数字转换为数字
*
* @param array $defaults 默认配置
* @param array $configs 新配置
* @return array
*/
public static function parseConfigs(array $defaults, array $configs): array
{
$array = array_replace_recursive($defaults, Utils::filter($configs));
array_walk_recursive($array, function (&$item) {
if (ctype_digit($item)) {
$item += 0;
}
if (is_null($item)) {
unset($item);
}
});
return $array;
}
}