Menu.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\BaseController;
  5. use think\App;
  6. use think\facade\Db;
  7. use think\Request;
  8. class Menu extends BaseController
  9. {
  10. private $uid = '';
  11. /**
  12. * 显示资源列表
  13. *
  14. * @return \think\Response
  15. */
  16. public function __construct(App $app)
  17. {
  18. parent::__construct($app);
  19. $post =$this->request->post();
  20. $token = isset($post['token']) ? trim($post['token']) : "";
  21. if($token==""){
  22. return error_show(101,'token不能为空');
  23. }
  24. $effetc = VerifyTokens($token);
  25. if(!empty($effetc) && $effetc['code']!=0){
  26. return error_show($effetc['code'],$effetc['message']);
  27. }
  28. $this->uid = isset($effetc['data']['user']['id']) ? $effetc['data']['user']['id'] :"";
  29. }
  30. public function list()
  31. {
  32. $post=$this->request->post();
  33. $uid = $this->uid;
  34. if($uid==''){
  35. return error_show(101,'未找到用户信息');
  36. }
  37. $roleid = Db::name("user_role")->where([["uid","=",$uid],["status","=",1],["is_del","=",0]])->find();
  38. if(empty($roleid)){
  39. return error_show(1004,'未找到用户权限信息');
  40. }
  41. $role = Db::name("role_action")->where("role_id","=", $roleid['roleid'])->find();
  42. if($role==false){
  43. return app_show(0,"获取成功",[]);
  44. }
  45. $data= Db::name("view_menu")->where('aid',"in",explode(",",$role['action_conllect']))->where(['status'=>1,"cstatus"=>1,"is_display"=>1])
  46. ->order("weight desc,id asc,cweight desc,cid asc")->select();
  47. $list=[];
  48. $act=[];
  49. foreach ($data as $value){
  50. $list[$value["id"]]['menu_name']=$value['menu_name'];
  51. $list[$value["id"]]['menu_img']=$value['menu_img'];
  52. $list[$value["id"]]['menu_route']=$value['menu_route'];
  53. $list[$value["id"]]['is_display']=$value['is_display'];
  54. $list[$value["id"]]['status']=$value['status'];
  55. $temp = [];
  56. $temp['menu_name']=$value['cname'];
  57. $temp['menu_img']=$value['cmenu_img'];
  58. $temp['menu_route']=$value['cmenu_route'];
  59. $temp['menu_url']=$value['cmenu_url'];
  60. $temp['menu_url']=$value['cmenu_url'];
  61. $temp['status']=$value['cstatus'];
  62. $temp['is_private']=$value['cprivate'];
  63. $list[$value["id"]]['child'][$value['cid']]=$temp;
  64. $act[$value['id']][$value['cid']][]=$value['acode'];
  65. $list[$value["id"]]['child'][$value['cid']]['action']= $act[$value['id']][$value['cid']];
  66. }
  67. array_walk($list,function (&$value){
  68. $value['child']= array_values($value['child']);
  69. });
  70. return app_show(0,"获取成功",array_values($list));
  71. }
  72. /**
  73. * 显示创建资源表单页.
  74. *
  75. * @return \think\Response
  76. */
  77. public function MenuEdit(){
  78. $post =$this->request->post();
  79. $id = isset($post['id']) ?intval($post['id']) :"";
  80. if($id!=""){
  81. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  82. if($menu==false){
  83. return error_show(1003,"菜单不信息不存在");
  84. }
  85. }
  86. $name = isset($post['name']) ?trim($post['name']) :"";
  87. if($name==""){
  88. return error_show(1002,"菜单名称不能为空");
  89. }
  90. $url = isset($post['url']) ?trim($post['url']) :"";
  91. $route = isset($post['route']) ?trim($post['route']) :"";
  92. $code = isset($post['menu_code']) ?trim($post['menu_code']) :"";
  93. $img = isset($post['img']) ?trim($post['img']) :"";
  94. $pid = isset($post['pid']) ?intval($post['pid']) :0;
  95. $weight = isset($post['weight']) ?floatval($post['weight']) :1;
  96. $status = isset($post['is_show']) ? intval($post['is_show']) : 0;
  97. $is_display = isset($post['is_display']) ? intval($post['is_display']) : 0;
  98. $private = isset($post['private']) ?intval($post['private']) :(isset($menu['is_private']) ?
  99. $menu['is_private'] : 0);
  100. if($pid!=0 && $route==""){
  101. return error_show(1002,"子级菜单路由不能为空");
  102. }
  103. $data=[
  104. "menu_name"=>$name,
  105. "menu_url"=>$url,
  106. "menu_route"=>$route,
  107. "menu_code"=>$code,
  108. "menu_img"=>$img,
  109. "pid"=>$pid,
  110. 'is_show'=>$status,
  111. 'is_display'=>$is_display,
  112. "is_private"=>$private,
  113. 'status'=>1,
  114. "weight"=>$weight,
  115. "updatetime"=>date("Y-m-d H:i:s"),
  116. ];
  117. $message="新建";
  118. if($id!=""){
  119. $message="编辑";
  120. $data['id']=$id;
  121. }
  122. $result = Db::name("admin_menu")->save($data);
  123. return $result ? app_show(0,"{$message}成功"): error_show(1003,"{$message}失败");
  124. }
  125. /**
  126. * 保存新建的资源
  127. *
  128. * @param \think\Request $request
  129. * @return \think\Response
  130. */
  131. public function MenuStatus(){
  132. $post =$this->request->post();
  133. $id = isset($post['id']) ?intval($post['id']) :"";
  134. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  135. if($menu==false){
  136. return error_show(1003,"菜单信息不存在");
  137. }
  138. $statu = isset($post['status'])&&$post['status']!="" ? intval($post['status']) :"";
  139. if($statu===""){
  140. return error_show(1003,"菜单状态不能为空");
  141. }
  142. $menu['status']=$statu;
  143. $menu['updatetime']=date("Y-m-d H:i:s");
  144. $result = Db::name("admin_menu")->save($menu);
  145. return $result ? app_show(0,"状态更新成功"): error_show(1003,"状态更新失败");
  146. }
  147. /**
  148. * @return \think\response\Json|void
  149. * @throws \think\db\exception\DataNotFoundException
  150. * @throws \think\db\exception\DbException
  151. * @throws \think\db\exception\ModelNotFoundException
  152. * @throws \think\exception\DbException
  153. */
  154. public function MenuAllList(){
  155. $post =$this->request->post();
  156. $data = Db::name("admin_menu")->where(['pid'=>0,'is_del'=>0])->order("weight desc,id asc")->select();
  157. $l=[];
  158. foreach ($data as $key=>$value){
  159. $temp=[];
  160. $temp = Db::name("admin_menu")->where(['pid'=>$value['id'],'is_del'=>0])->order("weight desc,id asc")
  161. ->select();
  162. $value['child']=$temp;
  163. $l[]=$value;
  164. }
  165. return app_show(0,"获取成功",$l);
  166. }
  167. /**
  168. * @return \think\response\Json|void
  169. * @throws \think\db\exception\DataNotFoundException
  170. * @throws \think\db\exception\DbException
  171. * @throws \think\db\exception\ModelNotFoundException
  172. * @throws \think\exception\DbException
  173. */
  174. public function MenuDel(){
  175. $post =$this->request->post();
  176. $id = isset($post['id']) ?intval($post['id']) :"";
  177. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  178. if($menu==false){
  179. return error_show(1003,"菜单不信息不存在");
  180. }
  181. $menu['is_show']=0;
  182. $menu['status']=0;
  183. $menu['is_del']=1;
  184. $menu['updatetime']=date("Y-m-d H:i:s");
  185. $result = Db::name("admin_menu")->save($menu);
  186. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  187. }
  188. /**
  189. * @return \think\response\Json|void
  190. * @throws \think\db\exception\DataNotFoundException
  191. * @throws \think\db\exception\DbException
  192. * @throws \think\db\exception\ModelNotFoundException
  193. */
  194. public function MenuActionDel(){
  195. $post =$this->request->post();
  196. $id = isset($post['id']) ?intval($post['id']) :"";
  197. $menu = Db::name("action")->where("id","=",$id)->find();
  198. if($menu==false){
  199. return error_show(1003,"菜单功能信息不存在");
  200. }
  201. $result = Db::name("action")->delete($menu);
  202. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  203. }
  204. }