Menu.php 10 KB

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