Menu.php 11 KB

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