| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- // +----------------------------------------------------------------------
- // | EasyAdmin
- // +----------------------------------------------------------------------
- // | PHP交流群: 763822524
- // +----------------------------------------------------------------------
- // | 开源协议 https://mit-license.org
- // +----------------------------------------------------------------------
- // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
- // +----------------------------------------------------------------------
- namespace app\admin\controller\system;
- use app\admin\logic\SystemMenuLogic;
- use app\common\model\SystemMenu;
- use app\common\model\SystemNode;
- use app\admin\service\TriggerService;
- use app\common\constants\MenuConstant;
- use EasyAdmin\annotation\ControllerAnnotation;
- use EasyAdmin\annotation\NodeAnotation;
- use app\common\controller\AdminController;
- use think\App;
- /**
- * Class Menu
- * @package app\admin\controller\system
- * @ControllerAnnotation(title="菜单管理",auth=true)
- */
- class Menu extends AdminController
- {
- use \app\admin\traits\Curd;
- protected $sort = [
- 'sort' => 'desc',
- 'id' => 'asc',
- ];
- public function __construct(App $app)
- {
- parent::__construct($app);
- $this->model = new SystemMenu();
- }
- /**
- * @NodeAnotation(title="列表")
- */
- public function index()
- {
- if ($this->request->isAjax()) {
- if (input('selectFields')) {
- return $this->selectList();
- }
- list($count, $list) = SystemMenuLogic::getList($this->sort);
- $data = [
- 'code' => 0,
- 'msg' => '',
- 'count' => $count,
- 'data' => $list,
- ];
- return json($data);
- }
- return $this->fetch();
- }
- /**
- * @NodeAnotation(title="添加")
- */
- public function add($id = null)
- {
- $homeId = $this->model
- ->where([
- 'pid' => MenuConstant::HOME_PID,
- ])
- ->value('id');
- if ($id == $homeId) {
- $this->error('首页不能添加子菜单');
- }
- if ($this->request->isPost()) {
- $post = $this->request->post();
- $rule = [
- 'pid|上级菜单' => 'require',
- 'title|菜单名称' => 'require',
- 'icon|菜单图标' => 'require',
- ];
- $this->validate($post, $rule);
- $result = SystemMenuLogic::add($post);
- $result === true ? $this->success('保存成功') : $this->error('保存失败');
- }
- $pidMenuList = $this->model->getPidMenuList();
- $this->assign('id', $id);
- $this->assign('pidMenuList', $pidMenuList);
- return $this->fetch();
- }
- /**
- * @NodeAnotation(title="编辑")
- */
- public function edit($id)
- {
- $row = $this->model->find($id);
- empty($row) && $this->error('数据不存在');
- if ($this->request->isPost()) {
- $post = $this->request->post();
- $rule = [
- 'pid|上级菜单' => 'require',
- 'title|菜单名称' => 'require',
- 'icon|菜单图标' => 'require',
- ];
- $this->validate($post, $rule);
- $result = SystemMenuLogic::edit($id, $post);
- $result === true ? $this->success('保存成功') : $this->error('保存失败');
- }
- $pidMenuList = $this->model->getPidMenuList();
- $this->assign([
- 'id' => $id,
- 'pidMenuList' => $pidMenuList,
- 'row' => $row,
- ]);
- return $this->fetch();
- }
- /**
- * @NodeAnotation(title="删除")
- */
- public function delete($id)
- {
- $this->checkPostRequest();
- $row = $this->model->whereIn('id', $id)->select();
- empty($row) && $this->error('数据不存在');
- try {
- $save = $row->delete();
- } catch (\Exception $e) {
- $this->error('删除失败');
- }
- if ($save) {
- TriggerService::updateMenu();
- $this->success('删除成功');
- } else {
- $this->error('删除失败');
- }
- }
- /**
- * @NodeAnotation(title="属性修改")
- */
- public function modify()
- {
- $this->checkPostRequest();
- $post = $this->request->post();
- $rule = [
- 'id|ID' => 'require',
- 'field|字段' => 'require',
- 'value|值' => 'require',
- ];
- $this->validate($post, $rule);
- $row = $this->model->find($post['id']);
- if (!$row) {
- $this->error('数据不存在');
- }
- if (!in_array($post['field'], $this->allowModifyFields)) {
- $this->error('该字段不允许修改:' . $post['field']);
- }
- $homeId = $this->model
- ->where([
- 'pid' => MenuConstant::HOME_PID,
- ])
- ->value('id');
- if ($post['id'] == $homeId && $post['field'] == 'status') {
- $this->error('首页状态不允许关闭');
- }
- try {
- $row->save([
- $post['field'] => $post['value'],
- ]);
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- TriggerService::updateMenu();
- $this->success('保存成功');
- }
- /**
- * @NodeAnotation(title="添加菜单提示")
- */
- public function getMenuTips()
- {
- $node = input('get.keywords');
- $list = SystemNode::whereLike('node', "%{$node}%")
- ->field('node,title')
- ->limit(10)
- ->select();
- return json([
- 'code' => 0,
- 'content' => $list,
- 'type' => 'success',
- ]);
- }
- }
|