122 lines
3.6 KiB
PHP
Raw Normal View History

2021-01-30 20:59:12 +08:00
<?php
2021-07-26 17:41:59 +08:00
/**
* @copyright Copyright (c) 2021 勾股工作室
2021-11-24 17:17:29 +08:00
* @license https://opensource.org/licenses/Apache-2.0
2021-07-26 17:41:59 +08:00
* @link https://www.gougucms.com
*/
2021-01-30 20:59:12 +08:00
declare (strict_types = 1);
namespace app\admin\controller;
use app\admin\BaseController;
use app\admin\validate\ConfCheck;
use think\exception\ValidateException;
2021-02-19 19:07:41 +08:00
use think\facade\Db;
2021-01-30 20:59:12 +08:00
use think\facade\View;
class Conf extends BaseController
{
2021-02-19 19:07:41 +08:00
public function index()
2021-01-30 20:59:12 +08:00
{
if (request()->isAjax()) {
$param = get_params();
$where = array();
$where[] = ['status', '>=', 0];
2021-07-26 17:41:59 +08:00
$rows = empty($param['limit']) ? get_config(app . page_size) : $param['limit'];
$content = Db::name('Config')
->where($where)
->paginate($rows, false, ['query' => $param]);
2021-09-09 08:33:05 +08:00
return table_assign(0, '', $content);
2021-07-26 17:41:59 +08:00
} else {
return view();
2021-07-26 17:41:59 +08:00
}
2021-01-30 20:59:12 +08:00
}
2021-02-19 19:07:41 +08:00
//添加
public function add()
2021-01-30 20:59:12 +08:00
{
2021-02-19 19:07:41 +08:00
$id = empty(get_params('id')) ? 0 : get_params('id');
if ($id > 0) {
2021-07-26 17:41:59 +08:00
$config = Db::name('Config')->where(['id' => $id])->find();
2021-02-19 19:07:41 +08:00
View::assign('config', $config);
}
View::assign('id', $id);
return view();
2021-01-30 20:59:12 +08:00
}
2021-02-19 19:07:41 +08:00
//提交添加
public function post_submit()
2021-01-30 20:59:12 +08:00
{
2021-02-22 23:43:11 +08:00
if (request()->isAjax()) {
2021-02-19 19:07:41 +08:00
$param = get_params();
try {
validate(ConfCheck::class)->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
2021-09-09 08:33:05 +08:00
return to_assign(1, $e->getError());
2021-02-19 19:07:41 +08:00
}
if (!empty($param['id']) && $param['id'] > 0) {
$param['update_time'] = time();
$res = Db::name('config')->strict(false)->field(true)->update($param);
2021-07-26 17:41:59 +08:00
if ($res) {
add_log('edit', $param['id'], $param);
}
2021-02-19 19:07:41 +08:00
return to_assign();
} else {
$param['create_time'] = time();
2021-07-26 17:41:59 +08:00
$insertId = Db::name('Config')->strict(false)->field(true)->insertGetId($param);
if ($insertId) {
add_log('add', $insertId, $param);
}
2021-02-19 19:07:41 +08:00
return to_assign();
}
2021-01-30 20:59:12 +08:00
}
}
2021-02-19 19:07:41 +08:00
//删除
public function delete()
2021-01-30 20:59:12 +08:00
{
2021-02-19 19:07:41 +08:00
$id = get_params("id");
$data['status'] = '-1';
$data['id'] = $id;
$data['update_time'] = time();
2021-07-26 17:41:59 +08:00
if (Db::name('Config')->update($data) !== false) {
add_log('delete', $id, $data);
2021-09-09 08:33:05 +08:00
return to_assign(0, "删除成功");
2021-02-19 19:07:41 +08:00
} else {
2021-09-09 08:33:05 +08:00
return to_assign(1, "删除失败");
2021-02-19 19:07:41 +08:00
}
2021-01-30 20:59:12 +08:00
}
2021-02-19 19:07:41 +08:00
//编辑配置
public function edit()
2021-01-30 20:59:12 +08:00
{
2021-02-19 19:07:41 +08:00
$id = empty(get_params('id')) ? 0 : get_params('id');
2021-07-26 17:41:59 +08:00
$conf = Db::name('Config')->where('id', $id)->find();
$config = [];
if ($conf['content']) {
2021-02-19 19:07:41 +08:00
$config = unserialize($conf['content']);
}
2021-07-26 17:41:59 +08:00
return view($conf['name'], ['id' => $id, 'config' => $config]);
2021-02-19 19:07:41 +08:00
}
//提交添加
public function conf_submit()
{
2021-02-22 23:43:11 +08:00
if (request()->isAjax()) {
2021-02-19 19:07:41 +08:00
$param = get_params();
$data['content'] = serialize($param);
$data['update_time'] = time();
$data['id'] = $param['id'];
2021-07-26 17:41:59 +08:00
$res = Db::name('Config')->strict(false)->field(true)->update($data);
$conf = Db::name('Config')->where('id', $param['id'])->find();
2021-02-19 19:07:41 +08:00
clear_cache('system_config' . $conf['name']);
2021-07-26 17:41:59 +08:00
if ($res) {
add_log('edit', $param['id'], $param);
}
2021-02-19 19:07:41 +08:00
return to_assign();
2021-01-30 20:59:12 +08:00
}
}
}