Menu.php 14 KB

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