Files
orico-official-website/app/admin/controller/v1/SysConfig.php
2025-03-07 17:57:02 +08:00

174 lines
4.2 KiB
PHP

<?php
declare (strict_types = 1);
namespace app\admin\controller\v1;
use app\admin\model\v1\SysConfigGroupModel;
use app\admin\model\v1\SysConfigModel;
use app\admin\model\v1\SysConfigTypeModel;
use app\admin\validate\v1\SysConfigValidate;
/**
* 配置项控制器
*/
class SysConfig
{
// 获取分组列表
public function groups()
{
$groups = SysConfigGroupModel::field([
'id',
'name'
])
->language(request()->lang_id)
->enabled()
->order(['sort' => 'asc', 'id' => 'desc'])
->select();
return success('获取成功', $groups);
}
// 获取配置类型
public function types()
{
$types = SysConfigTypeModel::field([
'name',
'value'
])
->order(['sort' => 'asc'])
->select();
return success('获取成功', $types);
}
// 配置项分页
public function index()
{
$param = request()->param([
'title',
'page/d' => 1,
'size/d' => 10
]);
$configs = SysConfigModel::field([
'id',
'group_id',
'title',
'name',
'type',
'sort'
])
->with([
'group' => function($query) {
$query->field(['id', 'name' => 'group_name']);
},
'type' => function($query) {
$query->field(['name' => 'type_name', 'value']);
}
])
->withSearch(['title'], ['title' => $param['title']??null])
->order(['sort' => 'asc', 'id' => 'desc'])
->paginate([
'list_rows' => $param['size'],
'page' => $param['page']
])
->bindAttr('group', ['group_name'])
->bindAttr('type', ['type_name'])
->hidden(['group_id', 'group', 'type']);
return success('获取成功', $configs);
}
// 配置项详情
public function read()
{
$id = request()->param('id');
$config = SysConfigModel::withoutField([
'created_at',
'updated_at',
'deleted_at'
])
->bypy($id)
->find();
if (empty($config)) {
return error('配置项不存在');
}
return success('获取成功', $config);
}
// 配置项新增
public function save()
{
$post = request()->post([
'group_id',
'title',
'name',
'value',
'extra',
'type' => 'text',
'sort',
'remark'
]);
$validate = new SysConfigValidate;
if (!$validate->scene('add')->check($post)) {
return error($validate->getError());
}
$config = SysConfigModel::create($post);
if ($config->isEmpty()) {
return error('操作失败');
}
return success('操作成功');
}
// 配置项更新
public function update()
{
$id = request()->param('id');
$put = request()->put([
'group_id',
'title',
'name',
'value',
'extra',
'type' => 'text',
'sort',
'remark'
]);
$validate = new SysConfigValidate;
if (!$validate->check(array_merge($put, ['id' => $id]))) {
return error($validate->getError());
}
$config = SysConfigModel::bypk($id)->find();
if (empty($config)) {
return error('请确认要操作对象是否存在');
}
if (!$config->save($put)) {
return error('操作失败');
}
return success('操作成功');
}
// 配置项删除
public function delete()
{
$id = request()->param('id');
$config = SysConfigModel::bypk($id)->find();
if (empty($config)) {
return error('请确认要操作对象是否存在');
}
if (!$config->delete()) {
return error('操作失败');
}
return success('操作成功');
}
}