Menu.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\bug\controller;
  4. use app\bug\model\AdminMenu;
  5. use app\bug\model\RoleAction;
  6. use app\bug\model\UserRole;
  7. use think\App;
  8. use think\facade\Db;
  9. use think\facade\Validate;
  10. class Menu extends Base
  11. {
  12. public function __construct(App $app)
  13. {
  14. parent::__construct($app);
  15. $this->model=new AdminMenu();
  16. }
  17. public function list()
  18. {
  19. $ACtion = RoleAction::where('role_id','=', $this->roleid)->findOrEmpty();
  20. if($ACtion->isEmpty())return success('角色无权限!');
  21. $data= $this->model->name("view_menu")->where('aid',"in",$ACtion->action_conllect)->where(['status'=>1,"cstatus"=>1,"is_display"=>1])
  22. ->order("weight desc,id asc,cweight desc,cid asc")->select();
  23. $list=[];
  24. $act=[];
  25. foreach ($data as $value){
  26. $list[$value["id"]]['menu_name']=$value['menu_name'];
  27. $list[$value["id"]]['menu_img']=$value['menu_img'];
  28. $list[$value["id"]]['menu_route']=$value['menu_route'];
  29. $list[$value["id"]]['is_display']=$value['is_display'];
  30. $list[$value["id"]]['status']=$value['status'];
  31. $temp = [];
  32. $temp['menu_name']=$value['cname'];
  33. $temp['menu_img']=$value['cmenu_img'];
  34. $temp['menu_route']=$value['cmenu_route'];
  35. $temp['menu_url']=$value['cmenu_url'];
  36. $temp['menu_url']=$value['cmenu_url'];
  37. $temp['status']=$value['cstatus'];
  38. $temp['is_private']=$value['cprivate'];
  39. $temp['is_display']=$value['cis_display'];
  40. $list[$value["id"]]['child'][$value['cid']]=$temp;
  41. $act[$value['id']][$value['cid']][]=$value['acode'];
  42. $list[$value["id"]]['child'][$value['cid']]['action']= $act[$value['id']][$value['cid']];
  43. }
  44. array_walk($list,function (&$value){
  45. $value['child']= array_values($value['child']);
  46. });
  47. return success("获取成功",array_values($list));
  48. }
  49. /**
  50. * 显示创建资源表单页.
  51. *
  52. * @return \think\Response
  53. */
  54. public function MenuEdit(){
  55. $post =$this->request->param(["id"=>"","name"=>"","url"=>"","route"=>"","img"=>"","pid"=>0,'weight'=>1,
  56. "is_display"=>0,"private"=>0],'post','trim');
  57. $valid=Validate::rule([
  58. "id|菜单id"=>"number|gt:0",
  59. "name|菜单名称"=>"require|max:255",
  60. "url|菜单地址"=>"max:255",
  61. "pid|父级id"=>"require|number",
  62. "route|菜单路由"=>"max:255"
  63. ]);
  64. if($valid->check($post)==false) return error($valid->getError());
  65. $menu = $this->model->where("id","=",$post['id'])->findOrEmpty();
  66. $message=$menu->isEmpty()?'新建':'编辑';
  67. if($post['pid']!=0 &&$post['route']=="")return error("子级菜单路由不能为空");
  68. $menu->menu_name=$post['name'];
  69. $menu->menu_url=$post['url'];
  70. $menu->menu_route=$post['route'];
  71. $menu->menu_img=$post['img'];
  72. $menu->pid=$post['pid'];
  73. $menu->is_display=$post['is_display'];
  74. $menu->is_private=$post['private'];
  75. $menu->weight=$post['weight'];
  76. $menu->status=1;
  77. $result =$menu->save();
  78. return $result ? success("{$message}成功"): error("{$message}失败");
  79. }
  80. /**
  81. * 保存新建的资源
  82. *
  83. * @param \think\Request $request
  84. * @return \think\Response
  85. */
  86. public function MenuStatus(){
  87. $post =$this->request->param(['id'=>'','status'=>1],"post","trim");
  88. $valid=Validate::rule([
  89. 'id|菜单id'=>'require|number|gt:0',
  90. 'status|菜单状态'=>'require|number|in:0,1'
  91. ]);
  92. if($valid->check($post)==false) return error($valid->getError());
  93. $menu = $this->model->where('id','=',$post['id'])->findOrEmpty();
  94. if($menu->isEmpty()==false)return error("菜单信息不存在");
  95. $menu->status=$post['status'];
  96. $result =$menu->save();
  97. return $result ? success("{$this->model->status_cn[$post['status']]}成功"): error("{$this->model->status_cn[$post['status']]}失败");
  98. }
  99. /**所有菜单
  100. */
  101. public function MenuAllList(){
  102. return success("获取成功",$this->model->GetMenuTree());
  103. }
  104. /**删除菜单
  105. * @return \think\response\Json|void
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. * @throws \think\exception\DbException
  110. */
  111. public function MenuDel(){
  112. $id =$this->request->post("id/d");
  113. $menu = $this->model->where("id","=",$id)->findOrEmpty();
  114. if($menu->isEmpty())return error("菜单不信息不存在");
  115. $menu->is_del=1;
  116. $result =$menu->save();
  117. return $result ? success("删除成功"): error("删除失败");
  118. }
  119. /**删除菜单功能
  120. * @return \think\response\Json|void
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\DbException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. */
  125. public function MenuActionDel(){
  126. $id =$this->request->post('id/d');
  127. $menu = \app\bug\model\Action::where("id","=",$id)->findOrEmpty();
  128. if($menu->isEmpty())return error('菜单功能信息不存在');
  129. $result =$menu->delete();
  130. return $result ? success('删除成功'): error('删除失败');
  131. }
  132. }