Menu.php 13 KB

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