feat(ConfigLogic): 添加配置表、编辑配置表、删除配置表、获取配置表详情的功能

This commit is contained in:
mkm 2024-06-24 17:57:51 +08:00
parent 97641a18ce
commit 751ddafe73
5 changed files with 330 additions and 7 deletions

View File

@ -14,6 +14,7 @@
namespace app\admin\controller;
use app\admin\lists\config\ConfigLists;
use app\admin\logic\auth\AuthLogic;
use app\admin\logic\ConfigLogic;
use think\facade\Db;

View File

@ -0,0 +1,95 @@
<?php
namespace app\admin\controller\config;
use app\admin\controller\BaseAdminController;
use app\admin\lists\config\ConfigLists;
use app\admin\logic\ConfigLogic;
use app\admin\validate\config\ConfigValidate;
/**
* 配置表控制器
* Class ConfigController
* @package app\admin\controller\config
*/
class ConfigController extends BaseAdminController
{
/**
* @notes 获取配置表列表
* @return \think\response\Json
* @author admin
* @date 2024/06/24 17:52
*/
public function lists()
{
return $this->dataLists(new ConfigLists());
}
/**
* @notes 添加配置表
* @return \think\response\Json
* @author admin
* @date 2024/06/24 17:52
*/
public function add()
{
$params = (new ConfigValidate())->post()->goCheck('add');
$result = ConfigLogic::add($params);
if (true === $result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(ConfigLogic::getError());
}
/**
* @notes 编辑配置表
* @return \think\response\Json
* @author admin
* @date 2024/06/24 17:52
*/
public function edit()
{
$params = (new ConfigValidate())->post()->goCheck('edit');
$result = ConfigLogic::edit($params);
if (true === $result) {
return $this->success('编辑成功', [], 1, 1);
}
return $this->fail(ConfigLogic::getError());
}
/**
* @notes 删除配置表
* @return \think\response\Json
* @author admin
* @date 2024/06/24 17:52
*/
public function delete()
{
$params = (new ConfigValidate())->post()->goCheck('delete');
ConfigLogic::delete($params);
return $this->success('删除成功', [], 1, 1);
}
/**
* @notes 获取配置表详情
* @return \think\response\Json
* @author admin
* @date 2024/06/24 17:52
*/
public function detail()
{
$params = (new ConfigValidate())->goCheck('detail');
$result = ConfigLogic::detail($params);
return $this->data($result);
}
}

View File

@ -0,0 +1,64 @@
<?php
namespace app\admin\lists\config;
use app\admin\lists\BaseAdminDataLists;
use app\common\model\app_update\AppUpdate;
use app\common\lists\ListsSearchInterface;
use app\common\model\Config;
/**
* 配置列表
* Class ConfigLists
* @package app\admin\lists\config
*/
class ConfigLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* @notes 设置搜索条件
* @return \string[][]
* @author likeadmin
* @date 2024/05/30 10:25
*/
public function setSearch(): array
{
return [
'=' => ['name', 'type'],
];
}
/**
* @notes 获取配置列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author likeadmin
* @date 2024/05/30 10:25
*/
public function lists(): array
{
return Config::where($this->searchWhere)
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()
->toArray();
}
/**
* @notes 获取配置数量
* @return int
* @author likeadmin
* @date 2024/05/30 10:25
*/
public function count(): int
{
return AppUpdate::where($this->searchWhere)->count();
}
}

View File

@ -14,17 +14,97 @@
namespace app\admin\logic;
use app\common\logic\BaseLogic;
use app\common\model\Config;
use app\common\model\dict\DictData;
use app\common\service\{FileService, ConfigService};
use think\facade\Db;
/**
* 配置类逻辑层
* Class ConfigLogic
* @package app\admin\logic
*/
class ConfigLogic
class ConfigLogic extends BaseLogic
{
/**
* @notes 添加配置表
* @param array $params
* @return bool
* @author admin
* @date 2024/06/24 17:52
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
Config::create([
'type' => $params['type'],
'name' => $params['name'],
'value' => $params['value']
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 编辑配置表
* @param array $params
* @return bool
* @author admin
* @date 2024/06/24 17:52
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
Config::where('id', $params['id'])->update([
'type' => $params['type'],
'name' => $params['name'],
'value' => $params['value']
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 删除配置表
* @param array $params
* @return bool
* @author admin
* @date 2024/06/24 17:52
*/
public static function delete(array $params): bool
{
return Config::destroy($params['id']);
}
/**
* @notes 获取配置表详情
* @param $params
* @return array
* @author admin
* @date 2024/06/24 17:52
*/
public static function detail($params): array
{
return Config::findOrEmpty($params['id'])->toArray();
}
/**
* @notes 获取配置
* @return array
@ -68,7 +148,7 @@ class ConfigLogic
if (!is_string($type)) {
return [];
}
$type = explode(',', $type);
$lists = DictData::whereIn('type_value', $type)->select()->toArray();
@ -86,7 +166,4 @@ class ConfigLogic
}
return $result;
}
}
}

View File

@ -0,0 +1,86 @@
<?php
namespace app\admin\validate\config;
use app\common\validate\BaseValidate;
/**
* 配置表验证器
* Class ConfigValidate
* @package app\admin\validate\config
*/
class ConfigValidate extends BaseValidate
{
/**
* 设置校验规则
* @var string[]
*/
protected $rule = [
'id' => 'require',
'name' => 'require',
'value' => 'require',
];
/**
* 参数描述
* @var string[]
*/
protected $field = [
'id' => 'id',
'name' => '名称',
'value' => '值',
];
/**
* @notes 添加场景
* @return ConfigValidate
* @author admin
* @date 2024/06/24 17:52
*/
public function sceneAdd()
{
return $this->only(['name','value']);
}
/**
* @notes 编辑场景
* @return ConfigValidate
* @author admin
* @date 2024/06/24 17:52
*/
public function sceneEdit()
{
return $this->only(['id','name','value']);
}
/**
* @notes 删除场景
* @return ConfigValidate
* @author admin
* @date 2024/06/24 17:52
*/
public function sceneDelete()
{
return $this->only(['id']);
}
/**
* @notes 详情场景
* @return ConfigValidate
* @author admin
* @date 2024/06/24 17:52
*/
public function sceneDetail()
{
return $this->only(['id']);
}
}