Menu.php 9.2 KB

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