Menu.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 list()
  58. {
  59. $uid = $this->uid;
  60. if ($uid == '') {
  61. return error_show(101, '未找到用户信息');
  62. }
  63. $role = $this->checkRole();
  64. if (empty($role)) {
  65. return app_show(0, "获取成功", []);
  66. }
  67. $where=[['status','=',1],['cstatus','=',1],['is_display','=',1]];
  68. if(!empty($role['action_conllect'])) $where[]=['aid','in',$role['action_conllect']];
  69. $data = Db::name("view_menu")
  70. ->where($where)
  71. ->order("weight desc,id asc,cweight desc,cid asc")
  72. ->select()
  73. ->toArray();
  74. $list = [];
  75. $act = [];
  76. foreach ($data as $value) {
  77. $list[$value["id"]]['menu_name'] = $value['menu_name'];
  78. $list[$value["id"]]['menu_img'] = $value['menu_img'];
  79. $list[$value["id"]]['menu_route'] = $value['menu_route'];
  80. $list[$value["id"]]['is_display'] = $value['is_display'];
  81. $list[$value["id"]]['status'] = $value['status'];
  82. $temp = [];
  83. $temp['menu_name'] = $value['cname'];
  84. $temp['menu_img'] = $value['cmenu_img'];
  85. $temp['menu_route'] = $value['cmenu_route'];
  86. $temp['menu_url'] = $value['cmenu_url'];
  87. $temp['status'] = $value['cstatus'];
  88. $temp['is_private'] = $value['cprivate'];
  89. $temp['is_display'] = $value['cis_display'];
  90. $list[$value["id"]]['child'][$value['cid']] = $temp;
  91. $act[$value['id']][$value['cid']][] = $value['acode'];
  92. $list[$value["id"]]['child'][$value['cid']]['action'] = $act[$value['id']][$value['cid']];
  93. }
  94. array_walk($list, function (&$value) {
  95. $value['child'] = array_values($value['child']);
  96. });
  97. return app_show(0, "获取成功", array_values($list));
  98. }
  99. /**
  100. * 显示创建资源表单页.
  101. *
  102. * @return \think\Response
  103. */
  104. public function MenuEdit(){
  105. $post =$this->request->post();
  106. // $token = isset($post['token']) ? trim($post['token']) : "";
  107. // if($token==""){
  108. // return error_show(101,'token不能为空');
  109. // }
  110. // $effetc = VerifyTokens($token);
  111. // if(!empty($effetc) && $effetc['code']!=0){
  112. // return error_show($effetc['code'],$effetc['message']);
  113. // }
  114. $id = isset($post['id']) ?intval($post['id']) :"";
  115. if($id!=""){
  116. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  117. if($menu==false){
  118. return error_show(1003,"菜单不信息不存在");
  119. }
  120. }
  121. $name = isset($post['name']) ?trim($post['name']) :"";
  122. if($name==""){
  123. return error_show(1002,"菜单名称不能为空");
  124. }
  125. $url = isset($post['url']) ?trim($post['url']) :"";
  126. $route = isset($post['route']) ?trim($post['route']) :"";
  127. $code = isset($post['menu_code']) ?trim($post['menu_code']) :"";
  128. $img = isset($post['img']) ?trim($post['img']) :"";
  129. $pid = isset($post['pid']) ?intval($post['pid']) :0;
  130. $weight = isset($post['weight']) ?floatval($post['weight']) :1;
  131. $status = isset($post['is_show']) ? intval($post['is_show']) : 0;
  132. $is_display = isset($post['is_display']) ? intval($post['is_display']) : 0;
  133. $private = isset($post['private']) ?intval($post['private']) :(isset($menu['is_private']) ?$menu['is_private'] : 0);
  134. if($pid!=0 && $route==""){
  135. return error_show(1002,"子级菜单路由不能为空");
  136. }
  137. $level = isset($post['level'])?intval($post['level']):'';
  138. if($level=='') return json_show(0,'菜单等级不能为空');
  139. $data=[
  140. "menu_name"=>$name,
  141. "menu_url"=>$url,
  142. "menu_route"=>$route,
  143. "menu_code"=>$code,
  144. "menu_img"=>$img,
  145. "pid"=>$pid,
  146. 'is_show'=>$status,
  147. 'is_display'=>$is_display,
  148. "is_private"=>$private,
  149. 'status'=>1,
  150. 'level'=>$level,
  151. "weight"=>$weight,
  152. "updatetime"=>date("Y-m-d H:i:s"),
  153. ];
  154. $message="新建";
  155. if($id!=""){
  156. $message="编辑";
  157. $data['id']=$id;
  158. }
  159. $result = Db::name("admin_menu")->save($data);
  160. $orde = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  161. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$orde,"admin_menu",0,$orde);
  162. return $result ? app_show(0,"{$message}成功"): error_show(1003,"{$message}失败");
  163. }
  164. /**
  165. * 保存新建的资源
  166. *
  167. * @param \think\Request $request
  168. * @return \think\Response
  169. */
  170. public function MenuStatus(){
  171. $post =$this->request->post();
  172. // $token = isset($post['token']) ? trim($post['token']) : "";
  173. // if($token==""){
  174. // return error_show(101,'token不能为空');
  175. // }
  176. // $effetc = VerifyTokens($token);
  177. // if(!empty($effetc) && $effetc['code']!=0){
  178. // return error_show($effetc['code'],$effetc['message']);
  179. // }
  180. $id = isset($post['id']) ?intval($post['id']) :"";
  181. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  182. if($menu==false){
  183. return error_show(1003,"菜单信息不存在");
  184. }
  185. $statu = isset($post['status'])&&$post['status']!="" ? intval($post['status']) :"";
  186. if($statu===""){
  187. return error_show(1003,"菜单状态不能为空");
  188. }
  189. $var= $menu['status'];
  190. $menu['status']=$statu;
  191. $menu['updatetime']=date("Y-m-d H:i:s");
  192. $result = Db::name("admin_menu")->save($menu);
  193. $orde = ["order_code"=>$menu['menu_name'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  194. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$orde,"admin_menu", $menu['status'],$orde);
  195. return $result ? app_show(0,"状态更新成功"): error_show(1003,"状态更新失败");
  196. }
  197. /**
  198. * @return \think\response\Json|void
  199. * @throws \think\db\exception\DataNotFoundException
  200. * @throws \think\db\exception\DbException
  201. * @throws \think\db\exception\ModelNotFoundException
  202. * @throws \think\exception\DbException
  203. */
  204. public function MenuAllList(){
  205. // $post =$this->request->post();
  206. $data = Db::name("admin_menu")
  207. ->where(['pid'=>0,'is_del'=>0])
  208. ->order("weight desc,id asc")
  209. ->select()
  210. ->toArray();
  211. $l=[];
  212. foreach ($data as $key=>$value){
  213. $temp=[];
  214. $temp = Db::name("admin_menu")
  215. ->where(['pid'=>$value['id'],'is_del'=>0])
  216. ->order("weight desc,id asc")
  217. ->select()
  218. ->toArray();
  219. $value['child']=$temp;
  220. $l[]=$value;
  221. }
  222. return app_show(0,"获取成功",$l);
  223. }
  224. /**
  225. * @return \think\response\Json|void
  226. * @throws \think\db\exception\DataNotFoundException
  227. * @throws \think\db\exception\DbException
  228. * @throws \think\db\exception\ModelNotFoundException
  229. * @throws \think\exception\DbException
  230. */
  231. public function MenuDel(){
  232. $post =$this->request->post();
  233. // $token = isset($post['token']) ? trim($post['token']) : "";
  234. // if($token==""){
  235. // return error_show(101,'token不能为空');
  236. // }
  237. // $effetc = VerifyTokens($token);
  238. // if(!empty($effetc) && $effetc['code']!=0){
  239. // return error_show($effetc['code'],$effetc['message']);
  240. // }
  241. $id = isset($post['id']) ?intval($post['id']) :"";
  242. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  243. if($menu==false){
  244. return error_show(1003,"菜单不信息不存在");
  245. }
  246. $var=$menu['status'];
  247. $menu['is_show']=0;
  248. $menu['status']=0;
  249. $menu['is_del']=1;
  250. $menu['updatetime']=date("Y-m-d H:i:s");
  251. $result = Db::name("admin_menu")->save($menu);
  252. $orde = ["order_code"=>$menu['menu_name'],"status"=>$var,"action_remark"=>'',"action_type"=>"delete"];
  253. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$orde,"admin_menu", $menu['status'],$orde);
  254. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  255. }
  256. /**
  257. * @return \think\response\Json|void
  258. * @throws \think\db\exception\DataNotFoundException
  259. * @throws \think\db\exception\DbException
  260. * @throws \think\db\exception\ModelNotFoundException
  261. */
  262. public function MenuActionDel(){
  263. $post =$this->request->post();
  264. // $token = isset($post['token']) ? trim($post['token']) : "";
  265. // if($token==""){
  266. // return error_show(101,'token不能为空');
  267. // }
  268. // $effetc = VerifyTokens($token);
  269. // if(!empty($effetc) && $effetc['code']!=0){
  270. // return error_show($effetc['code'],$effetc['message']);
  271. // }
  272. $id = isset($post['id']) ?intval($post['id']) :"";
  273. $menu = Db::name("action")->where("id","=",$id)->find();
  274. if($menu==false){
  275. return error_show(1003,"菜单功能信息不存在");
  276. }
  277. $result = Db::name("action")->delete($menu);
  278. $orde = ["order_code"=>$menu['id'],"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  279. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$orde,"action",0,$orde);
  280. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  281. }
  282. }