Menu.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\admin\model\ActionLog;
  5. use think\App;
  6. use think\facade\Db;
  7. //菜单
  8. class Menu extends Base
  9. {
  10. /**
  11. * 显示资源列表
  12. * @return \think\Response
  13. */
  14. public function __construct(App $app)
  15. {
  16. parent::__construct($app);
  17. // $this->post =$this->request->post();
  18. }
  19. // public function list()
  20. // {
  21. // $uid = $this->uid;
  22. // if($uid==''){
  23. // return error_show(101,'未找到用户信息');
  24. // }
  25. // $role =$this->checkRole();
  26. //// var_dump($role);
  27. // if(empty($role)){
  28. // return app_show(0,"获取成功",[]);
  29. // }
  30. // $data= Db::name("view_menu")->where('aid',"in",explode(",",$role['action_conllect']))->where(['status'=>1,"cstatus"=>1,"is_display"=>1])
  31. // ->order("weight desc,id asc,cweight desc,cid asc")->select();
  32. // $list=[];
  33. // $act=[];
  34. // foreach ($data as $value){
  35. // $list[$value["id"]]['menu_name']=$value['menu_name'];
  36. // $list[$value["id"]]['menu_img']=$value['menu_img'];
  37. // $list[$value["id"]]['menu_route']=$value['menu_route'];
  38. // $list[$value["id"]]['is_display']=$value['is_display'];
  39. // $list[$value["id"]]['status']=$value['status'];
  40. // $temp=[];
  41. // $temp['menu_name']=$value['cname'];
  42. // $temp['menu_img']=$value['cmenu_img'];
  43. // $temp['menu_route']=$value['cmenu_route'];
  44. // $temp['menu_url']=$value['cmenu_url'];
  45. // $temp['status']=$value['cstatus'];
  46. // $temp['is_private']=$value['cprivate'];
  47. // $temp['is_display']=$value['cis_display'];
  48. // $list[$value["id"]]['child'][$value['cid']]=$temp;
  49. // $act[$value['id']][$value['cid']][]=$value['acode'];
  50. // $list[$value["id"]]['child'][$value['cid']]['action']= $act[$value['id']][$value['cid']];
  51. // }
  52. // array_walk($list,function (&$value){
  53. // $value['child']= array_values($value['child']);
  54. // });
  55. // return app_show(0,"获取成功",array_values($list));
  56. // }
  57. public function MenuList(){
  58. $condition =["status"=>1,"is_del"=>0];
  59. if($this->level=='') return json_show(10000,"账户角色账户已禁用",[]);
  60. if ($this->level!=1){
  61. $role = Db::name("role_action")->where("role_id","=",$this->roleid)->find();
  62. if($role==false) return json_show(0,"获取成功",[]);
  63. $action = Db::name("action")
  64. ->where(['id'=>explode(",",$role['action_conllect']),"status"=>1,"is_del"=>0])
  65. ->column("id,menuid,action_code");
  66. if (empty($action)) return json_show(0,"获取成功",[]);
  67. $MenuAction=[];
  68. foreach ($action as $value){
  69. $MenuAction[$value['menuid']][]=$value['action_code'];
  70. }
  71. $menuid= array_column($action,"menuid");
  72. $condition['id']=$menuid;
  73. }else{
  74. $action = Db::name("action")->where(["status"=>1,"is_del"=>0])->column("id,action_code,menuid");
  75. $MenuAction=[];
  76. foreach ($action as $value){
  77. $MenuAction[$value['menuid']][]=$value['action_code'];
  78. }
  79. $menuid= array_column($action,"menuid");
  80. $condition['id']=$menuid;
  81. }
  82. $menuAll =Db::name("admin_menu")
  83. ->where($condition)
  84. ->where("level",">=",$this->level)
  85. ->column("id,menu_name,menu_img,menu_route,menu_url,pid,is_show,is_private,menu_type,level,status,weight");
  86. $list=[];
  87. foreach ($menuAll as $value){
  88. $value['action']=$MenuAction[$value['id']]??[];
  89. makeMenu($value,$list);
  90. }
  91. $keys =array_column($list, 'weight');
  92. array_multisort($keys,SORT_DESC,$list);
  93. $list=MenuTree($list,0);
  94. return json_show(0,"获取成功",$list);
  95. }
  96. /**
  97. * 显示创建资源表单页.
  98. *
  99. * @return \think\Response
  100. */
  101. public function MenuEdit(){
  102. $post =$this->request->post();
  103. $token = isset($post['token']) ? trim($post['token']) : "";
  104. if($token==""){
  105. return error_show(101,'token不能为空');
  106. }
  107. $effetc = VerifyTokens($token);
  108. if(!empty($effetc) && $effetc['code']!=0){
  109. return error_show($effetc['code'],$effetc['message']);
  110. }
  111. $id = isset($post['id']) ?intval($post['id']) :"";
  112. if($id!=""){
  113. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  114. if($menu==false){
  115. return error_show(1003,"菜单不信息不存在");
  116. }
  117. }
  118. $name = isset($post['name']) ?trim($post['name']) :"";
  119. if($name==""){
  120. return error_show(1002,"菜单名称不能为空");
  121. }
  122. $url = isset($post['url']) ?trim($post['url']) :"";
  123. $route = isset($post['route']) ?trim($post['route']) :"";
  124. $code = isset($post['menu_code']) ?trim($post['menu_code']) :"";
  125. $img = isset($post['img']) ?trim($post['img']) :"";
  126. $pid = isset($post['pid']) ?intval($post['pid']) :0;
  127. $weight = isset($post['weight']) ?floatval($post['weight']) :1;
  128. $status = isset($post['is_show']) ? intval($post['is_show']) : 0;
  129. $is_display = isset($post['is_display']) ? intval($post['is_display']) : 0;
  130. $private = isset($post['private']) ?intval($post['private']) :(isset($menu['is_private']) ?$menu['is_private'] : 0);
  131. if($pid!=0 && $route==""){
  132. return error_show(1002,"子级菜单路由不能为空");
  133. }
  134. $data=[
  135. "menu_name"=>$name,
  136. "menu_url"=>$url,
  137. "menu_route"=>$route,
  138. "menu_code"=>$code,
  139. "menu_img"=>$img,
  140. "pid"=>$pid,
  141. 'is_show'=>$status,
  142. 'is_display'=>$is_display,
  143. "is_private"=>$private,
  144. 'status'=>1,
  145. "weight"=>$weight,
  146. "updatetime"=>date("Y-m-d H:i:s"),
  147. ];
  148. $message="新建";
  149. if($id!=""){
  150. $message="编辑";
  151. $data['id']=$id;
  152. }
  153. $result = Db::name("admin_menu")->save($data);
  154. $orde = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  155. ActionLog::logAdd($token,$orde,"admin_menu",0,$orde);
  156. return $result ? app_show(0,"{$message}成功"): error_show(1003,"{$message}失败");
  157. }
  158. /**
  159. * 保存新建的资源
  160. *
  161. * @param \think\Request $request
  162. * @return \think\Response
  163. */
  164. public function MenuStatus(){
  165. $post =$this->request->post();
  166. $token = isset($post['token']) ? trim($post['token']) : "";
  167. if($token==""){
  168. return error_show(101,'token不能为空');
  169. }
  170. $effetc = VerifyTokens($token);
  171. if(!empty($effetc) && $effetc['code']!=0){
  172. return error_show($effetc['code'],$effetc['message']);
  173. }
  174. $id = isset($post['id']) ?intval($post['id']) :"";
  175. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  176. if($menu==false){
  177. return error_show(1003,"菜单信息不存在");
  178. }
  179. $statu = isset($post['status'])&&$post['status']!="" ? intval($post['status']) :"";
  180. if($statu===""){
  181. return error_show(1003,"菜单状态不能为空");
  182. }
  183. $var= $menu['status'];
  184. $menu['status']=$statu;
  185. $menu['updatetime']=date("Y-m-d H:i:s");
  186. $result = Db::name("admin_menu")->save($menu);
  187. $orde = ["order_code"=>$menu['menu_name'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  188. ActionLog::logAdd($token,$orde,"admin_menu", $menu['status'],$orde);
  189. return $result ? app_show(0,"状态更新成功"): error_show(1003,"状态更新失败");
  190. }
  191. /**
  192. * @return \think\response\Json|void
  193. * @throws \think\db\exception\DataNotFoundException
  194. * @throws \think\db\exception\DbException
  195. * @throws \think\db\exception\ModelNotFoundException
  196. * @throws \think\exception\DbException
  197. */
  198. public function MenuAllList(){
  199. $post =$this->request->post();
  200. $data = Db::name("admin_menu")->where(['pid'=>0,'is_del'=>0])->order("weight desc,id asc")->select();
  201. $l=[];
  202. foreach ($data as $key=>$value){
  203. $temp=[];
  204. $temp = Db::name("admin_menu")->where(['pid'=>$value['id'],'is_del'=>0])->order("weight desc,id asc")
  205. ->select();
  206. $value['child']=$temp;
  207. $l[]=$value;
  208. }
  209. return app_show(0,"获取成功",$l);
  210. }
  211. /**
  212. * @return \think\response\Json|void
  213. * @throws \think\db\exception\DataNotFoundException
  214. * @throws \think\db\exception\DbException
  215. * @throws \think\db\exception\ModelNotFoundException
  216. * @throws \think\exception\DbException
  217. */
  218. public function MenuDel(){
  219. $post =$this->request->post();
  220. $token = isset($post['token']) ? trim($post['token']) : "";
  221. if($token==""){
  222. return error_show(101,'token不能为空');
  223. }
  224. $effetc = VerifyTokens($token);
  225. if(!empty($effetc) && $effetc['code']!=0){
  226. return error_show($effetc['code'],$effetc['message']);
  227. }
  228. $id = isset($post['id']) ?intval($post['id']) :"";
  229. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  230. if($menu==false){
  231. return error_show(1003,"菜单不信息不存在");
  232. }
  233. $var=$menu['status'];
  234. $menu['is_show']=0;
  235. $menu['status']=0;
  236. $menu['is_del']=1;
  237. $menu['updatetime']=date("Y-m-d H:i:s");
  238. $result = Db::name("admin_menu")->save($menu);
  239. $orde = ["order_code"=>$menu['menu_name'],"status"=>$var,"action_remark"=>'',"action_type"=>"delete"];
  240. ActionLog::logAdd($token,$orde,"admin_menu", $menu['status'],$orde);
  241. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  242. }
  243. /**
  244. * @return \think\response\Json|void
  245. * @throws \think\db\exception\DataNotFoundException
  246. * @throws \think\db\exception\DbException
  247. * @throws \think\db\exception\ModelNotFoundException
  248. */
  249. public function MenuActionDel(){
  250. $post =$this->request->post();
  251. $token = isset($post['token']) ? trim($post['token']) : "";
  252. if($token==""){
  253. return error_show(101,'token不能为空');
  254. }
  255. $effetc = VerifyTokens($token);
  256. if(!empty($effetc) && $effetc['code']!=0){
  257. return error_show($effetc['code'],$effetc['message']);
  258. }
  259. $id = isset($post['id']) ?intval($post['id']) :"";
  260. $menu = Db::name("action")->where("id","=",$id)->find();
  261. if($menu==false){
  262. return error_show(1003,"菜单功能信息不存在");
  263. }
  264. $result = Db::name("action")->delete($menu);
  265. $orde = ["order_code"=>$menu['id'],"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  266. ActionLog::logAdd($token,$orde,"action",0,$orde);
  267. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  268. }
  269. }