Menu.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. $data= Db::name("view_menu")->where('aid',"in",explode(",",$role['action_conllect']))->where(['status'=>1,"cstatus"=>1])
  21. ->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. * @return \think\response\Json|void
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. * @throws \think\exception\DbException
  52. */
  53. public function MenuAllList(){
  54. $data = Db::name("admin_menu")->where(['pid'=>0])->order("weight desc,id asc")->select();
  55. $l=[];
  56. foreach ($data as $key=>$value){
  57. $temp = Db::name("admin_menu")->where(['pid'=>$value['id']])->order("weight desc,id asc")->select();
  58. $value['child']=$temp;
  59. $l[]=$value;
  60. }
  61. return app_show(0,"获取成功",$l);
  62. }
  63. /**
  64. * 菜单新建
  65. * @return \think\response\Json|void
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. * @throws \think\exception\DbException
  70. */
  71. public function MenuAdd(){
  72. $post =$this->post;
  73. $name = isset($post['menu_name']) ?trim($post['menu_name']) :"";
  74. if($name==""){
  75. return error_show(1002,"菜单名称不能为空");
  76. }
  77. $url = isset($post['menu_url']) ?trim($post['menu_url']) :"";
  78. $route = isset($post['menu_route']) ?trim($post['menu_route']) :"";
  79. $code = isset($post['menu_code']) ?trim($post['menu_code']) :"";
  80. $img = isset($post['menu_img']) ?trim($post['menu_img']) :"";
  81. $pid = isset($post['pid']) ?intval($post['pid']) :0;
  82. $private = isset($post['private']) ?intval($post['private']) :0;
  83. $weight = isset($post['weight']) ?floatval($post['weight']) :1;
  84. if($pid!=0 && $route==""){
  85. return error_show(1002,"子级菜单路由不能为空");
  86. }
  87. $data=[
  88. "menu_name"=>$name,
  89. "menu_url"=>$url,
  90. "menu_route"=>$route,
  91. "menu_code"=>$code,
  92. "menu_img"=>$img,
  93. "pid"=>$pid,
  94. "weight"=>$weight,
  95. "is_show"=>1,
  96. "is_private"=>$private,
  97. "status"=>1,
  98. "addtime"=>date("Y-m-d H:i:s"),
  99. "updatetime"=>date("Y-m-d H:i:s"),
  100. ];
  101. $result = Db::name("admin_menu")->insert($data);
  102. return $result ? app_show(0,"添加成功"): error_show(1003,"添加失败");
  103. }
  104. /**菜单编辑或新建
  105. * @return \think\response\Json|void
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. */
  110. public function MenuEdit(){
  111. $post =$this->post;
  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. /**菜单状态更新
  157. * @return \think\response\Json|void
  158. * @throws \think\db\exception\DataNotFoundException
  159. * @throws \think\db\exception\DbException
  160. * @throws \think\db\exception\ModelNotFoundException
  161. */
  162. public function MenuStatus(){
  163. $post =$this->post;
  164. $id = isset($post['id']) ?intval($post['id']) :"";
  165. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  166. if($menu==false){
  167. return error_show(1003,"菜单信息不存在");
  168. }
  169. $statu = isset($post['status'])&&$post['status']!="" ? intval($post['status']) :"";
  170. if($statu===""){
  171. return error_show(1003,"菜单状态不能为空");
  172. }
  173. $menu['status']=$statu;
  174. $menu['updatetime']=date("Y-m-d H:i:s");
  175. $result = Db::name("admin_menu")->save($menu);
  176. return $result ? app_show(0,"状态更新成功"): error_show(1003,"状态更新失败");
  177. }
  178. /**
  179. * 获取菜单下的所有功能列表
  180. * @return \think\response\Json|void
  181. * @throws \think\db\exception\DataNotFoundException
  182. * @throws \think\db\exception\DbException
  183. * @throws \think\db\exception\ModelNotFoundException
  184. * @throws \think\exception\DbException
  185. */
  186. public function ActionList(){
  187. $post =$this->post;
  188. $pageid = isset($post['id']) ? intval($post['id']) : "";
  189. if($pageid==""){
  190. return error_show(1001,'页面id不能为空');
  191. }
  192. $condition = ['menuid'=>$pageid];
  193. $data=Db::name('action')->alias("a")->leftJoin("cfp_action_list l","a.action_code=l.action_code")->field
  194. ("a.*,action_name")->where($condition)->select();
  195. return app_show(0,"获取成功",$data);
  196. }
  197. /** 菜单下功能信息修改*/
  198. public function ActionSave(){
  199. $post =$this->post;
  200. $actid = isset($post['id']) ? intval($post['id']) : "";
  201. if($actid==""){
  202. return error_show(1001,'功能id不能为空');
  203. }
  204. $menuid = isset($post['menuid']) ? intval($post['menuid']) : "";
  205. if($menuid==""){
  206. return error_show(1001,'页面menuid不能为空');
  207. }
  208. $code = isset($post['action_code']) ? trim($post['action_code']) : "";
  209. $status = isset($post['status']) ? intval($post['status']) : 1;
  210. if($code==""){
  211. return error_show(1002,'功能code不能为空');
  212. }
  213. $istrue =Db::name("action")->where(['menuid'=>$menuid,"action_code"=>$code])->find();
  214. if($istrue && $istrue['id']!=$actid){
  215. return error_show(1005,'此功能已存在');
  216. }
  217. try{
  218. $data = ['action_code'=>$code,'status'=>$status,"updatetime"=>date("Y-m-d H:i:s")];
  219. $result=Db::name("action")->where("id","=",$actid)->save($data);
  220. if($result){
  221. return app_show(0,"更新成功");
  222. }else{
  223. return error_show(1004,"更新失败");
  224. }
  225. }catch (\Exception $e){
  226. return error_show(1003,$e->getMessage());
  227. }
  228. }
  229. /** 菜单下功能信息状态修改
  230. * @return \think\response\Json|void
  231. * @throws \think\db\exception\DataNotFoundException
  232. * @throws \think\db\exception\DbException
  233. * @throws \think\db\exception\ModelNotFoundException
  234. * @throws \think\exception\DbException
  235. */
  236. public function ActionStatus(){
  237. $post =$this->post;
  238. $actid = isset($post['id']) ? intval($post['id']) : "";
  239. if($actid==""){
  240. return error_show(1001,'功能id不能为空');
  241. }
  242. $status = isset($post['status']) ? intval($post['status']) : 1;
  243. try{
  244. $data = ['status'=>$status,"updatetime"=>date("Y-m-d H:i:s")];
  245. $result=Db::name("action")->where("id","=",$actid)->save($data);
  246. if($result){
  247. return app_show(0,"更新成功");
  248. }else{
  249. return error_show(1004,"更新失败");
  250. }
  251. }catch (\Exception $e){
  252. return error_show(1003,$e->getMessage());
  253. }
  254. }
  255. /** 菜单下功能信息状态修改
  256. * @return \think\response\Json|void
  257. * @throws \think\exception\DbException
  258. */
  259. public function ActionAdd(){
  260. $post =$this->post;
  261. $pageid = isset($post['menuid']) ? intval($post['menuid']) : "";
  262. if($pageid==""){
  263. return error_show(1001,'页面id不能为空');
  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. try{
  271. $where = ['menuid'=>$pageid,'action_code'=>$code];
  272. $true =Db::name("action")->where($where)->find();
  273. $data = ['menuid'=>$pageid,'action_code'=>$code,'status'=>$status,"updatetime"=>date("Y-m-d H:i:s"),"addtime"=>date("Y-m-d H:i:s")];
  274. if($true){
  275. return error_show(1003,'此功能已存在');
  276. }else{
  277. Db::name("action")->insert($data);
  278. return app_show(0,"添加成功");
  279. }
  280. }catch (\Exception $e){
  281. return error_show(1005,$e->getMessage());
  282. }
  283. }
  284. }