Menu.php 9.2 KB

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