Menu.php 10 KB

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