Menu.php 12 KB

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