Menu.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\admin\BaseController;
  5. use think\App;
  6. use think\facade\Db;
  7. use think\facade\Validate;
  8. class Menu extends BaseController{
  9. public function __construct(App $app) {
  10. parent::__construct($app);
  11. }
  12. /** 进入页面获取权限列表
  13. * @return \think\response\Json|void
  14. * @throws \think\db\exception\DataNotFoundException
  15. * @throws \think\db\exception\DbException
  16. * @throws \think\db\exception\ModelNotFoundException
  17. */
  18. public function MenuList(){
  19. $condition =["status"=>1,"is_del"=>0];
  20. if($this->level=='') return app_show(10000,"账户角色账户已禁用",[]);
  21. if ($this->level!=1){
  22. $role = Db::name("role_action")->where("role_id","=",$this->roleid)->find();
  23. if($role==false){
  24. return app_show(0,"获取成功",[]);
  25. }
  26. $action = Db::name("action")->where(['id'=>explode(",",$role['action_conllect']),"status"=>1,"is_del"=>0])
  27. ->column("id,menuid,action_code");
  28. if (empty($action)){
  29. return app_show(0,"获取成功",[]);
  30. }
  31. $MenuAction=[];
  32. foreach ($action as $value){
  33. $MenuAction[$value['menuid']][]=$value['action_code'];
  34. }
  35. $menuid= array_column($action,"menuid");
  36. $condition['id']=$menuid;
  37. }else{
  38. $action = Db::name("action")->where(["status"=>1,"is_del"=>0])->column("id,action_code,menuid");
  39. $MenuAction=[];
  40. foreach ($action as $value){
  41. $MenuAction[$value['menuid']][]=$value['action_code'];
  42. }
  43. $menuid= array_column($action,"menuid");
  44. $condition['id']=$menuid;
  45. }
  46. $menuAll =Db::name("admin_menu")
  47. ->where($condition)
  48. ->where("level",">=",$this->level)
  49. ->column("id,menu_name,menu_img,menu_route,menu_url,pid,is_show,is_private,menu_type,level,status,weight");
  50. $list=[];
  51. foreach ($menuAll as $value){
  52. $value['action']=$MenuAction[$value['id']]??[];
  53. makeMenu($value,$list);
  54. }
  55. $keys =array_column($list, 'weight');
  56. array_multisort($keys,SORT_DESC,$list);
  57. $list=MenuTree($list,0);
  58. return app_show(0,"获取成功",$list);
  59. }
  60. /**菜单设置列表
  61. * @return \think\response\Json|void
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. * @throws \think\exception\DbException
  66. */
  67. public function MenuAllList(){
  68. $role = Db::name("role_action")->where("role_id","=",$this->roleid)->find();
  69. if($role==false){
  70. return app_show(0,"获取成功",[]);
  71. }
  72. $data = Db::name("admin_menu")->where([["is_del","=",0],["level",">=",$this->level]])->field("id,menu_name,menu_img,menu_route,menu_url,pid,is_show,is_private,menu_type,level,status,weight")->order("weight desc,id asc")->select();
  73. $l= MenuTree($data,0);
  74. return app_show(0,"获取成功",$l);
  75. }
  76. /**
  77. * 菜单新建
  78. * @return \think\response\Json|void
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @throws \think\exception\DbException
  83. */
  84. public function MenuAdd(){
  85. $post =$this->request->only([
  86. "menu_name"=>"",
  87. "menu_url"=>"",
  88. "menu_route"=>"",
  89. "menu_code"=>"",
  90. "menu_img"=>"",
  91. "pid"=>"0",
  92. "private"=>"0",
  93. "weight"=>"1",
  94. "menu_type"=>"1",
  95. "level"=>"",
  96. ],"post","trim");
  97. $valite =Validate::rule([
  98. "menu_name|菜单名称"=>"require|max:255",
  99. "menu_url|菜单链接"=>"max:255",
  100. "menu_route|菜单路由"=>"max:255",
  101. "menu_code|菜单编号"=>"max:255",
  102. "menu_img|菜单图标"=>"require|max:255",
  103. "pid|父级ID"=>"require|number",
  104. "private|是否私有"=>"require|number|in:0,1",
  105. "weight|权重"=>"require|number",
  106. "menu_type|菜单类型"=>"require|number|in:1,2",
  107. "level|菜单级别"=>"require|max:255",
  108. ]);
  109. if($valite->check($post)==false)return error_show(1004,$valite->getError());
  110. if($post['pid']!=0 && $post['menu_route']==""){
  111. return error_show(1002,"子级菜单路由不能为空");
  112. }
  113. $data=[
  114. "menu_name"=>$post['menu_name'],
  115. "menu_url"=>$post['menu_url'],
  116. "menu_route"=>$post['menu_route'],
  117. "menu_code"=>$post['menu_code'],
  118. "menu_img"=>$post['menu_img'],
  119. "pid"=>$post['pid'],
  120. "weight"=>$post['weight'],
  121. "is_show"=>1,
  122. "is_private"=>$post['private'],
  123. "menu_type"=>$post['menu_type'],
  124. "level"=>$post['level'],
  125. "status"=>1,
  126. "addtime"=>date("Y-m-d H:i:s"),
  127. "updatetime"=>date("Y-m-d H:i:s"),
  128. ];
  129. $result = Db::name("admin_menu")->insert($data);
  130. return $result ? app_show(0,"添加成功"): error_show(1003,"添加失败");
  131. }
  132. /**菜单编辑或新建
  133. * @return \think\response\Json|void
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public function MenuEdit(){
  139. $post =$this->request->only([
  140. "id"=>"",
  141. "menu_name"=>"",
  142. "menu_url"=>"",
  143. "menu_route"=>"",
  144. "menu_code"=>"",
  145. "menu_img"=>"",
  146. "pid"=>"0",
  147. "private"=>"0",
  148. "weight"=>"1",
  149. "menu_type"=>"1",
  150. "level"=>"",
  151. ],"post","trim");
  152. $valite =Validate::rule([
  153. "menu_name|菜单名称"=>"require|max:255",
  154. "menu_url|菜单链接"=>"max:255",
  155. "menu_route|菜单路由"=>"max:255",
  156. "menu_code|菜单编号"=>"max:255",
  157. "menu_img|菜单图标"=>"require|max:255",
  158. "pid|父级ID"=>"require|number",
  159. "private|是否私有"=>"require|number|in:0,1",
  160. "weight|权重"=>"require|number",
  161. "menu_type|菜单类型"=>"require|number|in:1,2",
  162. "level|菜单级别"=>"require|max:255",
  163. "id|菜单ID"=>"require|number|gt:0",
  164. ]);
  165. if($valite->check($post)==false)return error_show(1004,$valite->getError());
  166. if($post['pid']!=0 && $post['menu_route']==""){
  167. return error_show(1002,"子级菜单路由不能为空");
  168. }
  169. $menu = Db::name("admin_menu")->where([["id","=",$post['id']],["is_del","=",0]])->find();
  170. if($menu==false){
  171. return error_show(1003,"菜单不信息不存在");
  172. }
  173. if($post['id']!=0 && $post['menu_route']==""){
  174. return error_show(1002,"子级菜单路由不能为空");
  175. }
  176. $data=[
  177. "id"=>$post['id']??null,
  178. "menu_name"=>$post['menu_name'],
  179. "menu_url"=>$post['menu_url'],
  180. "menu_route"=>$post['menu_route'],
  181. "menu_code"=>$post['menu_code'],
  182. "menu_img"=>$post['menu_img'],
  183. "pid"=>$post['pid'],
  184. "weight"=>$post['weight'],
  185. "is_show"=>1,
  186. "is_private"=>$post['private'],
  187. "menu_type"=>$post['menu_type'],
  188. "level"=>$post['level'],
  189. "status"=>1,
  190. "updatetime"=>date("Y-m-d H:i:s"),
  191. ];
  192. $message="新建";
  193. if($post['id']!=""){
  194. $message="编辑";
  195. }
  196. $result = Db::name("admin_menu")->save($data);
  197. return $result ? app_show(0,"{$message}成功"): error_show(1003,"{$message}失败");
  198. }
  199. /**菜单状态更新
  200. * @return \think\response\Json|void
  201. * @throws \think\db\exception\DataNotFoundException
  202. * @throws \think\db\exception\DbException
  203. * @throws \think\db\exception\ModelNotFoundException
  204. */
  205. public function MenuStatus(){
  206. $post =$this->post;
  207. $id = isset($post['id']) ?intval($post['id']) :"";
  208. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  209. if($menu==false){
  210. return error_show(1003,"菜单信息不存在");
  211. }
  212. $statu = isset($post['status'])&&$post['status']!="" ? intval($post['status']) :"";
  213. if($statu===""){
  214. return error_show(1003,"菜单状态不能为空");
  215. }
  216. $menu['status']=$statu;
  217. $menu['updatetime']=date("Y-m-d H:i:s");
  218. $result = Db::name("admin_menu")->save($menu);
  219. return $result ? app_show(0,"状态更新成功"): error_show(1003,"状态更新失败");
  220. }
  221. //菜单删除
  222. public function MenuDel(){
  223. $post =$this->post;
  224. $id = isset($post['id']) ?intval($post['id']) :"";
  225. $menu = Db::name("admin_menu")->where([["id","=",$id],["is_del","=",0]])->find();
  226. if($menu==false){
  227. return error_show(1003,"菜单信息不存在");
  228. }
  229. $update =["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")];
  230. $result = Db::name("admin_menu")->where($menu)->update($update);
  231. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  232. }
  233. /**
  234. * 获取菜单下的所有功能列表
  235. * @return \think\response\Json|void
  236. * @throws \think\db\exception\DataNotFoundException
  237. * @throws \think\db\exception\DbException
  238. * @throws \think\db\exception\ModelNotFoundException
  239. * @throws \think\exception\DbException
  240. */
  241. public function ActionList(){
  242. $post =$this->post;
  243. $pageid = isset($post['id']) ? intval($post['id']) : "";
  244. if($pageid==""){
  245. return error_show(1001,'页面id不能为空');
  246. }
  247. $condition = ['menuid'=>$pageid,"a.is_del"=>0];
  248. $data=Db::name('action')->alias("a")->leftJoin("cfp_action_list l","a.action_code=l.action_code")->field
  249. ("a.*,action_name")->where($condition)->select();
  250. return app_show(0,"获取成功",$data);
  251. }
  252. /** 菜单下功能信息修改* **/
  253. public function ActionSave(){
  254. $post =$this->post;
  255. $actid = isset($post['id']) ? intval($post['id']) : "";
  256. if($actid==""){
  257. return error_show(1001,'功能id不能为空');
  258. }
  259. $menuid = isset($post['menuid']) ? intval($post['menuid']) : "";
  260. if($menuid==""){
  261. return error_show(1001,'页面menuid不能为空');
  262. }
  263. $code = isset($post['action_code']) ? trim($post['action_code']) : "";
  264. $action_name = isset($post['action_name']) ? trim($post['action_name']) : "";
  265. if($action_name==''){
  266. return error_show(1001,'参数 action_name 不能为空');
  267. }
  268. $status = isset($post['status']) ? intval($post['status']) : 1;
  269. if($code==""){
  270. return error_show(1002,'功能code不能为空');
  271. }
  272. $istrue =Db::name("action")->where(['menuid'=>$menuid,"action_code"=>$code,"is_del"=>0])->find();
  273. if($istrue && $istrue['id']!=$actid){
  274. return error_show(1005,'此功能已存在');
  275. }
  276. Db::startTrans();
  277. try{
  278. $actioninfo =Db::name("action_list")->where(["action_code"=>$code,"is_del"=>0])->find();
  279. if($actioninfo){
  280. $update =Db::name("action_list")->where($actioninfo)->update(["action_name"=>$action_name,"updatetime"=>date("Y-m-d H:i:s")]);
  281. }else{
  282. $data=["action_name"=>$action_name,"action_code"=>$code,"addtime"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  283. $update = Db::name("action_list")->insert($data);
  284. }
  285. if($update==false){
  286. Db::rollback();
  287. return error_show(1002,'功能新建失败');
  288. }
  289. $data = ['action_code'=>$code,'status'=>$status,"updatetime"=>date("Y-m-d H:i:s")];
  290. $result=Db::name("action")->where("id","=",$actid)->save($data);
  291. if($result){
  292. Db::commit();
  293. return app_show(0,"更新成功");
  294. }else{
  295. Db::rollback();
  296. return error_show(1004,"更新失败");
  297. }
  298. }catch (\Exception $e){
  299. Db::rollback();
  300. return error_show(1003,$e->getMessage());
  301. }
  302. }
  303. /** 菜单下功能信息状态修改
  304. * @return \think\response\Json|void
  305. * @throws \think\db\exception\DataNotFoundException
  306. * @throws \think\db\exception\DbException
  307. * @throws \think\db\exception\ModelNotFoundException
  308. * @throws \think\exception\DbException
  309. */
  310. public function ActionStatus(){
  311. $post =$this->post;
  312. $actid = isset($post['id']) ? intval($post['id']) : "";
  313. if($actid==""){
  314. return error_show(1001,'功能id不能为空');
  315. }
  316. $status = isset($post['status']) ? intval($post['status']) : 1;
  317. try{
  318. $data = ['status'=>$status,"updatetime"=>date("Y-m-d H:i:s")];
  319. $result=Db::name("action")->where("id","=",$actid)->save($data);
  320. if($result){
  321. return app_show(0,"更新成功");
  322. }else{
  323. return error_show(1004,"更新失败");
  324. }
  325. }catch (\Exception $e){
  326. return error_show(1003,$e->getMessage());
  327. }
  328. }
  329. /** 菜单下功能信息状态修改
  330. * @return \think\response\Json|void
  331. * @throws \think\exception\DbException
  332. */
  333. public function ActionAdd(){
  334. $post =$this->post;
  335. $pageid = isset($post['menuid']) ? intval($post['menuid']) : "";
  336. if($pageid==""){
  337. return error_show(1001,'页面id不能为空');
  338. }
  339. $code = isset($post['action_code']) ? trim($post['action_code']) : "";
  340. $action_name = isset($post['action_name']) ? trim($post['action_name']) : "";
  341. $status = isset($post['status']) ? intval($post['status']) : 1;
  342. if($code==""){
  343. return error_show(1002,'功能code不能为空');
  344. }
  345. if($action_name==""){
  346. return error_show(1002,'功能 action_name 不能为空');
  347. }
  348. Db::startTrans();
  349. try{
  350. $actioninfo =Db::name("action_list")->where(["action_code"=>$code,"is_del"=>0])->find();
  351. if($actioninfo){
  352. $update =Db::name("action_list")->where($actioninfo)->update(["action_name"=>$action_name,"updatetime"=>date("Y-m-d H:i:s")]);
  353. }else{
  354. $data=["action_name"=>$action_name,"action_code"=>$code,"addtime"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  355. $update = Db::name("action_list")->insert($data);
  356. }
  357. if($update==false){
  358. Db::rollback();
  359. return error_show(1002,'功能新建失败');
  360. }
  361. $where = ['menuid'=>$pageid,'action_code'=>$code,"is_del"=>0];
  362. $true =Db::name("action")->where($where)->find();
  363. if($true){
  364. Db::rollback();
  365. return error_show(1003,'此功能已存在');
  366. }else{
  367. $data = ['menuid'=>$pageid,'action_code'=>$code,'status'=>$status,"updatetime"=>date("Y-m-d H:i:s"),"addtime"=>date("Y-m-d H:i:s")];
  368. $in = Db::name("action")->insert($data);
  369. if($in){
  370. Db::commit();
  371. return app_show(0,"添加成功");
  372. }
  373. }
  374. Db::rollback();
  375. return error_show(1005,'功能新建失败');
  376. }catch (\Exception $e){
  377. Db::rollback();
  378. return error_show(1005,$e->getMessage());
  379. }
  380. }
  381. //菜单下功能删除
  382. public function ActionDel(){
  383. $post =$this->post;
  384. $actid = isset($post['id']) ? intval($post['id']) : "";
  385. if($actid==""){
  386. return error_show(1001,'功能id不能为空');
  387. }
  388. $act=Db::name("action")->where([["id","=",$actid],["is_del","=",0]])->find();
  389. if($act==false){
  390. return error_show(1004,'未找到相关数据');
  391. }
  392. try{
  393. $data = ['is_del'=>1,"updatetime"=>date("Y-m-d H:i:s")];
  394. $result=Db::name("action")->where($act)->update($data);
  395. if($result){
  396. return app_show(0,"删除成功");
  397. }else{
  398. return error_show(1004,"删除失败");
  399. }
  400. }catch (\Exception $e){
  401. return error_show(1003,$e->getMessage());
  402. }
  403. }
  404. }