diff --git a/app/controller/Admin.php b/app/controller/Admin.php index a929567c..af3609b9 100755 --- a/app/controller/Admin.php +++ b/app/controller/Admin.php @@ -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(); diff --git a/app/controller/admin/Config.php b/app/controller/admin/Config.php index 76264320..33e3b7f5 100644 --- a/app/controller/admin/Config.php +++ b/app/controller/admin/Config.php @@ -6,10 +6,11 @@ // +---------------------------------------------------------------------- // | Author: molong // +---------------------------------------------------------------------- - 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(); } } diff --git a/app/controller/admin/Menu.php b/app/controller/admin/Menu.php index 4f47031f..77214504 100644 --- a/app/controller/admin/Menu.php +++ b/app/controller/admin/Menu.php @@ -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 */ 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 + */ 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 - */ - 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(); } } } \ No newline at end of file diff --git a/app/http/validate/admin/Config.php b/app/http/validate/admin/Config.php new file mode 100644 index 00000000..8de14b64 --- /dev/null +++ b/app/http/validate/admin/Config.php @@ -0,0 +1,37 @@ + +// +---------------------------------------------------------------------- +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'); + } +} \ No newline at end of file diff --git a/app/http/validate/admin/Menu.php b/app/http/validate/admin/Menu.php new file mode 100644 index 00000000..7d555623 --- /dev/null +++ b/app/http/validate/admin/Menu.php @@ -0,0 +1,29 @@ + +// +---------------------------------------------------------------------- +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'], + ]; +} \ No newline at end of file diff --git a/app/model/Menu.php b/app/model/Menu.php index b6e70380..ccba1e96 100644 --- a/app/model/Menu.php +++ b/app/model/Menu.php @@ -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') { diff --git a/view/admin/config/edit.html b/view/admin/config/edit.html index b5025206..72cdb3a8 100644 --- a/view/admin/config/edit.html +++ b/view/admin/config/edit.html @@ -10,40 +10,40 @@
- -
+ +
(用于config函数调用,只能使用英文且不能重复)
- -
+ +
(用于后台显示的配置标题)
- -
+ +
(用于分组显示的顺序)
- -
+ +
(系统会根据不同类型解析配置值)
- -
+ +
(配置值)
- -
+ +
(如果是枚举型 需要配置该项)
- -
+ +
(配置详细说明)
-
+
diff --git a/view/admin/config/index.html b/view/admin/config/index.html index 35a8d8c3..05f9ad4c 100644 --- a/view/admin/config/index.html +++ b/view/admin/config/index.html @@ -47,15 +47,15 @@ {notempty name="list"} {volist name="list" id="config"} - + {$config.id} - {$config.name} - {$config.title} + {$config['name']} + {$config['title']} {$group[$config['group']]|default=''} {$config['type_text']} - 编辑 - 删除 + 编辑 + 删除 {/volist} diff --git a/view/admin/menu/edit.html b/view/admin/menu/edit.html index 165e7f04..d2d1520f 100644 --- a/view/admin/menu/edit.html +++ b/view/admin/menu/edit.html @@ -10,36 +10,36 @@
- -
+ +
(用于后台显示的配置标题)
- -
+ +
(用于显示在菜单左侧,不填则不显示)
- -
+ +
(用于分组显示的顺序)
- -
+ +
(U函数解析的URL或者外链)
- -
+ +
(用于左侧分组二级菜单)
- -
+ +
- -
+ +
(菜单详细说明)
-
+
diff --git a/view/admin/menu/import.html b/view/admin/menu/import.html index c8bd3261..9904e831 100644 --- a/view/admin/menu/import.html +++ b/view/admin/menu/import.html @@ -3,21 +3,21 @@
-

批量导入 [{$data['title']|default='顶级菜单'}]

+

批量导入 [{$menu['title']|default='顶级菜单'}]

- +
- -
+ +
- 导入格式:
首页|Index/index|0|分组名称
更新缓存|Index/clear|0|分组名称
(请按照导入格式输入)
+ 导入格式:
首页|/admin/index/index|0|分组名称
更新缓存|/admin/menu/clear|0|分组名称
(请按照导入格式输入)
-
+
diff --git a/view/admin/menu/index.html b/view/admin/menu/index.html index 2b5ecc79..b726121f 100644 --- a/view/admin/menu/index.html +++ b/view/admin/menu/index.html @@ -62,13 +62,13 @@ - {$menu.is_dev_text} + {$menu['is_dev_text']} - {$menu.hide_text} + {$menu['hide_text']}