Menu.php 13 KB

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