修复配置管理以及菜单管理,增加验证

This commit is contained in:
2020-03-26 16:19:45 +08:00
parent 1346905e82
commit 8cd53f9366
11 changed files with 190 additions and 153 deletions

View File

@@ -27,10 +27,10 @@ class Admin extends Base {
],
];
// protected $middleware = [
// '\app\http\middleware\Validate',
// '\app\http\middleware\Admin',
// ];
protected $middleware = [
'\app\http\middleware\Validate',
'\app\http\middleware\Admin',
];
protected function initialize() {
$url = str_replace(".", "/", strtolower($this->request->controller())) . '/' . $this->request->action();

View File

@@ -6,10 +6,11 @@
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\controller\admin;
use app\controller\Admin;
use app\model\Config as ConfigM;
use think\facade\Cache;
/**
* @title 配置管理
@@ -89,15 +90,15 @@ class Config extends Admin {
if ($this->request->isPost()) {
$data = $this->request->post();
if ($data) {
$id = $config->save($data);
if ($id) {
cache('system_config_data', null);
$result = ConfigM::create($data);
if (false !== $result) {
Cache::pull('db_config_data');
return $this->success('新增成功', url('/admin/config/index'));
} else {
return $this->error('新增失败');
}
} else {
return $this->error($config->getError());
return $this->error('无添加数据!');
}
} else {
$this->data['info'] = [];
@@ -111,31 +112,28 @@ class Config extends Admin {
*/
public function edit($id = 0) {
if ($this->request->isPost()) {
$config = model('Config');
$data = $this->request->post();
if ($data) {
$result = $config->validate('Config.edit')->save($data, array('id' => $data['id']));
$result = ConfigM::update($data, array('id' => $data['id']));
if (false !== $result) {
cache('db_config_data', null);
Cache::pull('db_config_data');
//记录行为
action_log('update_config', 'config', $data['id'], session('user_auth.uid'));
return $this->success('更新成功', Cookie('__forward__'));
} else {
return $this->error($config->getError(), '');
return $this->error('更新失败!');
}
} else {
return $this->error($config->getError());
return $this->error('无更新数据!');
}
} else {
$info = array();
/* 获取数据 */
$info = db('Config')->field(true)->find($id);
$info = ConfigM::find($id);
if (false === $info) {
return $this->error('获取配置信息错误');
}
$this->assign('info', $info);
$this->setMeta('编辑配置');
$this->data = ['info' => $info];
return $this->fetch();
}
}

View File

@@ -11,6 +11,7 @@ namespace app\controller\admin;
use sent\tree\Tree;
use app\controller\Admin;
use app\model\Menu as MenuM;
use think\facade\Cache;
/**
* @title 菜单管理
@@ -44,10 +45,13 @@ class Menu extends Admin {
* @title 编辑菜单字段
*/
public function editable() {
$data = $this->request->param();
if ($name && ($value != null || $value != '') && $pk) {
db('Menu')->where(array('id' => $pk))->setField($name, $value);
$name = $this->request->param('name', '');
$value = $this->request->param('value', '');
$pk = $this->request->param('pk', '');
if ($name && $value && $pk) {
$save[$name] = $value;
MenuM::update($save, ['id' => $pk]);
}
}
@@ -57,13 +61,11 @@ class Menu extends Admin {
*/
public function add() {
if ($this->request->isPost()) {
$Menu = model('Menu');
$data = input('post.');
$id = $Menu->save($data);
$data = $this->request->post();
$id = MenuM::create($data);
if ($id) {
session('admin_menu_list', null);
//记录行为
action_log('update_menu', 'Menu', $id, session('user_auth.uid'));
Cache::pull('admin_menu_list');
return $this->success('新增成功', Cookie('__forward__'));
} else {
return $this->error('新增失败');
@@ -130,17 +132,21 @@ class Menu extends Admin {
* @author yangweijie <yangweijiester@gmail.com>
*/
public function del() {
$id = $this->getArrayParam('id');
$id = $this->request->param('id', '');
if (empty($id)) {
$map = [];
if (!$id) {
return $this->error('请选择要操作的数据!');
}
if (is_array($id)) {
$map[] = ['id', 'IN', $id];
}else{
$map[] = ['id', '=', $id];
}
$map = array('id' => array('in', $id));
if (db('Menu')->where($map)->delete()) {
session('admin_menu_list', null);
//记录行为
action_log('update_menu', 'Menu', $id, session('user_auth.uid'));
if (MenuM::where($map)->delete()) {
Cache::pull('admin_menu_list');
return $this->success('删除成功');
} else {
return $this->error('删除失败!');
@@ -148,8 +154,9 @@ class Menu extends Admin {
}
public function toogleHide($id, $value = 1) {
session('admin_menu_list', null);
$result = db('Menu')->where(array('id' => $id))->setField(array('hide' => $value));
Cache::pull('admin_menu_list');
$result = MenuM::update(['hide'=> $value], ['id' => $id]);
if ($result !== false) {
return $this->success('操作成功!');
} else {
@@ -158,8 +165,9 @@ class Menu extends Admin {
}
public function toogleDev($id, $value = 1) {
session('admin_menu_list', null);
$result = db('Menu')->where(array('id' => $id))->setField(array('is_dev' => $value));
Cache::pull('admin_menu_list');
$result = MenuM::update(['is_dev'=> $value], ['id' => $id]);
if ($result !== false) {
return $this->success('操作成功!');
} else {
@@ -190,80 +198,45 @@ class Menu extends Admin {
}
}
/**
* @title 批量导入
* @author yangweijie <yangweijiester@gmail.com>
*/
public function import() {
if ($this->request->isPost()) {
$tree = input('post.tree');
$tree = $this->request->post('tree', '');
$pid = $this->request->param('pid', 0);
$lists = explode(PHP_EOL, $tree);
$menuModel = db('Menu');
if ($lists == array()) {
if (empty($lists)) {
return $this->error('请按格式填写批量导入的菜单,至少一个菜单');
} else {
$pid = input('post.pid');
foreach ($lists as $key => $value) {
$record = explode('|', $value);
if (count($record) == 4) {
$menuModel->add(array(
'title' => $record[0],
'url' => $record[1],
'pid' => $record[2],
'sort' => 0,
'hide' => 0,
'tip' => '',
'is_dev' => 0,
'group' => $record[3],
));
}
}
session('admin_menu_list', null);
return $this->success('导入成功', url('index?pid=' . $pid));
}
} else {
$this->setMeta('批量导入后台菜单');
$pid = (int) input('get.pid');
$this->assign('pid', $pid);
$data = db('Menu')->where("id={$pid}")->field(true)->find();
$this->assign('data', $data);
return $this->fetch();
}
}
/**
* @title 菜单排序
* @author huajie <banhuajie@163.com>
*/
public function sort() {
if ($this->request->isGet()) {
$ids = input('ids');
$pid = input('pid');
//获取排序的数据
$map = array('status' => array('gt', -1));
if (!empty($ids)) {
$map['id'] = array('in', $ids);
} else {
if ($pid !== '') {
$map['pid'] = $pid;
$data = [];
foreach ($lists as $value) {
list($title, $url, $pid, $group) = explode('|', $value);
if ($title != '' && $url != '' && $pid != '' && $group != '') {
$data[] = ['title' => $title, 'url' => $url, 'pid' => $pid, 'sort' => 0, 'hide' => 0, 'tip' => '', 'is_dev' => 0, 'group' => $group];
}
}
$list = db('Menu')->where($map)->field('id,title')->order('sort asc,id asc')->select();
$this->assign('list', $list);
$this->setMeta('菜单排序');
return $this->fetch();
} elseif ($this->request->isPost()) {
$ids = input('post.ids');
$ids = explode(',', $ids);
foreach ($ids as $key => $value) {
$res = db('Menu')->where(array('id' => $value))->setField('sort', $key + 1);
}
if ($res !== false) {
session('admin_menu_list', null);
return $this->success('排序成功!');
} else {
return $this->error('排序失败!');
$result = (new MenuM())->saveAll($data);
if (false !== $result) {
Cache::pull('admin_menu_list');
return $this->success('导入成功', url('/admin/menu/index'));
}else{
return $this->error('导入失败!');
}
} else {
return $this->error('非法请求!');
$pid = $this->request->param('pid', 0);
$menu = MenuM::find($pid);
$this->data = [
'pid' => $pid,
'menu' => $menu
];
return $this->fetch();
}
}
}

View File

@@ -0,0 +1,37 @@
<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\http\validate\admin;
use think\Validate;
/**
* 菜单验证
*/
class Config extends Validate{
protected $rule = [
'name' => 'require|unique:config',
'title' => 'require',
];
protected $message = [
'name.require' => '配置标识必须',
'name.unique' => '配置标识已存在',
'title.require' => '配置标题必须',
];
protected $scene = [
'add' => ['title', 'name'],
];
// edit 验证场景定义
public function sceneEdit() {
return $this->only([['title', 'name']])
->remove('name', 'unique');
}
}

View File

@@ -0,0 +1,29 @@
<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\http\validate\admin;
use think\Validate;
/**
* 菜单验证
*/
class Menu extends Validate{
protected $rule = [
'title' => 'require',
];
protected $message = [
'title.require' => '菜单名称必须',
];
protected $scene = [
'add' => ['title'],
'edit' => ['title'],
];
}

View File

@@ -26,7 +26,7 @@ class Menu extends \think\Model {
protected function getHideTextAttr($value, $data){
$is_dev = [0 => '否', 1 => '是'];
return isset($is_dev[$data['is_dev']]) ? $is_dev[$data['is_dev']] : '是';
return isset($is_dev[$data['hide']]) ? $is_dev[$data['hide']] : '是';
}
public function getAuthNodes($type = 'admin') {

View File

@@ -10,40 +10,40 @@
<div class="main-box-body clearfix">
<form method="post" class="form form-horizontal">
<div class="form-group">
<label class="col-lg-2 control-label">配置标识</label>
<div class="col-lg-10">
<label class="col-sm-2 control-label">配置标识</label>
<div class="col-sm-10">
<input type="text" class="form-control" style="width: 400px" name="name" value="{$info['name']|default=''}">
<span class="help-block">用于config函数调用只能使用英文且不能重复</span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">配置标题</label>
<div class="col-lg-10">
<label class="col-sm-2 control-label">配置标题</label>
<div class="col-sm-10">
<input type="text" class="form-control" style="width:400px;" name="title" value="{$info['title']|default=''}">
<span class="help-block">(用于后台显示的配置标题)</span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">排序</label>
<div class="col-lg-10">
<label class="col-sm-2 control-label">排序</label>
<div class="col-sm-10">
<input type="text" class="form-control" style="width: 400px" name="sort" value="{$info['sort']|default=0}">
<span class="help-block">(用于分组显示的顺序)</span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">配置类型</label>
<div class="col-lg-10">
<label class="col-sm-2 control-label">配置类型</label>
<div class="col-sm-10">
<select name="type" class="form-control" style="width:auto;">
{volist name="config['config_type_list']" id="item"}
<option value="{$item['key']}" {if isset($info['type']) && $type['key'] == $info['type']}selected{/if}>{$item['label']}</option>
<option value="{$item['key']}" {if isset($info['type']) && $item['key'] == $info['type']}selected{/if}>{$item['label']}</option>
{/volist}
</select>
<span class="help-block">(系统会根据不同类型解析配置值)</span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">配置分组</label>
<div class="col-lg-10">
<label class="col-sm-2 control-label">配置分组</label>
<div class="col-sm-10">
<select name="group" class="form-control" style="width: auto">
<option value="0">不分组</option>
{volist name="config['config_group_list']" id="item"}
@@ -54,28 +54,28 @@
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">配置值</label>
<div class="col-lg-10">
<label class="col-sm-2 control-label">配置值</label>
<div class="col-sm-10">
<textarea name="value" class="form-control" style="width: 80%;height: 120px">{$info['value']|default=''}</textarea>
<span class="help-block">(配置值)</span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">配置项</label>
<div class="col-lg-10">
<label class="col-sm-2 control-label">配置项</label>
<div class="col-sm-10">
<textarea name="extra" class="form-control" style="width: 80%;height: 120px">{$info['extra']|default=''}</textarea>
<span class="help-block">(如果是枚举型 需要配置该项)</span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">说明</label>
<div class="col-lg-10">
<label class="col-sm-2 control-label">说明</label>
<div class="col-sm-10">
<textarea name="remark" class="form-control" style="width: 80%;height: 120px">{$info['remark']|default=''}</textarea>
<span class="help-block">(配置详细说明)</span>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<div class="col-sm-offset-2 col-sm-10">
<input type="hidden" name="id" value="{$info.id|default=''}">
<button class="btn btn-success submit-btn ajax-post" type="submit" target-form="form-horizontal">确 定</button>
<button class="btn btn-danger btn-return" onclick="javascript:history.back(-1);return false;">返 回</button>

View File

@@ -47,15 +47,15 @@
{notempty name="list"}
{volist name="list" id="config"}
<tr>
<td><input class="ids row-selected" type="checkbox" name="id[]" value="{$config.id}"></td>
<td><input class="ids row-selected" type="checkbox" name="id[]" value="{$config['id']}"></td>
<td>{$config.id}</td>
<td><a href="{:url('edit?id='.$config['id'])}">{$config.name}</a></td>
<td>{$config.title}</td>
<td><a href="{:url('/admin/config/edit', ['id' => $config['id']])}">{$config['name']}</a></td>
<td>{$config['title']}</td>
<td>{$group[$config['group']]|default=''}</td>
<td>{$config['type_text']}</td>
<td>
<a title="编辑" href="{:url('edit?id='.$config['id'])}">编辑</a>
<a class="confirm ajax-get" title="删除" href="{:url('del?id='.$config['id'])}">删除</a>
<a title="编辑" href="{:url('/admin/config/edit', ['id' => $config['id']])}">编辑</a>
<a class="confirm ajax-get" title="删除" href="{:url('/admin/config/del', ['id' => $config['id']])}">删除</a>
</td>
</tr>
{/volist}

View File

@@ -10,36 +10,36 @@
<div class="main-box-body clearfix">
<form method="post" class="form form-horizontal">
<div class="form-group">
<label class="col-lg-2 control-label">标题</label>
<div class="col-lg-10">
<label class="col-sm-2 control-label">标题</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="title" value="{$info.title|default=''}" style="width: 80%">
<span class="help-block">(用于后台显示的配置标题)</span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">小图标</label>
<div class="col-lg-10">
<label class="col-sm-2 control-label">小图标</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="icon" value="{$info.icon|default=''}" style="width: 80%">
<span class="help-block">(用于显示在菜单左侧,不填则不显示)</span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">排序</label>
<div class="col-lg-10">
<label class="col-sm-2 control-label">排序</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="sort" value="{$info.sort|default=0}" style="width: 60%">
<span class="help-block">(用于分组显示的顺序)</span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">链接</label>
<div class="col-lg-10">
<label class="col-sm-2 control-label">链接</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="url" value="{$info['url']|default=''}" style="width: 80%">
<span class="help-block">U函数解析的URL或者外链</span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">上级菜单</label>
<div class="col-lg-10">
<label class="col-sm-2 control-label">上级菜单</label>
<div class="col-sm-10">
<select name="pid" class="form-control" style="width: auto;">
{volist name="Menus" id="menu"}
<option value="{$menu['id']}" {if $info['pid'] == $menu['id']}selected{/if}>{$menu.title_show|raw}</option>
@@ -49,15 +49,15 @@
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">分组</label>
<div class="col-lg-10">
<label class="col-sm-2 control-label">分组</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="group" value="{$info['group']|default=''}" style="width: 50%">
<span class="help-block">(用于左侧分组二级菜单)</span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">是否隐藏</label>
<div class="col-lg-1">
<label class="col-sm-2 control-label">是否隐藏</label>
<div class="col-sm-2">
<select name="hide" class="form-control">
<option value="0" ></option>
<option value="1" {if isset($info['hide']) && $info['hide']==1}selected{/if}>
@@ -66,8 +66,8 @@
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">仅开发者模式可见</label>
<div class="col-lg-1">
<label class="col-sm-2 control-label">仅开发者模式可见</label>
<div class="col-sm-2">
<select name="is_dev" class="form-control">
<option value="0" ></option>
<option value="1" {if isset($info['is_dev']) && $info['is_dev']==1}selected{/if}>
@@ -76,15 +76,15 @@
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">说明</label>
<div class="col-lg-10">
<label class="col-sm-2 control-label">说明</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="tip" value="{$info.tip|default=''}" style="width: 60%;">
<span class="help-block">(菜单详细说明)</span>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<div class="col-sm-offset-2 col-sm-10">
<input type="hidden" name="id" value="{$info['id']|default=''}">
<button class="btn btn-success submit-btn ajax-post" type="submit" target-form="form-horizontal">确 定</button>
<button class="btn btn-danger btn-return" onclick="javascript:history.back(-1);return false;">返 回</button>

View File

@@ -3,21 +3,21 @@
<div class="main-box clearfix">
<header class="main-box-header clearfix">
<div class="pull-left">
<h2>批量导入 [{$data['title']|default='顶级菜单'}]</h2>
<h2>批量导入 [{$menu['title']|default='顶级菜单'}]</h2>
</div>
</header>
<div class="main-box-body clearfix">
<form id="form" action="{:url('import')}" method="post" class="form form-horizontal">
<form id="form" action="{:url('/admin/menu/import')}" method="post" class="form form-horizontal">
<!-- 基础文档模型 -->
<div class="form-group">
<label class="col-lg-2 control-label">导入的内容</label>
<div class="col-lg-10">
<label class="col-sm-2 control-label">导入的内容</label>
<div class="col-sm-10">
<textarea name="tree" class="form-control" style="width:80%; height:160px;"></textarea>
<span class="help-block"><b>导入格式:</b><br/>首页|Index/index|0|分组名称<br/>更新缓存|Index/clear|0|分组名称<br/>(请按照导入格式输入)</span>
<span class="help-block"><b>导入格式:</b><br/>首页|/admin/index/index|0|分组名称<br/>更新缓存|/admin/menu/clear|0|分组名称<br/>(请按照导入格式输入)</span>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<div class="col-sm-offset-2 col-sm-10">
<input type="hidden" name="id" value="{$info.id|default=''}">
<button class="btn btn-success submit-btn ajax-post" type="submit" target-form="form-horizontal">确 定</button>
<button class="btn btn-danger btn-return" onclick="javascript:history.back(-1);return false;">返 回</button>

View File

@@ -62,13 +62,13 @@
<td>
<a href="{:url('/admin/menu/toogleDev',array('id'=>$menu['id'],'value'=>abs($menu['is_dev']-1)))}"
class="ajax-get">
{$menu.is_dev_text}
{$menu['is_dev_text']}
</a>
</td>
<td>
<a href="{:url('/admin/menu/toogleHide',array('id'=>$menu['id'],'value'=>abs($menu['hide']-1)))}"
class="ajax-get">
{$menu.hide_text}
{$menu['hide_text']}
</a>
</td>
<td>