网站配置

dev
GeekQu 7 years ago
parent 8546a43f9e
commit 4f312527ae
No known key found for this signature in database
GPG Key ID: F0EEABFCD04A5891

@ -3,6 +3,7 @@
namespace App\Command; namespace App\Command;
use App\Services\Config; use App\Services\Config;
use App\Services\DefaultConfig;
class Update class Update
{ {
@ -17,6 +18,10 @@ class Update
return false; return false;
} }
echo(PHP_EOL);
// 检查并创建新增的配置项
echo DefaultConfig::detectConfigs();
echo(PHP_EOL); echo(PHP_EOL);
echo('开始升级客户端...' . PHP_EOL); echo('开始升级客户端...' . PHP_EOL);

@ -0,0 +1,113 @@
<?php
namespace App\Controllers\Admin;
use App\Controllers\AdminController;
use App\Services\Auth;
use App\Models\GConfig;
class GConfigController extends AdminController
{
public function index($ajax_url, $tpl_path)
{
$table_config['total_column'] = array(
'op' => '操作',
'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'] = '<a class="btn btn-brand" href="/admin/config/telegram/' . $config->key . '/edit">编辑</a>';
$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);
}
}

@ -0,0 +1,55 @@
<?php
namespace App\Models;
use App\Services\DefaultConfig;
class GConfig extends Model
{
/**
* The connection name for the model.
*
* @var string
*/
protected $connection = 'default';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'gconfig';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'key';
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
/**
* The "type" of the auto-incrementing ID.
*
* @var string
*/
protected $keyType = 'string';
public static function recover($user)
{
$this->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();
}
}

@ -2,6 +2,8 @@
namespace App\Services; namespace App\Services;
use App\Models\GConfig;
class Config class Config
{ {
// TODO: remove // TODO: remove
@ -10,6 +12,15 @@ class Config
return $_ENV[$key]; 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() public static function getPublicConfig()
{ {
return [ return [

@ -0,0 +1,227 @@
<?php
namespace App\Services;
use App\Models\GConfig;
class DefaultConfig
{
public static function create($key)
{
$value = self::default_value($key);
if ($value != null) {
$new = new GConfig();
$new->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
);
}
}

@ -314,6 +314,14 @@ $app->group('/admin', function () {
$this->get('/sys', App\Controllers\AdminController::class . ':sys'); $this->get('/sys', App\Controllers\AdminController::class . ':sys');
$this->get('/logout', App\Controllers\AdminController::class . ':logout'); $this->get('/logout', App\Controllers\AdminController::class . ':logout');
$this->post('/payback/ajax', App\Controllers\AdminController::class . ':ajax_payback'); $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()); })->add(new Admin());
// mu // mu

@ -0,0 +1,119 @@
{include file='admin/main.tpl'}
<main class="content">
<div class="content-header ui-content-header">
<div class="container">
<h1 class="content-heading">配置编辑 #{$edit_config->id}</h1>
</div>
</div>
<div class="container">
<div class="col-lg-12 col-sm-12">
<section class="content-inner margin-top-no">
<div class="card">
<div class="card-main">
<div class="card-inner">
<div class="form-group form-group-label">
<label class="floating-label" for="key">配置名</label>
<input class="form-control maxwidth-edit" id="key" type="text" value="{$edit_config->key}" readonly>
</div>
<div class="form-group form-group-label">
<label class="floating-label" for="name">配置名称</label>
<input class="form-control maxwidth-edit" id="name" type="text" value="{$edit_config->name}" readonly>
</div>
{if $edit_config->comment!=''}
<div class="form-group form-group-label">
<label class="floating-label" for="comment">配置描述</label>
<textarea class="form-control maxwidth-edit" id="comment" rows="4" readonly>{$edit_config->comment}</textarea>
</div>
{/if}
{if strpos($edit_config->key,'.enable.')===false}
<div class="form-group form-group-label">
<label class="floating-label" for="value">配置值</label>
<textarea class="form-control maxwidth-edit" id="value" rows="5">{$edit_config->value}</textarea>
</div>
{else}
<div class="form-group form-group-label">
<label for="value">
<label class="floating-label" for="value">配置开关</label>
<select id="value" class="form-control maxwidth-edit" name="value">
<option value="0" {if $edit_config->value=='0'}selected{/if}>关闭</option>
<option value="1" {if $edit_config->value=='1'}selected{/if}>开启</option>
</select>
</label>
</div>
{/if}
</div>
</div>
</div>
<div class="card">
<div class="card-main">
<div class="card-inner">
<div class="form-group">
<div class="row">
<div class="col-md-10 col-md-push-1">
<button id="submit" type="submit"
class="btn btn-block btn-brand waves-attach waves-light">修改
</button>
</div>
</div>
</div>
</div>
</div>
</div>
{include file='dialog.tpl'}
</div>
</div>
</main>
{include file='admin/footer.tpl'}
<script>
window.addEventListener('load', () => {
function submit() {
$.ajax({
type: "PUT",
url: "/admin/config/telegram/{$edit_config->key}",
dataType: "json",
data: {
value: $$getValue('value')
},
success: data => {
if (data.ret) {
$("#result").modal();
$$.getElementById('msg').innerHTML = data.msg;
window.setTimeout("location.href=top.document.referrer", {$config['jump_delay']});
} else {
$("#result").modal();
$$.getElementById('msg').innerHTML = data.msg;
}
},
error: jqXHR => {
$("#result").modal();
$$.getElementById('msg').innerHTML = `发生错误:${
jqXHR.status
}`;
}
});
}
$("html").keydown(event => {
if (event.keyCode == 13) login();
});
$$.getElementById('submit').addEventListener('click', submit);
})
</script>

@ -0,0 +1,41 @@
{include file='admin/main.tpl'}
<main class="content">
<div class="content-header ui-content-header">
<div class="container">
<h1 class="content-heading">Telegram 配置</h1>
</div>
</div>
<div class="container">
<div class="col-lg-12 col-sm-12">
<section class="content-inner margin-top-no">
<div class="card">
<div class="card-main">
<div class="card-inner">
<p>显示表项: {include file='table/checkbox.tpl'}</p>
</div>
</div>
</div>
<div class="table-responsive">
{include file='table/table.tpl'}
</div>
</div>
</div>
</main>
{include file='admin/footer.tpl'}
<script>
{include file='table/js_1.tpl'}
window.addEventListener('load', () => {
{include file='table/js_2.tpl'}
});
</script>

@ -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='网站配置';
Loading…
Cancel
Save