新增配置管理
This commit is contained in:
parent
be743d9d23
commit
1b954c1b16
65
catch/system/controller/Config.php
Normal file
65
catch/system/controller/Config.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
namespace catchAdmin\system\controller;
|
||||
|
||||
use app\Request;
|
||||
use catcher\base\CatchController;
|
||||
use catchAdmin\system\model\Config as ConfigModel;
|
||||
use catcher\CatchResponse;
|
||||
use think\response\Json;
|
||||
|
||||
class Config extends CatchController
|
||||
{
|
||||
protected $configModel;
|
||||
|
||||
public function __construct(ConfigModel $configModel)
|
||||
{
|
||||
$this->configModel = $configModel;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取父级别配置
|
||||
*
|
||||
* @time 2020年04月17日
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return Json
|
||||
*/
|
||||
public function parent()
|
||||
{
|
||||
return CatchResponse::success($this->configModel->getParentConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储配置
|
||||
*
|
||||
* @time 2020年04月17日
|
||||
* @param Request $request
|
||||
* @return Json
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
*/
|
||||
public function save(Request $request)
|
||||
{
|
||||
return CatchResponse::success([
|
||||
'id' => $this->configModel->storeBy($request->param()),
|
||||
'parents' => $this->configModel->getParentConfig(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置
|
||||
*
|
||||
* @time 2020年04月20日
|
||||
* @param $id
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return Json
|
||||
*/
|
||||
public function read($id)
|
||||
{
|
||||
return CatchResponse::success($this->configModel->getConfig($id));
|
||||
}
|
||||
}
|
44
catch/system/database/migrations/20200417083602_config.php
Normal file
44
catch/system/database/migrations/20200417083602_config.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Migrator;
|
||||
use think\migration\db\Column;
|
||||
|
||||
class Config extends Migrator
|
||||
{
|
||||
/**
|
||||
* Change Method.
|
||||
*
|
||||
* Write your reversible migrations using this method.
|
||||
*
|
||||
* More information on writing migrations is available here:
|
||||
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
|
||||
*
|
||||
* The following commands can be used in this method and Phinx will
|
||||
* automatically reverse them when rolling back:
|
||||
*
|
||||
* createTable
|
||||
* renameTable
|
||||
* addColumn
|
||||
* renameColumn
|
||||
* addIndex
|
||||
* addForeignKey
|
||||
*
|
||||
* Remember to call "create()" or "update()" and NOT "save()" when working
|
||||
* with the Table class.
|
||||
*/
|
||||
public function change()
|
||||
{
|
||||
$table = $this->table('config',['engine'=>'InnoDB', 'comment' => '配置管理', 'signed' => false]);
|
||||
$table->addColumn('name', 'string',['limit' => 50,'default'=>'','comment'=>'配置名称'])
|
||||
->addColumn('pid', 'integer', array('default'=> 0,'comment'=>'父级配置', 'signed' => false ))
|
||||
->addColumn('component', 'string', ['default'=> '', 'limit' => 100, 'comment'=>'tab 引入的组件名称'])
|
||||
->addColumn('key', 'string',['default'=> '', 'limit' => 100, 'comment'=>'配置键名'])
|
||||
->addColumn('value', 'string',['default'=> '', 'limit' => 255, 'comment'=>'配置键值'])
|
||||
->addColumn('status', 'integer',['limit' => \Phinx\Db\Adapter\MysqlAdapter::INT_TINY,'default'=> 1,'comment'=>'1 启用 2 禁用'])
|
||||
->addColumn('creator_id', 'integer', array('default'=> 0,'comment'=>'创建人', 'signed' => false ))
|
||||
->addColumn('created_at', 'integer', ['default'=> 0,'comment'=>'创建时间', 'signed' => false])
|
||||
->addColumn('updated_at', 'integer', ['default'=> 0,'comment'=>'更新时间', 'signed' => false])
|
||||
->addColumn('deleted_at', 'integer', ['default'=> 0,'comment'=>'删除时间', 'signed' => false])
|
||||
->create();
|
||||
}
|
||||
}
|
210
catch/system/model/Config.php
Normal file
210
catch/system/model/Config.php
Normal file
@ -0,0 +1,210 @@
|
||||
<?php
|
||||
namespace catchAdmin\system\model;
|
||||
|
||||
use catcher\base\CatchModel;
|
||||
use thans\jwt\exception\UserNotDefinedException;
|
||||
use think\Model;
|
||||
|
||||
class Config extends CatchModel
|
||||
{
|
||||
protected $name = 'config';
|
||||
|
||||
protected $pk = 'id';
|
||||
|
||||
protected $field = [
|
||||
'id', //
|
||||
'name', // 配置名称
|
||||
'pid', // 父级配置
|
||||
'key', // 配置键名
|
||||
'value', // 配置键值
|
||||
'component', // 组件
|
||||
'status', // 1 启用 2 禁用
|
||||
'creator_id', // 创建人
|
||||
'created_at', // 创建时间
|
||||
'updated_at', // 更新时间
|
||||
'deleted_at', // 删除时间
|
||||
];
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2020年04月17日
|
||||
* @return \think\Collection
|
||||
*@throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
*/
|
||||
public function getParentConfig()
|
||||
{
|
||||
return $this->where('pid', 0)
|
||||
->field(['id', 'name', 'component'])->select();
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储配置
|
||||
*
|
||||
* @time 2020年04月20日
|
||||
* @param array $data
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return bool
|
||||
*/
|
||||
public function storeBy(array $data)
|
||||
{
|
||||
if (empty($data)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 子配置
|
||||
if ($data['pid'] ?? false) {
|
||||
$config = \json_decode($data['config'], true);
|
||||
$pid = $data['pid'];
|
||||
unset($data['pid']);
|
||||
/**[
|
||||
'key' => [
|
||||
'k' => 'v'
|
||||
],
|
||||
|
||||
'k' => 'v'
|
||||
]*/
|
||||
foreach ($config as $key => $value) {
|
||||
if (empty($value)) {
|
||||
continue;
|
||||
}
|
||||
// 如果二级配置存在
|
||||
$secondLevel = $this->isExistConfig($key, $pid);
|
||||
if ($secondLevel) {
|
||||
// value 是字符串
|
||||
if (!is_array($value)) {
|
||||
if ($value != $secondLevel->value) {
|
||||
$secondLevel->value = $value;
|
||||
$secondLevel->save();
|
||||
}
|
||||
} else {
|
||||
// 数组
|
||||
$thirdLevel = [];
|
||||
$this->subConfig($secondLevel->id, ['id', 'key', 'value'])
|
||||
->each(function ($item, $key) use (&$thirdLevel){
|
||||
$thirdLevel[$item['key']] = $item;
|
||||
});
|
||||
|
||||
if (!empty($value)) {
|
||||
$new = [];
|
||||
foreach ($value as $k => $v) {
|
||||
if (isset($thirdLevel[$k])) {
|
||||
if ($v != $thirdLevel[$k]->value) {
|
||||
$thirdLevel[$k]->value = $v;
|
||||
$thirdLevel[$k]->save();
|
||||
}
|
||||
} else {
|
||||
$new[] = [
|
||||
'pid' => $secondLevel->id,
|
||||
'key' => $k,
|
||||
'value' => $v,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($new)) {
|
||||
parent::insertAllBy($new);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!is_array($value)) {
|
||||
parent::storeBy([
|
||||
'pid' => $pid,
|
||||
'key' => $key,
|
||||
'value' => $value,
|
||||
]);
|
||||
} else {
|
||||
$id = parent::storeBy([
|
||||
'pid' => $pid,
|
||||
'key' => $key,
|
||||
]);
|
||||
if (!empty($value)) {
|
||||
$newConfig = [];
|
||||
foreach ($value as $k => $v) {
|
||||
$newConfig[] = [
|
||||
'key' => $k,
|
||||
'value' => $v,
|
||||
'pid' => $id,
|
||||
];
|
||||
}
|
||||
parent::insertAllBy($newConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return parent::storeBy($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置是否存在
|
||||
*
|
||||
* @time 2020年04月19日
|
||||
* @param $key
|
||||
* @param int $pid
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return array|Model|null
|
||||
*/
|
||||
public function isExistConfig($key, $pid = 0)
|
||||
{
|
||||
return $this->where('pid', $pid)
|
||||
->where('key', $key)
|
||||
->find();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取子配置
|
||||
*
|
||||
* @time 2020年04月19日
|
||||
* @param int $pid
|
||||
* @param array $field
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return \think\Collection
|
||||
*/
|
||||
public function subConfig($pid = 0, array $field = ['*'])
|
||||
{
|
||||
return $this->where('pid', $pid)
|
||||
->field($field)
|
||||
->select();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置
|
||||
*
|
||||
* @time 2020年04月20日
|
||||
* @param int $pid
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return array|mixed
|
||||
*/
|
||||
public function getConfig($pid = 0)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
$configs = $this->where('pid', $pid)
|
||||
->field('id,`key` as k,value,pid')
|
||||
->select();
|
||||
|
||||
foreach ($configs as $config) {
|
||||
if ($config->value) {
|
||||
$data[$config->k] = $config->value;
|
||||
} else {
|
||||
$data[$config->k] = $this->getConfig($config->id);
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
@ -18,3 +18,7 @@ $router->post('upload/file', '\catchAdmin\system\controller\Upload@file');
|
||||
|
||||
// 附件
|
||||
$router->resource('attachments', '\catchAdmin\system\controller\Attachments');
|
||||
|
||||
// 配置
|
||||
$router->get('config/parent', '\catchAdmin\system\controller\Config@parent');
|
||||
$router->resource('config', '\catchAdmin\system\controller\Config');
|
Loading…
x
Reference in New Issue
Block a user