Menu.php 14 KB

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