Menu.php 16 KB

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