Menu.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. $token = isset($post['token']) ? trim($post['token']) : "";
  83. if($token==""){
  84. return error_show(101,'token不能为空');
  85. }
  86. $effetc = VerifyTokens($token);
  87. if(!empty($effetc) && $effetc['code']!=0){
  88. return error_show($effetc['code'],$effetc['message']);
  89. }
  90. $id = isset($post['id']) ?intval($post['id']) :"";
  91. if($id!=""){
  92. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  93. if($menu==false){
  94. return error_show(1003,"菜单不信息不存在");
  95. }
  96. }
  97. $name = isset($post['name']) ?trim($post['name']) :"";
  98. if($name==""){
  99. return error_show(1002,"菜单名称不能为空");
  100. }
  101. $url = isset($post['url']) ?trim($post['url']) :"";
  102. $route = isset($post['route']) ?trim($post['route']) :"";
  103. $code = isset($post['menu_code']) ?trim($post['menu_code']) :"";
  104. $img = isset($post['img']) ?trim($post['img']) :"";
  105. $pid = isset($post['pid']) ?intval($post['pid']) :0;
  106. $weight = isset($post['weight']) ?floatval($post['weight']) :1;
  107. $status = isset($post['is_show']) ? intval($post['is_show']) : 0;
  108. $is_display = isset($post['is_display']) ? intval($post['is_display']) : 0;
  109. $private = isset($post['private']) ?intval($post['private']) :(isset($menu['is_private']) ?
  110. $menu['is_private'] : 0);
  111. if($pid!=0 && $route==""){
  112. return error_show(1002,"子级菜单路由不能为空");
  113. }
  114. $data=[
  115. "menu_name"=>$name,
  116. "menu_url"=>$url,
  117. "menu_route"=>$route,
  118. "menu_code"=>$code,
  119. "menu_img"=>$img,
  120. "pid"=>$pid,
  121. 'is_show'=>$status,
  122. 'is_display'=>$is_display,
  123. "is_private"=>$private,
  124. 'status'=>1,
  125. "weight"=>$weight,
  126. "updatetime"=>date("Y-m-d H:i:s"),
  127. ];
  128. $message="新建";
  129. if($id!=""){
  130. $message="编辑";
  131. $data['id']=$id;
  132. }
  133. $result = Db::name("admin_menu")->save($data);
  134. $orde = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  135. ActionLog::logAdd($token,$orde,"admin_menu",0,$orde);
  136. return $result ? app_show(0,"{$message}成功"): error_show(1003,"{$message}失败");
  137. }
  138. /**
  139. * 保存新建的资源
  140. *
  141. * @param \think\Request $request
  142. * @return \think\Response
  143. */
  144. public function MenuStatus(){
  145. $post =$this->request->post();
  146. $token = isset($post['token']) ? trim($post['token']) : "";
  147. if($token==""){
  148. return error_show(101,'token不能为空');
  149. }
  150. $effetc = VerifyTokens($token);
  151. if(!empty($effetc) && $effetc['code']!=0){
  152. return error_show($effetc['code'],$effetc['message']);
  153. }
  154. $id = isset($post['id']) ?intval($post['id']) :"";
  155. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  156. if($menu==false){
  157. return error_show(1003,"菜单信息不存在");
  158. }
  159. $statu = isset($post['status'])&&$post['status']!="" ? intval($post['status']) :"";
  160. if($statu===""){
  161. return error_show(1003,"菜单状态不能为空");
  162. }
  163. $var= $menu['status'];
  164. $menu['status']=$statu;
  165. $menu['updatetime']=date("Y-m-d H:i:s");
  166. $result = Db::name("admin_menu")->save($menu);
  167. $orde = ["order_code"=>$menu['menu_name'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  168. ActionLog::logAdd($token,$orde,"admin_menu", $menu['status'],$orde);
  169. return $result ? app_show(0,"状态更新成功"): error_show(1003,"状态更新失败");
  170. }
  171. /**
  172. * @return \think\response\Json|void
  173. * @throws \think\db\exception\DataNotFoundException
  174. * @throws \think\db\exception\DbException
  175. * @throws \think\db\exception\ModelNotFoundException
  176. * @throws \think\exception\DbException
  177. */
  178. public function MenuAllList(){
  179. $post =$this->request->post();
  180. $data = Db::name("admin_menu")->where(['pid'=>0,'is_del'=>0])->order("weight desc,id asc")->select();
  181. $l=[];
  182. foreach ($data as $key=>$value){
  183. $temp=[];
  184. $temp = Db::name("admin_menu")->where(['pid'=>$value['id'],'is_del'=>0])->order("weight desc,id asc")
  185. ->select();
  186. $value['child']=$temp;
  187. $l[]=$value;
  188. }
  189. return app_show(0,"获取成功",$l);
  190. }
  191. /**
  192. * @return \think\response\Json|void
  193. * @throws \think\db\exception\DataNotFoundException
  194. * @throws \think\db\exception\DbException
  195. * @throws \think\db\exception\ModelNotFoundException
  196. * @throws \think\exception\DbException
  197. */
  198. public function MenuDel(){
  199. $post =$this->request->post();
  200. $token = isset($post['token']) ? trim($post['token']) : "";
  201. if($token==""){
  202. return error_show(101,'token不能为空');
  203. }
  204. $effetc = VerifyTokens($token);
  205. if(!empty($effetc) && $effetc['code']!=0){
  206. return error_show($effetc['code'],$effetc['message']);
  207. }
  208. $id = isset($post['id']) ?intval($post['id']) :"";
  209. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  210. if($menu==false){
  211. return error_show(1003,"菜单不信息不存在");
  212. }
  213. $var=$menu['status'];
  214. $menu['is_show']=0;
  215. $menu['status']=0;
  216. $menu['is_del']=1;
  217. $menu['updatetime']=date("Y-m-d H:i:s");
  218. $result = Db::name("admin_menu")->save($menu);
  219. $orde = ["order_code"=>$menu['menu_name'],"status"=>$var,"action_remark"=>'',"action_type"=>"delete"];
  220. ActionLog::logAdd($token,$orde,"admin_menu", $menu['status'],$orde);
  221. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  222. }
  223. /**
  224. * @return \think\response\Json|void
  225. * @throws \think\db\exception\DataNotFoundException
  226. * @throws \think\db\exception\DbException
  227. * @throws \think\db\exception\ModelNotFoundException
  228. */
  229. public function MenuActionDel(){
  230. $post =$this->request->post();
  231. $token = isset($post['token']) ? trim($post['token']) : "";
  232. if($token==""){
  233. return error_show(101,'token不能为空');
  234. }
  235. $effetc = VerifyTokens($token);
  236. if(!empty($effetc) && $effetc['code']!=0){
  237. return error_show($effetc['code'],$effetc['message']);
  238. }
  239. $id = isset($post['id']) ?intval($post['id']) :"";
  240. $menu = Db::name("action")->where("id","=",$id)->find();
  241. if($menu==false){
  242. return error_show(1003,"菜单功能信息不存在");
  243. }
  244. $result = Db::name("action")->delete($menu);
  245. $orde = ["order_code"=>$menu['menu_name'],"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  246. ActionLog::logAdd($token,$orde,"action",0,$orde);
  247. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  248. }
  249. }