修复配置管理以及菜单管理,增加验证
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user