Menu.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. ->field('id')
  70. ->where(['is_del' => 0, 'status' => 1])
  71. ->buildSql();
  72. } else $role['action_conllect'] = Db::name('user_role')
  73. ->alias('a')
  74. ->field('b.action_conllect')
  75. ->leftJoin('role_action b', 'b.role_id=a.roleid')
  76. ->where(['a.uid' => $this->uid, 'a.is_del' => 0, 'a.companyNo' => $post['relaComNo']])
  77. ->buildSql();
  78. // if (empty($role['action_conllect'])) {
  79. // return app_show(0, "获取成功", []);
  80. // }
  81. $where = [['status', '=', 1], ['cstatus', '=', 1], ['is_display', '=', 1]];
  82. // if (!empty($role['action_conllect'])) $where[] = ['aid', 'in', $role['action_conllect']];
  83. $data = Db::name("view_menu")
  84. ->where($where)
  85. ->where('aid in '.$role['action_conllect'])
  86. ->order("weight desc,id asc,cweight desc,cid asc")
  87. ->select()
  88. ->toArray();
  89. $list = [];
  90. $act = [];
  91. foreach ($data as $value) {
  92. $list[$value["id"]]['menu_name'] = $value['menu_name'];
  93. $list[$value["id"]]['menu_img'] = $value['menu_img'];
  94. $list[$value["id"]]['menu_route'] = $value['menu_route'];
  95. $list[$value["id"]]['is_display'] = $value['is_display'];
  96. $list[$value["id"]]['status'] = $value['status'];
  97. $temp = [];
  98. $temp['menu_name'] = $value['cname'];
  99. $temp['menu_img'] = $value['cmenu_img'];
  100. $temp['menu_route'] = $value['cmenu_route'];
  101. $temp['menu_url'] = $value['cmenu_url'];
  102. $temp['status'] = $value['cstatus'];
  103. $temp['is_private'] = $value['cprivate'];
  104. $temp['is_display'] = $value['cis_display'];
  105. $list[$value["id"]]['child'][$value['cid']] = $temp;
  106. $act[$value['id']][$value['cid']][] = $value['acode'];
  107. $list[$value["id"]]['child'][$value['cid']]['action'] = $act[$value['id']][$value['cid']];
  108. }
  109. array_walk($list, function (&$value) {
  110. $value['child'] = array_values($value['child']);
  111. });
  112. return app_show(0, "获取成功", array_values($list));
  113. }
  114. /**
  115. * 显示创建资源表单页.
  116. *
  117. * @return \think\Response
  118. */
  119. public function MenuEdit(){
  120. $post =$this->request->post();
  121. // $token = isset($post['token']) ? trim($post['token']) : "";
  122. // if($token==""){
  123. // return error_show(101,'token不能为空');
  124. // }
  125. // $effetc = VerifyTokens($token);
  126. // if(!empty($effetc) && $effetc['code']!=0){
  127. // return error_show($effetc['code'],$effetc['message']);
  128. // }
  129. $id = isset($post['id']) ?intval($post['id']) :"";
  130. if($id!=""){
  131. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  132. if($menu==false){
  133. return error_show(1003,"菜单不信息不存在");
  134. }
  135. }
  136. $name = isset($post['name']) ?trim($post['name']) :"";
  137. if($name==""){
  138. return error_show(1002,"菜单名称不能为空");
  139. }
  140. $url = isset($post['url']) ?trim($post['url']) :"";
  141. $route = isset($post['route']) ?trim($post['route']) :"";
  142. $code = isset($post['menu_code']) ?trim($post['menu_code']) :"";
  143. $img = isset($post['img']) ?trim($post['img']) :"";
  144. $pid = isset($post['pid']) ?intval($post['pid']) :0;
  145. $weight = isset($post['weight']) ?floatval($post['weight']) :1;
  146. $status = isset($post['is_show']) ? intval($post['is_show']) : 0;
  147. $is_display = isset($post['is_display']) ? intval($post['is_display']) : 0;
  148. $private = isset($post['private']) ?intval($post['private']) :(isset($menu['is_private']) ?$menu['is_private'] : 0);
  149. if($pid!=0 && $route==""){
  150. return error_show(1002,"子级菜单路由不能为空");
  151. }
  152. $level = isset($post['level'])?intval($post['level']):'';
  153. if($level=='') return json_show(0,'菜单等级不能为空');
  154. $data=[
  155. "menu_name"=>$name,
  156. "menu_url"=>$url,
  157. "menu_route"=>$route,
  158. "menu_code"=>$code,
  159. "menu_img"=>$img,
  160. "pid"=>$pid,
  161. 'is_show'=>$status,
  162. 'is_display'=>$is_display,
  163. "is_private"=>$private,
  164. 'status'=>1,
  165. 'level'=>$level,
  166. "weight"=>$weight,
  167. "updatetime"=>date("Y-m-d H:i:s"),
  168. ];
  169. $message="新建";
  170. if($id!=""){
  171. $message="编辑";
  172. $data['id']=$id;
  173. }
  174. $result = Db::name("admin_menu")->save($data);
  175. $orde = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  176. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$orde,"admin_menu",0,$orde);
  177. return $result ? app_show(0,"{$message}成功"): error_show(1003,"{$message}失败");
  178. }
  179. /**
  180. * 保存新建的资源
  181. *
  182. * @param \think\Request $request
  183. * @return \think\Response
  184. */
  185. public function MenuStatus(){
  186. $post =$this->request->post();
  187. // $token = isset($post['token']) ? trim($post['token']) : "";
  188. // if($token==""){
  189. // return error_show(101,'token不能为空');
  190. // }
  191. // $effetc = VerifyTokens($token);
  192. // if(!empty($effetc) && $effetc['code']!=0){
  193. // return error_show($effetc['code'],$effetc['message']);
  194. // }
  195. $id = isset($post['id']) ?intval($post['id']) :"";
  196. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  197. if($menu==false){
  198. return error_show(1003,"菜单信息不存在");
  199. }
  200. $statu = isset($post['status'])&&$post['status']!="" ? intval($post['status']) :"";
  201. if($statu===""){
  202. return error_show(1003,"菜单状态不能为空");
  203. }
  204. $var= $menu['status'];
  205. $menu['status']=$statu;
  206. $menu['updatetime']=date("Y-m-d H:i:s");
  207. $result = Db::name("admin_menu")->save($menu);
  208. $orde = ["order_code"=>$menu['menu_name'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  209. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$orde,"admin_menu", $menu['status'],$orde);
  210. return $result ? app_show(0,"状态更新成功"): error_show(1003,"状态更新失败");
  211. }
  212. /**
  213. * @return \think\response\Json|void
  214. * @throws \think\db\exception\DataNotFoundException
  215. * @throws \think\db\exception\DbException
  216. * @throws \think\db\exception\ModelNotFoundException
  217. * @throws \think\exception\DbException
  218. */
  219. public function MenuAllList(){
  220. // $post =$this->request->post();
  221. $data = Db::name("admin_menu")
  222. ->where(['pid'=>0,'is_del'=>0])
  223. ->order("weight desc,id asc")
  224. ->select()
  225. ->toArray();
  226. $l=[];
  227. foreach ($data as $key=>$value){
  228. $temp=[];
  229. $temp = Db::name("admin_menu")
  230. ->where(['pid'=>$value['id'],'is_del'=>0])
  231. ->order("weight desc,id asc")
  232. ->select()
  233. ->toArray();
  234. $value['child']=$temp;
  235. $l[]=$value;
  236. }
  237. return app_show(0,"获取成功",$l);
  238. }
  239. /**
  240. * @return \think\response\Json|void
  241. * @throws \think\db\exception\DataNotFoundException
  242. * @throws \think\db\exception\DbException
  243. * @throws \think\db\exception\ModelNotFoundException
  244. * @throws \think\exception\DbException
  245. */
  246. public function MenuDel(){
  247. $post =$this->request->post();
  248. // $token = isset($post['token']) ? trim($post['token']) : "";
  249. // if($token==""){
  250. // return error_show(101,'token不能为空');
  251. // }
  252. // $effetc = VerifyTokens($token);
  253. // if(!empty($effetc) && $effetc['code']!=0){
  254. // return error_show($effetc['code'],$effetc['message']);
  255. // }
  256. $id = isset($post['id']) ?intval($post['id']) :"";
  257. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  258. if($menu==false){
  259. return error_show(1003,"菜单不信息不存在");
  260. }
  261. $var=$menu['status'];
  262. $menu['is_show']=0;
  263. $menu['status']=0;
  264. $menu['is_del']=1;
  265. $menu['updatetime']=date("Y-m-d H:i:s");
  266. $result = Db::name("admin_menu")->save($menu);
  267. $orde = ["order_code"=>$menu['menu_name'],"status"=>$var,"action_remark"=>'',"action_type"=>"delete"];
  268. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$orde,"admin_menu", $menu['status'],$orde);
  269. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  270. }
  271. /**
  272. * @return \think\response\Json|void
  273. * @throws \think\db\exception\DataNotFoundException
  274. * @throws \think\db\exception\DbException
  275. * @throws \think\db\exception\ModelNotFoundException
  276. */
  277. public function MenuActionDel(){
  278. $post =$this->request->post();
  279. // $token = isset($post['token']) ? trim($post['token']) : "";
  280. // if($token==""){
  281. // return error_show(101,'token不能为空');
  282. // }
  283. // $effetc = VerifyTokens($token);
  284. // if(!empty($effetc) && $effetc['code']!=0){
  285. // return error_show($effetc['code'],$effetc['message']);
  286. // }
  287. $id = isset($post['id']) ?intval($post['id']) :"";
  288. $menu = Db::name("action")->where("id","=",$id)->find();
  289. if($menu==false){
  290. return error_show(1003,"菜单功能信息不存在");
  291. }
  292. $result = Db::name("action")->delete($menu);
  293. $orde = ["order_code"=>$menu['id'],"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  294. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$orde,"action",0,$orde);
  295. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  296. }
  297. }