Menu.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\BaseController;
  5. use think\App;
  6. use think\facade\Db;
  7. use think\Request;
  8. class Menu extends BaseController
  9. {
  10. public $post="";
  11. private $uid = '';
  12. /**
  13. * 显示资源列表
  14. *
  15. * @return \think\Response
  16. */
  17. public function __construct(App $app)
  18. {
  19. parent::__construct($app);
  20. $post =$this->request->post();
  21. $token = isset($post['token']) ? trim($post['token']) : "";
  22. if($token==""){
  23. return error_show(101,'token不能为空');
  24. }
  25. $effetc = VerifyTokens($token);
  26. if(!empty($effetc) && $effetc['code']!=0){
  27. return error_show($effetc['code'],$effetc['message']);
  28. }
  29. $this->uid = isset($effetc['data']['id']) ? $effetc['data']['id'] :"";
  30. }
  31. public function list()
  32. {
  33. $post=$this->request->post();
  34. $uid = $this->uid;
  35. if($uid==''){
  36. return error_show(101,'未找到用户信息');
  37. }
  38. $roleid = Db::name("user_role")->where([["uid","=",$uid],["status","=",1],["is_del","=",0]])->find();
  39. if(empty($roleid)){
  40. return error_show(1004,'未找到用户权限信息');
  41. }
  42. $role = Db::name("role_action")->where("role_id","=", $roleid['roleid'])->find();
  43. if($role==false){
  44. return app_show(0,"获取成功",[]);
  45. }
  46. $data= Db::name("view_menu")->where('aid',"in",explode(",",$role['action_conllect']))->where(['status'=>1,"cstatus"=>1,"is_display"=>1])
  47. ->order("weight desc,id asc,cweight desc,cid asc")->select();
  48. $list=[];
  49. $act=[];
  50. foreach ($data as $value){
  51. $list[$value["id"]]['menu_name']=$value['menu_name'];
  52. $list[$value["id"]]['menu_img']=$value['menu_img'];
  53. $list[$value["id"]]['menu_route']=$value['menu_route'];
  54. $list[$value["id"]]['is_display']=$value['is_display'];
  55. $list[$value["id"]]['status']=$value['status'];
  56. $temp = [];
  57. $temp['menu_name']=$value['cname'];
  58. $temp['menu_img']=$value['cmenu_img'];
  59. $temp['menu_route']=$value['cmenu_route'];
  60. $temp['menu_url']=$value['cmenu_url'];
  61. $temp['menu_url']=$value['cmenu_url'];
  62. $temp['status']=$value['cstatus'];
  63. $temp['is_private']=$value['cprivate'];
  64. $temp['is_display']=$value['cis_display'];
  65. $list[$value["id"]]['child'][$value['cid']]=$temp;
  66. $act[$value['id']][$value['cid']][]=$value['acode'];
  67. $list[$value["id"]]['child'][$value['cid']]['action']= $act[$value['id']][$value['cid']];
  68. }
  69. array_walk($list,function (&$value){
  70. $value['child']= array_values($value['child']);
  71. });
  72. return app_show(0,"获取成功",array_values($list));
  73. }
  74. /**
  75. * 显示创建资源表单页.
  76. *
  77. * @return \think\Response
  78. */
  79. public function MenuEdit(){
  80. $post =$this->request->post();
  81. $token = isset($post['token']) ? trim($post['token']) : "";
  82. if($token==""){
  83. return error_show(101,'token不能为空');
  84. }
  85. $effetc = VerifyTokens($token);
  86. if(!empty($effetc) && $effetc['code']!=0){
  87. return error_show($effetc['code'],$effetc['message']);
  88. }
  89. $id = isset($post['id']) ?intval($post['id']) :"";
  90. if($id!=""){
  91. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  92. if($menu==false){
  93. return error_show(1003,"菜单不信息不存在");
  94. }
  95. }
  96. $name = isset($post['name']) ?trim($post['name']) :"";
  97. if($name==""){
  98. return error_show(1002,"菜单名称不能为空");
  99. }
  100. $url = isset($post['url']) ?trim($post['url']) :"";
  101. $route = isset($post['route']) ?trim($post['route']) :"";
  102. $code = isset($post['menu_code']) ?trim($post['menu_code']) :"";
  103. $img = isset($post['img']) ?trim($post['img']) :"";
  104. $pid = isset($post['pid']) ?intval($post['pid']) :0;
  105. $weight = isset($post['weight']) ?floatval($post['weight']) :1;
  106. $status = isset($post['is_show']) ? intval($post['is_show']) : 0;
  107. $is_display = isset($post['is_display']) ? intval($post['is_display']) : 0;
  108. $private = isset($post['private']) ?intval($post['private']) :(isset($menu['is_private']) ? $menu['is_private'] : 0);
  109. if($pid!=0 && $route==""){
  110. return error_show(1002,"子级菜单路由不能为空");
  111. }
  112. $data=[
  113. "menu_name"=>$name,
  114. "menu_url"=>$url,
  115. "menu_route"=>$route,
  116. "menu_code"=>$code,
  117. "menu_img"=>$img,
  118. "pid"=>$pid,
  119. 'is_show'=>$status,
  120. 'is_display'=>$is_display,
  121. "is_private"=>$private,
  122. 'status'=>1,
  123. "weight"=>$weight,
  124. "updatetime"=>date("Y-m-d H:i:s"),
  125. ];
  126. $message="新建";
  127. if($id!=""){
  128. $message="编辑";
  129. $data['id']=$id;
  130. }
  131. $result = Db::name("admin_menu")->save($data);
  132. return $result ? app_show(0,"{$message}成功"): error_show(1003,"{$message}失败");
  133. }
  134. /**
  135. * 保存新建的资源
  136. *
  137. * @param \think\Request $request
  138. * @return \think\Response
  139. */
  140. public function MenuStatus(){
  141. $post =$this->request->post();
  142. $token = isset($post['token']) ? trim($post['token']) : "";
  143. if($token==""){
  144. return error_show(101,'token不能为空');
  145. }
  146. $effetc = VerifyTokens($token);
  147. if(!empty($effetc) && $effetc['code']!=0){
  148. return error_show($effetc['code'],$effetc['message']);
  149. }
  150. $id = isset($post['id']) ?intval($post['id']) :"";
  151. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  152. if($menu==false){
  153. return error_show(1003,"菜单信息不存在");
  154. }
  155. $statu = isset($post['status'])&&$post['status']!="" ? intval($post['status']) :"";
  156. if($statu===""){
  157. return error_show(1003,"菜单状态不能为空");
  158. }
  159. $menu['status']=$statu;
  160. $menu['updatetime']=date("Y-m-d H:i:s");
  161. $result = Db::name("admin_menu")->save($menu);
  162. return $result ? app_show(0,"状态更新成功"): error_show(1003,"状态更新失败");
  163. }
  164. /**
  165. * @return \think\response\Json|void
  166. * @throws \think\db\exception\DataNotFoundException
  167. * @throws \think\db\exception\DbException
  168. * @throws \think\db\exception\ModelNotFoundException
  169. * @throws \think\exception\DbException
  170. */
  171. public function MenuAllList(){
  172. $post =$this->request->post();
  173. $data = Db::name("admin_menu")->where(['pid'=>0,'is_del'=>0])->order("weight desc,id asc")->select();
  174. $l=[];
  175. foreach ($data as $key=>$value){
  176. $temp=[];
  177. $temp = Db::name("admin_menu")->where(['pid'=>$value['id'],'is_del'=>0])->order("weight desc,id asc")
  178. ->select();
  179. $value['child']=$temp;
  180. $l[]=$value;
  181. }
  182. return app_show(0,"获取成功",$l);
  183. }
  184. /**
  185. * @return \think\response\Json|void
  186. * @throws \think\db\exception\DataNotFoundException
  187. * @throws \think\db\exception\DbException
  188. * @throws \think\db\exception\ModelNotFoundException
  189. * @throws \think\exception\DbException
  190. */
  191. public function MenuDel(){
  192. $post =$this->request->post();
  193. $token = isset($post['token']) ? trim($post['token']) : "";
  194. if($token==""){
  195. return error_show(101,'token不能为空');
  196. }
  197. $effetc = VerifyTokens($token);
  198. if(!empty($effetc) && $effetc['code']!=0){
  199. return error_show($effetc['code'],$effetc['message']);
  200. }
  201. $id = isset($post['id']) ?intval($post['id']) :"";
  202. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  203. if($menu==false){
  204. return error_show(1003,"菜单不信息不存在");
  205. }
  206. // $var=$menu['status'];
  207. $menu['is_show']=0;
  208. $menu['status']=0;
  209. $menu['is_del']=1;
  210. $menu['updatetime']=date("Y-m-d H:i:s");
  211. $result = Db::name("admin_menu")->save($menu);
  212. // $orde = ["order_code"=>$menu['menu_name'],"status"=>$var,"action_remark"=>'',"action_type"=>"delete"];
  213. // ActionLog::logAdd($token,$orde,"admin_menu", $menu['status'],$orde);
  214. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  215. }
  216. /**
  217. * @return \think\response\Json|void
  218. * @throws \think\db\exception\DataNotFoundException
  219. * @throws \think\db\exception\DbException
  220. * @throws \think\db\exception\ModelNotFoundException
  221. */
  222. public function MenuActionDel(){
  223. $post =$this->request->post();
  224. $token = isset($post['token']) ? trim($post['token']) : "";
  225. if($token==""){
  226. return error_show(101,'token不能为空');
  227. }
  228. $effetc = VerifyTokens($token);
  229. if(!empty($effetc) && $effetc['code']!=0){
  230. return error_show($effetc['code'],$effetc['message']);
  231. }
  232. $id = isset($post['id']) ?intval($post['id']) :"";
  233. $menu = Db::name("action")->where("id","=",$id)->find();
  234. if($menu==false){
  235. return error_show(1003,"菜单功能信息不存在");
  236. }
  237. $result = Db::name("action")->delete($menu);
  238. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  239. }
  240. }