From 1b954c1b166a319dbda38650c2549d9444427c65 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Tue, 21 Apr 2020 10:34:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=85=8D=E7=BD=AE=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/system/controller/Config.php | 65 ++++++ .../migrations/20200417083602_config.php | 44 ++++ catch/system/model/Config.php | 210 ++++++++++++++++++ catch/system/route.php | 6 +- 4 files changed, 324 insertions(+), 1 deletion(-) create mode 100644 catch/system/controller/Config.php create mode 100644 catch/system/database/migrations/20200417083602_config.php create mode 100644 catch/system/model/Config.php diff --git a/catch/system/controller/Config.php b/catch/system/controller/Config.php new file mode 100644 index 0000000..0a3c2b5 --- /dev/null +++ b/catch/system/controller/Config.php @@ -0,0 +1,65 @@ +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)); + } +} \ No newline at end of file diff --git a/catch/system/database/migrations/20200417083602_config.php b/catch/system/database/migrations/20200417083602_config.php new file mode 100644 index 0000000..2d7e03e --- /dev/null +++ b/catch/system/database/migrations/20200417083602_config.php @@ -0,0 +1,44 @@ +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(); + } +} diff --git a/catch/system/model/Config.php b/catch/system/model/Config.php new file mode 100644 index 0000000..b84f4a0 --- /dev/null +++ b/catch/system/model/Config.php @@ -0,0 +1,210 @@ +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; + } +} \ No newline at end of file diff --git a/catch/system/route.php b/catch/system/route.php index 42ce818..4ab6a68 100644 --- a/catch/system/route.php +++ b/catch/system/route.php @@ -17,4 +17,8 @@ $router->post('upload/image', '\catchAdmin\system\controller\Upload@image'); $router->post('upload/file', '\catchAdmin\system\controller\Upload@file'); // 附件 -$router->resource('attachments', '\catchAdmin\system\controller\Attachments'); \ No newline at end of file +$router->resource('attachments', '\catchAdmin\system\controller\Attachments'); + +// 配置 +$router->get('config/parent', '\catchAdmin\system\controller\Config@parent'); +$router->resource('config', '\catchAdmin\system\controller\Config'); \ No newline at end of file