Menu.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. }
  18. public function list()
  19. {
  20. $post = $this->request->filter('trim')->post();
  21. $where = [['status', '=', 1], ['cstatus', '=', 1], ['is_display', '=', 1]];
  22. //先把当前账号能看到的菜单筛选出来
  23. switch ($this->level) {
  24. case 1:
  25. //超管,能看到等级为012的菜单
  26. $role['action_conllect'] = Db::name('action')
  27. ->where(['is_del' => 0, 'status' => 1])
  28. ->column('id');
  29. break;
  30. case 2:
  31. $role['action_conllect'] = Db::name('user_role')
  32. ->alias('a')
  33. ->leftJoin('role_action b', 'b.role_id=a.roleid')
  34. ->where(['a.uid' => $this->uid, 'a.is_del' => 0, 'a.companyNo' => $post['relaComNo']])
  35. ->value('b.action_conllect');
  36. break;
  37. case 3:
  38. $role['action_conllect'] = Db::name('user_role')
  39. ->alias('a')
  40. ->leftJoin('role_action b', 'b.role_id=a.roleid')
  41. ->where(['a.uid' => $this->uid, 'a.is_del' => 0])
  42. ->value('b.action_conllect');
  43. break;
  44. }
  45. if (isset($post['level']) && $post['level'] !== '') $where[] = ['blevel', 'in', [0, $post['level']]];
  46. else {
  47. if ($this->level == 1) $where[] = ['blevel', 'in', [0, 1, 2]];
  48. else $where[] = ['blevel', 'in', [0, $this->level]];
  49. }
  50. $roleaction = is_array($role['action_conllect'])?$role['action_conllect']:json_decode($role['action_conllect'],true);
  51. $data = Db::name("view_menu")
  52. ->where($where)
  53. ->whereIn('aid', $roleaction)
  54. ->order("weight desc,id asc,cweight desc,cid asc")
  55. ->select()
  56. ->toArray();
  57. $list = [];
  58. $act = [];
  59. foreach ($data as $value) {
  60. $list[$value["id"]]['menu_name'] = $value['menu_name'];
  61. $list[$value["id"]]['menu_img'] = $value['menu_img'];
  62. $list[$value["id"]]['menu_route'] = $value['menu_route'];
  63. $list[$value["id"]]['is_display'] = $value['is_display'];
  64. $list[$value["id"]]['status'] = $value['status'];
  65. $temp = [];
  66. $temp['menu_name'] = $value['cname'];
  67. $temp['menu_img'] = $value['cmenu_img'];
  68. $temp['menu_route'] = $value['cmenu_route'];
  69. $temp['menu_url'] = $value['cmenu_url'];
  70. $temp['status'] = $value['cstatus'];
  71. $temp['is_private'] = $value['cprivate'];
  72. $temp['is_display'] = $value['cis_display'];
  73. $list[$value["id"]]['child'][$value['cid']] = $temp;
  74. $act[$value['id']][$value['cid']][] = $value['acode'];
  75. $list[$value["id"]]['child'][$value['cid']]['action'] = $act[$value['id']][$value['cid']];
  76. }
  77. array_walk($list, function (&$value) {
  78. $value['child'] = array_values($value['child']);
  79. });
  80. return app_show(0, "获取成功", array_values($list));
  81. }
  82. /**
  83. * 显示创建资源表单页.
  84. *
  85. * @return \think\Response
  86. */
  87. public function MenuEdit(){
  88. $post =$this->request->post();
  89. $id = isset($post['id']) ?intval($post['id']) :"";
  90. if($id!=""){
  91. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  92. if($menu==false){
  93. return error_show(1003,"菜单不信息不存在");
  94. }
  95. }
  96. $name = isset($post['name']) ?trim($post['name']) :"";
  97. if($name==""){
  98. return error_show(1002,"菜单名称不能为空");
  99. }
  100. $url = isset($post['url']) ?trim($post['url']) :"";
  101. $route = isset($post['route']) ?trim($post['route']) :"";
  102. $code = isset($post['menu_code']) ?trim($post['menu_code']) :"";
  103. $img = isset($post['img']) ?trim($post['img']) :"";
  104. $pid = isset($post['pid']) ?intval($post['pid']) :0;
  105. $weight = isset($post['weight']) ?floatval($post['weight']) :1;
  106. $status = isset($post['is_show']) ? intval($post['is_show']) : 0;
  107. $is_display = isset($post['is_display']) ? intval($post['is_display']) : 0;
  108. $private = isset($post['private']) ?intval($post['private']) :(isset($menu['is_private']) ?$menu['is_private'] : 0);
  109. if($pid!=0 && $route==""){
  110. return error_show(1002,"子级菜单路由不能为空");
  111. }
  112. $menu_api = isset($post['menu_api'])?trim($post['menu_api']):'';
  113. $level = isset($post['level'])?intval($post['level']):'';
  114. if($level==='') return json_show(0,'菜单等级不能为空');//这里要用全等,有可能level传值0
  115. $data=[
  116. "menu_name"=>$name,
  117. "menu_url"=>$url,
  118. "menu_route"=>$route,
  119. "menu_code"=>$code,
  120. "menu_img"=>$img,
  121. "pid"=>$pid,
  122. 'is_show'=>$status,
  123. 'is_display'=>$is_display,
  124. "is_private"=>$private,
  125. 'status'=>1,
  126. 'level'=>$level,
  127. "weight"=>$weight,
  128. "menu_api"=>$menu_api,
  129. "updatetime"=>date("Y-m-d H:i:s"),
  130. ];
  131. $message="新建";
  132. if($id!=""){
  133. $message="编辑";
  134. $data['id']=$id;
  135. }
  136. $result = Db::name("admin_menu")->save($data);
  137. $orde = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  138. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$orde,"admin_menu",0,$orde);
  139. return $result ? app_show(0,"{$message}成功"): error_show(1003,"{$message}失败");
  140. }
  141. /**
  142. * 保存新建的资源
  143. *
  144. * @param \think\Request $request
  145. * @return \think\Response
  146. */
  147. public function MenuStatus(){
  148. $post =$this->request->post();
  149. // $token = isset($post['token']) ? trim($post['token']) : "";
  150. // if($token==""){
  151. // return error_show(101,'token不能为空');
  152. // }
  153. // $effetc = VerifyTokens($token);
  154. // if(!empty($effetc) && $effetc['code']!=0){
  155. // return error_show($effetc['code'],$effetc['message']);
  156. // }
  157. $id = isset($post['id']) ?intval($post['id']) :"";
  158. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  159. if($menu==false){
  160. return error_show(1003,"菜单信息不存在");
  161. }
  162. $statu = isset($post['status'])&&$post['status']!="" ? intval($post['status']) :"";
  163. if($statu===""){
  164. return error_show(1003,"菜单状态不能为空");
  165. }
  166. $var= $menu['status'];
  167. $menu['status']=$statu;
  168. $menu['updatetime']=date("Y-m-d H:i:s");
  169. $result = Db::name("admin_menu")->save($menu);
  170. $orde = ["order_code"=>$menu['menu_name'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  171. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$orde,"admin_menu", $menu['status'],$orde);
  172. return $result ? app_show(0,"状态更新成功"): error_show(1003,"状态更新失败");
  173. }
  174. /**
  175. * @return \think\response\Json|void
  176. * @throws \think\db\exception\DataNotFoundException
  177. * @throws \think\db\exception\DbException
  178. * @throws \think\db\exception\ModelNotFoundException
  179. * @throws \think\exception\DbException
  180. */
  181. public function MenuAllList(){
  182. // $post =$this->request->post();
  183. $data = Db::name("admin_menu")
  184. ->where(['pid'=>0,'is_del'=>0])
  185. ->order("weight desc,id asc")
  186. ->select()
  187. ->toArray();
  188. $l=[];
  189. foreach ($data as $key=>$value){
  190. $temp=[];
  191. $temp = Db::name("admin_menu")
  192. ->where(['pid'=>$value['id'],'is_del'=>0])
  193. ->order("weight desc,id asc")
  194. ->select()
  195. ->toArray();
  196. $value['child']=$temp;
  197. $l[]=$value;
  198. }
  199. return app_show(0,"获取成功",$l);
  200. }
  201. /**
  202. * @return \think\response\Json|void
  203. * @throws \think\db\exception\DataNotFoundException
  204. * @throws \think\db\exception\DbException
  205. * @throws \think\db\exception\ModelNotFoundException
  206. * @throws \think\exception\DbException
  207. */
  208. public function MenuDel(){
  209. $post =$this->request->post();
  210. // $token = isset($post['token']) ? trim($post['token']) : "";
  211. // if($token==""){
  212. // return error_show(101,'token不能为空');
  213. // }
  214. // $effetc = VerifyTokens($token);
  215. // if(!empty($effetc) && $effetc['code']!=0){
  216. // return error_show($effetc['code'],$effetc['message']);
  217. // }
  218. $id = isset($post['id']) ?intval($post['id']) :"";
  219. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  220. if($menu==false){
  221. return error_show(1003,"菜单不信息不存在");
  222. }
  223. $var=$menu['status'];
  224. $menu['is_show']=0;
  225. $menu['status']=0;
  226. $menu['is_del']=1;
  227. $menu['updatetime']=date("Y-m-d H:i:s");
  228. $result = Db::name("admin_menu")->save($menu);
  229. $orde = ["order_code"=>$menu['menu_name'],"status"=>$var,"action_remark"=>'',"action_type"=>"delete"];
  230. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$orde,"admin_menu", $menu['status'],$orde);
  231. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  232. }
  233. /**
  234. * @return \think\response\Json|void
  235. * @throws \think\db\exception\DataNotFoundException
  236. * @throws \think\db\exception\DbException
  237. * @throws \think\db\exception\ModelNotFoundException
  238. */
  239. public function MenuActionDel(){
  240. $post =$this->request->post();
  241. $id = isset($post['id']) ?intval($post['id']) :"";
  242. $menu = Db::name("action")->where("id","=",$id)->find();
  243. if($menu==false){
  244. return error_show(1003,"菜单功能信息不存在");
  245. }
  246. $result = Db::name("action")->delete($menu);
  247. $orde = ["order_code"=>$menu['id'],"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  248. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$orde,"action",0,$orde);
  249. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  250. }
  251. }