Menu.php 8.8 KB

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