Menu.php 15 KB

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