Menu.php 15 KB

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