Menu.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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"]]['status']=$value['status'];
  54. $temp = [];
  55. $temp['menu_name']=$value['cname'];
  56. $temp['menu_img']=$value['cmenu_img'];
  57. $temp['menu_route']=$value['cmenu_route'];
  58. $temp['menu_url']=$value['cmenu_url'];
  59. $temp['menu_url']=$value['cmenu_url'];
  60. $temp['status']=$value['cstatus'];
  61. $temp['is_private']=$value['cprivate'];
  62. $list[$value["id"]]['child'][$value['cid']]=$temp;
  63. $act[$value['id']][$value['cid']][]=$value['acode'];
  64. $list[$value["id"]]['child'][$value['cid']]['action']= $act[$value['id']][$value['cid']];
  65. }
  66. array_walk($list,function (&$value){
  67. $value['child']= array_values($value['child']);
  68. });
  69. return app_show(0,"获取成功",array_values($list));
  70. }
  71. /**
  72. * 显示创建资源表单页.
  73. *
  74. * @return \think\Response
  75. */
  76. public function MenuEdit(){
  77. $post =$this->request->post();
  78. $id = isset($post['id']) ?intval($post['id']) :"";
  79. if($id!=""){
  80. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  81. if($menu==false){
  82. return error_show(1003,"菜单不信息不存在");
  83. }
  84. }
  85. $name = isset($post['name']) ?trim($post['name']) :"";
  86. if($name==""){
  87. return error_show(1002,"菜单名称不能为空");
  88. }
  89. $url = isset($post['url']) ?trim($post['url']) :"";
  90. $route = isset($post['route']) ?trim($post['route']) :"";
  91. $code = isset($post['menu_code']) ?trim($post['menu_code']) :"";
  92. $img = isset($post['img']) ?trim($post['img']) :"";
  93. $pid = isset($post['pid']) ?intval($post['pid']) :0;
  94. $weight = isset($post['weight']) ?floatval($post['weight']) :1;
  95. $status = isset($post['is_show']) ? intval($post['is_show']) : 0;
  96. $is_display = isset($post['is_display']) ? intval($post['is_display']) : 0;
  97. $private = isset($post['private']) ?intval($post['private']) :(isset($menu['is_private']) ?
  98. $menu['is_private'] : 0);
  99. if($pid!=0 && $route==""){
  100. return error_show(1002,"子级菜单路由不能为空");
  101. }
  102. $data=[
  103. "menu_name"=>$name,
  104. "menu_url"=>$url,
  105. "menu_route"=>$route,
  106. "menu_code"=>$code,
  107. "menu_img"=>$img,
  108. "pid"=>$pid,
  109. 'is_show'=>$status,
  110. 'is_display'=>$is_display,
  111. "is_private"=>$private,
  112. 'status'=>1,
  113. "weight"=>$weight,
  114. "updatetime"=>date("Y-m-d H:i:s"),
  115. ];
  116. $message="新建";
  117. if($id!=""){
  118. $message="编辑";
  119. $data['id']=$id;
  120. }
  121. $result = Db::name("admin_menu")->save($data);
  122. return $result ? app_show(0,"{$message}成功"): error_show(1003,"{$message}失败");
  123. }
  124. /**
  125. * 保存新建的资源
  126. *
  127. * @param \think\Request $request
  128. * @return \think\Response
  129. */
  130. public function MenuStatus(){
  131. $post =$this->request->post();
  132. $id = isset($post['id']) ?intval($post['id']) :"";
  133. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  134. if($menu==false){
  135. return error_show(1003,"菜单信息不存在");
  136. }
  137. $statu = isset($post['status'])&&$post['status']!="" ? intval($post['status']) :"";
  138. if($statu===""){
  139. return error_show(1003,"菜单状态不能为空");
  140. }
  141. $menu['status']=$statu;
  142. $menu['updatetime']=date("Y-m-d H:i:s");
  143. $result = Db::name("admin_menu")->save($menu);
  144. return $result ? app_show(0,"状态更新成功"): error_show(1003,"状态更新失败");
  145. }
  146. /**
  147. * @return \think\response\Json|void
  148. * @throws \think\db\exception\DataNotFoundException
  149. * @throws \think\db\exception\DbException
  150. * @throws \think\db\exception\ModelNotFoundException
  151. * @throws \think\exception\DbException
  152. */
  153. public function MenuAllList(){
  154. $post =$this->request->post();
  155. $data = Db::name("admin_menu")->where(['pid'=>0,'is_del'=>0])->order("weight desc,id asc")->select();
  156. $l=[];
  157. foreach ($data as $key=>$value){
  158. $temp=[];
  159. $temp = Db::name("admin_menu")->where(['pid'=>$value['id'],'is_del'=>0])->order("weight desc,id asc")
  160. ->select();
  161. $value['child']=$temp;
  162. $l[]=$value;
  163. }
  164. return app_show(0,"获取成功",$l);
  165. }
  166. /**
  167. * @return \think\response\Json|void
  168. * @throws \think\db\exception\DataNotFoundException
  169. * @throws \think\db\exception\DbException
  170. * @throws \think\db\exception\ModelNotFoundException
  171. * @throws \think\exception\DbException
  172. */
  173. public function MenuDel(){
  174. $post =$this->request->post();
  175. $id = isset($post['id']) ?intval($post['id']) :"";
  176. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  177. if($menu==false){
  178. return error_show(1003,"菜单不信息不存在");
  179. }
  180. $menu['is_show']=0;
  181. $menu['status']=0;
  182. $menu['is_del']=1;
  183. $menu['updatetime']=date("Y-m-d H:i:s");
  184. $result = Db::name("admin_menu")->save($menu);
  185. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  186. }
  187. /**
  188. * @return \think\response\Json|void
  189. * @throws \think\db\exception\DataNotFoundException
  190. * @throws \think\db\exception\DbException
  191. * @throws \think\db\exception\ModelNotFoundException
  192. */
  193. public function MenuActionDel(){
  194. $post =$this->request->post();
  195. $id = isset($post['id']) ?intval($post['id']) :"";
  196. $menu = Db::name("action")->where("id","=",$id)->find();
  197. if($menu==false){
  198. return error_show(1003,"菜单功能信息不存在");
  199. }
  200. $result = Db::name("action")->delete($menu);
  201. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  202. }
  203. }