Menu.php 12 KB

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