Menu.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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])
  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. $token = isset($post['token']) ? trim($post['token']) : "";
  79. if($token==""){
  80. return error_show(101,'token不能为空');
  81. }
  82. $effetc = VerifyTokens($token);
  83. if(!empty($effetc) && $effetc['code']!=0){
  84. return error_show($effetc['code'],$effetc['message']);
  85. }
  86. $id = isset($post['id']) ?intval($post['id']) :"";
  87. if($id!=""){
  88. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  89. if($menu==false){
  90. return error_show(1003,"菜单不信息不存在");
  91. }
  92. }
  93. $name = isset($post['name']) ?trim($post['name']) :"";
  94. if($name==""){
  95. return error_show(1002,"菜单名称不能为空");
  96. }
  97. $url = isset($post['url']) ?trim($post['url']) :"";
  98. $route = isset($post['route']) ?trim($post['route']) :"";
  99. $code = isset($post['menu_code']) ?trim($post['menu_code']) :"";
  100. $img = isset($post['img']) ?trim($post['img']) :"";
  101. $pid = isset($post['pid']) ?intval($post['pid']) :0;
  102. $weight = isset($post['weight']) ?floatval($post['weight']) :1;
  103. $status = isset($post['is_show']) ? intval($post['is_show']) : 0;
  104. $private = isset($post['private']) ?intval($post['private']) :(isset($menu['is_private']) ?
  105. $menu['is_private'] : 0);
  106. if($pid!=0 && $route==""){
  107. return error_show(1002,"子级菜单路由不能为空");
  108. }
  109. $data=[
  110. "menu_name"=>$name,
  111. "menu_url"=>$url,
  112. "menu_route"=>$route,
  113. "menu_code"=>$code,
  114. "menu_img"=>$img,
  115. "pid"=>$pid,
  116. 'is_show'=>$status,
  117. "is_private"=>$private,
  118. 'status'=>1,
  119. "weight"=>$weight,
  120. "updatetime"=>date("Y-m-d H:i:s"),
  121. ];
  122. $message="新建";
  123. if($id!=""){
  124. $message="编辑";
  125. $data['id']=$id;
  126. }
  127. $result = Db::name("admin_menu")->save($data);
  128. return $result ? app_show(0,"{$message}成功"): error_show(1003,"{$message}失败");
  129. }
  130. /**
  131. * 保存新建的资源
  132. *
  133. * @param \think\Request $request
  134. * @return \think\Response
  135. */
  136. public function MenuStatus(){
  137. $post =$this->request->post();
  138. $token = isset($post['token']) ? trim($post['token']) : "";
  139. if($token==""){
  140. return error_show(101,'token不能为空');
  141. }
  142. $effetc = VerifyTokens($token);
  143. if(!empty($effetc) && $effetc['code']!=0){
  144. return error_show($effetc['code'],$effetc['message']);
  145. }
  146. $id = isset($post['id']) ?intval($post['id']) :"";
  147. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  148. if($menu==false){
  149. return error_show(1003,"菜单信息不存在");
  150. }
  151. $statu = isset($post['status'])&&$post['status']!="" ? intval($post['status']) :"";
  152. if($statu===""){
  153. return error_show(1003,"菜单状态不能为空");
  154. }
  155. $menu['status']=$statu;
  156. $menu['updatetime']=date("Y-m-d H:i:s");
  157. $result = Db::name("admin_menu")->save($menu);
  158. return $result ? app_show(0,"状态更新成功"): error_show(1003,"状态更新失败");
  159. }
  160. /**
  161. * @return \think\response\Json|void
  162. * @throws \think\db\exception\DataNotFoundException
  163. * @throws \think\db\exception\DbException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. * @throws \think\exception\DbException
  166. */
  167. public function MenuAllList(){
  168. $post =$this->request->post();
  169. $token = isset($post['token']) ? trim($post['token']) : "";
  170. if($token==""){
  171. return error_show(101,'token不能为空');
  172. }
  173. $effetc = VerifyTokens($token);
  174. if(!empty($effetc) && $effetc['code']!=0){
  175. return error_show($effetc['code'],$effetc['message']);
  176. }
  177. $data = Db::name("admin_menu")->where(['pid'=>0,'is_del'=>0])->order("weight desc,id asc")->select();
  178. $l=[];
  179. foreach ($data as $key=>$value){
  180. $temp=[];
  181. $temp = Db::name("admin_menu")->where(['pid'=>$value['id'],'is_del'=>0])->order("weight desc,id asc")
  182. ->select();
  183. $value['child']=$temp;
  184. $l[]=$value;
  185. }
  186. return app_show(0,"获取成功",$l);
  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. * @throws \think\exception\DbException
  194. */
  195. public function MenuDel(){
  196. $post =$this->request->post();
  197. $token = isset($post['token']) ? trim($post['token']) : "";
  198. if($token==""){
  199. return error_show(101,'token不能为空');
  200. }
  201. $effetc = VerifyTokens($token);
  202. if(!empty($effetc) && $effetc['code']!=0){
  203. return error_show($effetc['code'],$effetc['message']);
  204. }
  205. $id = isset($post['id']) ?intval($post['id']) :"";
  206. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  207. if($menu==false){
  208. return error_show(1003,"菜单不信息不存在");
  209. }
  210. $menu['is_show']=0;
  211. $menu['status']=0;
  212. $menu['is_del']=1;
  213. $menu['updatetime']=date("Y-m-d H:i:s");
  214. $result = Db::name("admin_menu")->save($menu);
  215. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  216. }
  217. }