Menu.php 17 KB

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