diff --git a/app/Command/Update.php b/app/Command/Update.php index f5e56110..4d24db14 100644 --- a/app/Command/Update.php +++ b/app/Command/Update.php @@ -3,6 +3,7 @@ namespace App\Command; use App\Services\Config; +use App\Services\DefaultConfig; class Update { @@ -17,6 +18,10 @@ class Update return false; } + echo(PHP_EOL); + // 检查并创建新增的配置项 + echo DefaultConfig::detectConfigs(); + echo(PHP_EOL); echo('开始升级客户端...' . PHP_EOL); diff --git a/app/Controllers/Admin/GConfigController.php b/app/Controllers/Admin/GConfigController.php new file mode 100644 index 00000000..3cd4cba5 --- /dev/null +++ b/app/Controllers/Admin/GConfigController.php @@ -0,0 +1,113 @@ + '操作', + 'name' => '配置名称', + 'key' => '配置名', + 'value' => '配置值', + 'operator_id' => '操作员 ID', + 'operator_name' => '操作员名称', + 'operator_email' => '操作员邮箱', + 'last_update' => '修改时间' + ); + $table_config['default_show_column'] = array(); + foreach ($table_config['total_column'] as $column => $value) { + $table_config['default_show_column'][] = $column; + } + $table_config['ajax_url'] = $ajax_url; + return $this->view()->assign('table_config', $table_config)->fetch($tpl_path); + } + + public function edit($key, $tpl_path) + { + $config = GConfig::find($key); + return $this->view()->assign('edit_config', $config)->fetch($tpl_path); + } + + public function update($request, $response, $args) + { + $key = trim($args['key']); + $user = Auth::getUser(); + + $config = GConfig::find($key); + $config->oldvalue = $config->value; + $config->value = $request->getParam('value'); + $config->operator_id = $user->id; + $config->operator_name = $user->user_name; + $config->operator_email = $user->email; + $config->last_update = time(); + if (!$config->save()) { + $rs['ret'] = 0; + $rs['msg'] = '修改失败'; + return $response->write(json_encode($rs)); + } + $rs['ret'] = 1; + $rs['msg'] = '修改成功'; + return $response->write(json_encode($rs)); + } + + public function telegram($request, $response, $args) + { + return self::index('telegram/ajax', 'admin/config/telegram/index.tpl'); + } + + public function telegram_edit($request, $response, $args) + { + $key = trim($args['key']); + return self::edit($key, 'admin/config/telegram/edit.tpl'); + } + + public function telegram_update($request, $response, $args) + { + return self::update($request, $response, $args); + } + + public function telegram_ajax($request, $response, $args) + { + $configs = GConfig::where('key', 'LIKE', "%Telegram%")->get(); + $data = []; + foreach ($configs as $config) { + $tempdata = []; + $tempdata['op'] = '编辑'; + $tempdata['name'] = $config->name; + $tempdata['key'] = $config->key; + $tempdata['value'] = $config->value; + $tempdata['operator_id'] = $config->operator_id; + $tempdata['operator_name'] = $config->operator_name; + $tempdata['operator_email'] = $config->operator_email; + $tempdata['last_update'] = date('Y-m-d H:i:s', $config->last_update); + if (strpos($config->key, '.enable.')) { + switch ((int) $config->value) { + case 0: + $tempdata['value'] = '关闭'; + break; + case 1: + $tempdata['value'] = '开启'; + break; + default: + $tempdata['value'] = '未知'; + break; + } + } + $data[] = $tempdata; + } + $info = [ + 'draw' => $request->getParam('draw'), + 'recordsTotal' => count($configs), + 'recordsFiltered' => count($configs), + 'data' => $data + ]; + + return json_encode($info, true); + } +} diff --git a/app/Models/GConfig.php b/app/Models/GConfig.php new file mode 100755 index 00000000..c67061a8 --- /dev/null +++ b/app/Models/GConfig.php @@ -0,0 +1,55 @@ +oldvalue = $this->value; + $this->value = DefaultConfig::default_value($this->key)['value']; + $this->operator_id = $user->id; + $this->operator_name = ('[恢复默认] - ' . $user->user_name); + $this->operator_email = $user->email; + $this->last_update = time(); + $this->save(); + } +} diff --git a/app/Services/Config.php b/app/Services/Config.php index 9ecd5de3..63c25afb 100644 --- a/app/Services/Config.php +++ b/app/Services/Config.php @@ -2,6 +2,8 @@ namespace App\Services; +use App\Models\GConfig; + class Config { // TODO: remove @@ -10,6 +12,15 @@ class Config return $_ENV[$key]; } + public static function getdb($key) + { + $value = GConfig::find($key); + if ($value === null) { + $value = DefaultConfig::firstOrCreate($key); + } + return $value; + } + public static function getPublicConfig() { return [ diff --git a/app/Services/DefaultConfig.php b/app/Services/DefaultConfig.php new file mode 100644 index 00000000..de0c4dd8 --- /dev/null +++ b/app/Services/DefaultConfig.php @@ -0,0 +1,227 @@ +key = $value['key']; + $new->value = $value['value']; + $new->name = $value['name']; + $new->comment = $value['comment']; + $new->operator_id = $value['operator_id']; + $new->operator_name = $value['operator_name']; + $new->oldvalue = ''; + $new->operator_email = ''; + $new->last_update = time(); + if ($new->save()) { + return true; + } + } + + return false; + } + + public static function firstOrCreate($key) + { + return (self::create($key) + ? GConfig::find($key) + : null + ); + } + + public static function detectConfigs() + { + $return = '开始检查新增的配置项...' . PHP_EOL; + $configs = self::configs(); + foreach ($configs as $key => $value) { + $config = GConfig::find($key); + if ($config == null) { + if (self::create($key)) { + $return .= '新增的配置项:' . $key . ':' . $value['name'] . PHP_EOL; + } else { + $return .= $key . ':配置项创建失败,请检查错误' . PHP_EOL; + } + } + } + $return .= '以上是新增的配置项...' . PHP_EOL; + + return $return; + } + + public static function configs($key = null) + { + $configs = [ + 'Telegram.enable.Diary' => [ + 'key' => $key, + 'value' => 1, + 'name' => '开启 Telegram 群组推送系统今天的运行状况', + 'comment' => '', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + 'Telegram.msg.Diary' => [ + 'key' => $key, + 'value' => ('各位老爷少奶奶,我来为大家报告一下系统今天的运行状况哈~' . PHP_EOL . '今日签到人数:%getTodayCheckinUser%' . PHP_EOL . '今日使用总流量:%lastday_total%' . PHP_EOL . '晚安~'), + 'name' => '自定义向 Telegram 群组推送系统今天的运行状况的信息', + 'comment' => '可用变量:' . PHP_EOL . '[今日签到人数] %getTodayCheckinUser%' . PHP_EOL . '[今日使用总流量] %lastday_total%', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + + 'Telegram.enable.DailyJob' => [ + 'key' => $key, + 'value' => 1, + 'name' => '开启 Telegram 群组推送数据库清理的通知', + 'comment' => '', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + 'Telegram.msg.DailyJob' => [ + 'key' => $key, + 'value' => '姐姐姐姐,数据库被清理了,感觉身体被掏空了呢~', + 'name' => '自定义向 Telegram 群组推送数据库清理通知的信息', + 'comment' => '', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + + 'Telegram.enable.NodeOffline' => [ + 'key' => $key, + 'value' => 1, + 'name' => '开启 Telegram 群组推送节点掉线的通知', + 'comment' => '', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + 'Telegram.msg.NodeOffline' => [ + 'key' => $key, + 'value' => '喵喵喵~ %node_name% 节点掉线了喵~', + 'name' => '自定义向 Telegram 群组推送节点掉线通知的信息', + 'comment' => '可用变量:' . PHP_EOL . '[节点名称] %node_name%', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + + 'Telegram.enable.NodeOnline' => [ + 'key' => $key, + 'value' => 1, + 'name' => '开启 Telegram 群组推送节点上线的通知', + 'comment' => '', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + 'Telegram.msg.NodeOnline' => [ + 'key' => $key, + 'value' => '喵喵喵~ %node_name% 节点恢复了喵~', + 'name' => '自定义向 Telegram 群组推送节点上线通知的信息', + 'comment' => '可用变量:' . PHP_EOL . '[节点名称] %node_name%', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + + 'Telegram.enable.NodeGFW' => [ + 'key' => $key, + 'value' => 1, + 'name' => '开启 Telegram 群组推送节点被墙的通知', + 'comment' => '', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + 'Telegram.msg.NodeGFW' => [ + 'key' => $key, + 'value' => '喵喵喵~ %node_name% 节点被墙了喵~', + 'name' => '自定义向 Telegram 群组推送节点被墙通知的信息', + 'comment' => '可用变量:' . PHP_EOL . '[节点名称] %node_name%', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + + 'Telegram.enable.NodeGFW_recover' => [ + 'key' => $key, + 'value' => 1, + 'name' => '开启 Telegram 群组推送节点被墙恢复的通知', + 'comment' => '', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + 'Telegram.msg.NodeGFW_recover' => [ + 'key' => $key, + 'value' => '喵喵喵~ %node_name% 节点恢复了喵~', + 'name' => '自定义向 Telegram 群组推送节点被墙恢复通知的信息', + 'comment' => '可用变量:' . PHP_EOL . '[节点名称] %node_name%', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + + 'Telegram.enable.AddNode' => [ + 'key' => $key, + 'value' => 1, + 'name' => '开启 Telegram 群组推送节点新增的通知', + 'comment' => '', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + 'Telegram.msg.AddNode' => [ + 'key' => $key, + 'value' => '新节点添加~ %node_name%', + 'name' => '自定义向 Telegram 群组推送节点新增通知的信息', + 'comment' => '可用变量:' . PHP_EOL . '[节点名称] %node_name%', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + + 'Telegram.enable.UpdateNode' => [ + 'key' => $key, + 'value' => 1, + 'name' => '开启 Telegram 群组推送节点修改的通知', + 'comment' => '', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + 'Telegram.msg.UpdateNode' => [ + 'key' => $key, + 'value' => '节点信息被修改~ %node_name%', + 'name' => '自定义向 Telegram 群组推送节点修改通知的信息', + 'comment' => '可用变量:' . PHP_EOL . '[节点名称] %node_name%', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + + 'Telegram.enable.DeleteNode' => [ + 'key' => $key, + 'value' => 1, + 'name' => '开启 Telegram 群组推送节点删除的通知', + 'comment' => '', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + 'Telegram.msg.DeleteNode' => [ + 'key' => $key, + 'value' => '节点被删除~ %node_name%', + 'name' => '自定义向 Telegram 群组推送节点删除通知的信息', + 'comment' => '可用变量:' . PHP_EOL . '[节点名称] %node_name%', + 'operator_id' => 0, + 'operator_name' => '系统默认', + ], + ]; + return ($key === null + ? $configs + : $configs[$key] + ); + } + + public static function default_value($key) + { + return (in_array($key, array_keys(self::configs())) + ? self::configs($key) + : null + ); + } +} diff --git a/config/routes.php b/config/routes.php index 7ee493e9..2ba4ef3b 100644 --- a/config/routes.php +++ b/config/routes.php @@ -314,6 +314,14 @@ $app->group('/admin', function () { $this->get('/sys', App\Controllers\AdminController::class . ':sys'); $this->get('/logout', App\Controllers\AdminController::class . ':logout'); $this->post('/payback/ajax', App\Controllers\AdminController::class . ':ajax_payback'); + + // Config Mange + $this->group('/config', function () { + $this->get('/telegram', App\Controllers\Admin\GConfigController::class . ':telegram'); + $this->post('/telegram/ajax', App\Controllers\Admin\GConfigController::class . ':telegram_ajax'); + $this->get('/telegram/{key}/edit', App\Controllers\Admin\GConfigController::class . ':telegram_edit'); + $this->put('/telegram/{key}', App\Controllers\Admin\GConfigController::class . ':telegram_update'); + }); })->add(new Admin()); // mu diff --git a/resources/views/material/admin/config/telegram/edit.tpl b/resources/views/material/admin/config/telegram/edit.tpl new file mode 100644 index 00000000..f5603cda --- /dev/null +++ b/resources/views/material/admin/config/telegram/edit.tpl @@ -0,0 +1,119 @@ +{include file='admin/main.tpl'} + + + + + + 配置编辑 #{$edit_config->id} + + + + + + + + + + + + 配置名 + + + + + 配置名称 + + + + {if $edit_config->comment!=''} + + 配置描述 + {$edit_config->comment} + + {/if} + + {if strpos($edit_config->key,'.enable.')===false} + + 配置值 + {$edit_config->value} + + {else} + + + 配置开关 + + value=='0'}selected{/if}>关闭 + value=='1'}selected{/if}>开启 + + + + {/if} + + + + + + + + + + + + + 修改 + + + + + + + + + {include file='dialog.tpl'} + + + + + + + +{include file='admin/footer.tpl'} + + + diff --git a/resources/views/material/admin/config/telegram/index.tpl b/resources/views/material/admin/config/telegram/index.tpl new file mode 100644 index 00000000..826efecc --- /dev/null +++ b/resources/views/material/admin/config/telegram/index.tpl @@ -0,0 +1,41 @@ +{include file='admin/main.tpl'} + + + + + Telegram 配置 + + + + + + + + + + 显示表项: {include file='table/checkbox.tpl'} + + + + + + {include file='table/table.tpl'} + + + + + + + + + +{include file='admin/footer.tpl'} + + + diff --git a/sql/gconfig.sql b/sql/gconfig.sql new file mode 100644 index 00000000..92640fbf --- /dev/null +++ b/sql/gconfig.sql @@ -0,0 +1,12 @@ +CREATE TABLE IF NOT EXISTS `gconfig` ( + `key` varchar(50) NOT NULL COMMENT '配置名', + `value` text NOT NULL COMMENT '配置值', + `oldvalue` text NOT NULL COMMENT '之前的配置值', + `name` varchar(128) NOT NULL COMMENT '配置名称', + `comment` text NOT NULL COMMENT '配置描述', + `operator_id` int(11) NOT NULL COMMENT '操作员 ID', + `operator_name` varchar(128) NOT NULL COMMENT '操作员名称', + `operator_email` varchar(32) NOT NULL COMMENT '操作员邮箱', + `last_update` bigint(20) NOT NULL COMMENT '修改时间', + PRIMARY KEY (`key`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='网站配置';
显示表项: {include file='table/checkbox.tpl'}