Menu.php 15 KB

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