Menu.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\admin\BaseController;
  5. use think\App;
  6. use think\facade\Db;
  7. use think\facade\Validate;
  8. class Menu extends BaseController{
  9. public function __construct(App $app) {
  10. parent::__construct($app);
  11. }
  12. /** 进入页面获取权限列表
  13. * @return \think\response\Json|void
  14. * @throws \think\db\exception\DataNotFoundException
  15. * @throws \think\db\exception\DbException
  16. * @throws \think\db\exception\ModelNotFoundException
  17. */
  18. public function MenuList()
  19. {
  20. $condition = ["status" => 1, "is_del" => 0];
  21. $level = $this->request->post('level/d',$this->level,'trim');
  22. if ($level == '') return app_show(10000, "账户角色账户已禁用", []);
  23. if ($level != 1) {
  24. $role = Db::name("role_action")->where("role_id", "=", $this->roleid)->find();
  25. if ($role == false) {
  26. return app_show(0, "获取成功", []);
  27. }
  28. $action = Db::name("action")->where(['id' => explode(",", $role['action_conllect']), "status" => 1, "is_del" => 0])->column("id,menuid,action_code");
  29. if (empty($action)) return app_show(0, "获取成功", []);
  30. $MenuAction = [];
  31. foreach ($action as $value) {
  32. $MenuAction[$value['menuid']][] = $value['action_code'];
  33. }
  34. $menuid = array_column($action, "menuid");
  35. $condition['id'] = $menuid;
  36. $condition['level'] = [0,$level];
  37. } else {
  38. $action = Db::name("action")->where(["status" => 1, "is_del" => 0])->column("id,action_code,menuid");
  39. $MenuAction = [];
  40. foreach ($action as $value) {
  41. $MenuAction[$value['menuid']][] = $value['action_code'];
  42. }
  43. $menuid = array_column($action, "menuid");
  44. $condition['id'] = $menuid;
  45. $condition['level'] = [0,$level,2];
  46. }
  47. $menuAll = Db::name("admin_menu")
  48. ->where($condition)
  49. ->column("id,menu_name,menu_img,menu_route,menu_url,pid,is_show,is_private,menu_type,level,status,weight");
  50. $list = [];
  51. foreach ($menuAll as $value) {
  52. $value['action'] = $MenuAction[$value['id']] ?? [];
  53. makeMenu($value, $list);
  54. }
  55. $keys = array_column($list, 'weight');
  56. array_multisort($keys, SORT_DESC, $list);
  57. $list = MenuTree($list, 0);
  58. return app_show(0, "获取成功", $list);
  59. }
  60. /**菜单设置列表
  61. * @return \think\response\Json|void
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. * @throws \think\exception\DbException
  66. */
  67. public function MenuAllList(){
  68. $role = Db::name("role_action")->where("role_id","=",$this->roleid)->find();
  69. if($role==false){
  70. return app_show(0,"获取成功",[]);
  71. }
  72. $data = Db::name("admin_menu")
  73. ->where([["is_del", "=", 0]])
  74. ->field("id,menu_name,menu_img,menu_route,menu_url,pid,is_show,is_private,menu_type,level,status,weight")
  75. ->order("weight desc,id asc")
  76. ->select()
  77. ->toArray();
  78. $l = MenuTree($data, 0);
  79. return app_show(0, "获取成功", $l);
  80. }
  81. /**
  82. * 菜单新建
  83. * @return \think\response\Json|void
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. * @throws \think\exception\DbException
  88. */
  89. public function MenuAdd(){
  90. $post =$this->request->only([
  91. "menu_name"=>"",
  92. "menu_url"=>"",
  93. "menu_route"=>"",
  94. "menu_code"=>"",
  95. "menu_img"=>"",
  96. "pid"=>"0",
  97. "private"=>"0",
  98. "weight"=>"1",
  99. "menu_type"=>"1",
  100. "level"=>"",
  101. ],"post","trim");
  102. $valite =Validate::rule([
  103. "menu_name|菜单名称"=>"require|max:255",
  104. "menu_url|菜单链接"=>"max:255",
  105. "menu_route|菜单路由"=>"max:255",
  106. "menu_code|菜单编号"=>"max:255",
  107. "menu_img|菜单图标"=>"require|max:255",
  108. "pid|父级ID"=>"require|number",
  109. "private|是否私有"=>"require|number|in:0,1",
  110. "weight|权重"=>"require|number",
  111. "menu_type|菜单类型"=>"require|number|in:1,2",
  112. "level|菜单级别" => "requireIf:menu_type,2|in:0,1,2,3",
  113. ]);
  114. if($valite->check($post)==false)return error_show(1004,$valite->getError());
  115. if($post['pid']!=0 && $post['menu_route']==""){
  116. return error_show(1002,"子级菜单路由不能为空");
  117. }
  118. $data=[
  119. "menu_name"=>$post['menu_name'],
  120. "menu_url"=>$post['menu_url'],
  121. "menu_route"=>$post['menu_route'],
  122. "menu_code"=>$post['menu_code'],
  123. "menu_img"=>$post['menu_img'],
  124. "pid"=>$post['pid'],
  125. "weight"=>$post['weight'],
  126. "is_show"=>1,
  127. "is_private"=>$post['private'],
  128. "menu_type"=>$post['menu_type'],
  129. "level" => $post['menu_type'] == 2 ? $post['level'] : 0,
  130. "status"=>1,
  131. "addtime"=>date("Y-m-d H:i:s"),
  132. "updatetime"=>date("Y-m-d H:i:s"),
  133. ];
  134. $result = Db::name("admin_menu")->insert($data);
  135. return $result ? app_show(0,"添加成功"): error_show(1003,"添加失败");
  136. }
  137. /**菜单编辑或新建
  138. * @return \think\response\Json|void
  139. * @throws \think\db\exception\DataNotFoundException
  140. * @throws \think\db\exception\DbException
  141. * @throws \think\db\exception\ModelNotFoundException
  142. */
  143. public function MenuEdit(){
  144. $post =$this->request->only([
  145. "id"=>"",
  146. "menu_name"=>"",
  147. "menu_url"=>"",
  148. "menu_route"=>"",
  149. "menu_code"=>"",
  150. "menu_img"=>"",
  151. "pid"=>"0",
  152. "private"=>"0",
  153. "weight"=>"1",
  154. "menu_type"=>"1",
  155. "level"=>"",
  156. ],"post","trim");
  157. $valite =Validate::rule([
  158. "menu_name|菜单名称"=>"require|max:255",
  159. "menu_url|菜单链接"=>"max:255",
  160. "menu_route|菜单路由"=>"max:255",
  161. "menu_code|菜单编号"=>"max:255",
  162. "menu_img|菜单图标"=>"require|max:255",
  163. "pid|父级ID"=>"require|number",
  164. "private|是否私有"=>"require|number|in:0,1",
  165. "weight|权重"=>"require|number",
  166. "menu_type|菜单类型"=>"require|number|in:1,2",
  167. "level|菜单级别" => "requireIf:menu_type,2|in:0,1,2,3",
  168. "id|菜单ID"=>"require|number|gt:0",
  169. ]);
  170. if($valite->check($post)==false)return error_show(1004,$valite->getError());
  171. if($post['pid']!=0 && $post['menu_route']==""){
  172. return error_show(1002,"子级菜单路由不能为空");
  173. }
  174. $menu = Db::name("admin_menu")->where([["id","=",$post['id']],["is_del","=",0]])->find();
  175. if($menu==false){
  176. return error_show(1003,"菜单不信息不存在");
  177. }
  178. if($post['id']!=0 && $post['menu_route']==""){
  179. return error_show(1002,"子级菜单路由不能为空");
  180. }
  181. $data=[
  182. "id"=>$post['id']??null,
  183. "menu_name"=>$post['menu_name'],
  184. "menu_url"=>$post['menu_url'],
  185. "menu_route"=>$post['menu_route'],
  186. "menu_code"=>$post['menu_code'],
  187. "menu_img"=>$post['menu_img'],
  188. "pid"=>$post['pid'],
  189. "weight"=>$post['weight'],
  190. "is_show"=>1,
  191. "is_private"=>$post['private'],
  192. "menu_type"=>$post['menu_type'],
  193. "level" => $post['menu_type'] == 2 ? $post['level'] : 0,
  194. "status"=>1,
  195. "updatetime"=>date("Y-m-d H:i:s"),
  196. ];
  197. $message="新建";
  198. if($post['id']!=""){
  199. $message="编辑";
  200. }
  201. $result = Db::name("admin_menu")->save($data);
  202. return $result ? app_show(0,"{$message}成功"): error_show(1003,"{$message}失败");
  203. }
  204. /**菜单状态更新
  205. * @return \think\response\Json|void
  206. * @throws \think\db\exception\DataNotFoundException
  207. * @throws \think\db\exception\DbException
  208. * @throws \think\db\exception\ModelNotFoundException
  209. */
  210. public function MenuStatus(){
  211. $post =$this->post;
  212. $id = isset($post['id']) ?intval($post['id']) :"";
  213. $menu = Db::name("admin_menu")->where("id","=",$id)->find();
  214. if($menu==false){
  215. return error_show(1003,"菜单信息不存在");
  216. }
  217. $statu = isset($post['status'])&&$post['status']!="" ? intval($post['status']) :"";
  218. if($statu===""){
  219. return error_show(1003,"菜单状态不能为空");
  220. }
  221. $menu['status']=$statu;
  222. $menu['updatetime']=date("Y-m-d H:i:s");
  223. $result = Db::name("admin_menu")->save($menu);
  224. return $result ? app_show(0,"状态更新成功"): error_show(1003,"状态更新失败");
  225. }
  226. //菜单删除
  227. public function MenuDel(){
  228. $post =$this->post;
  229. $id = isset($post['id']) ?intval($post['id']) :"";
  230. $menu = Db::name("admin_menu")->where([["id","=",$id],["is_del","=",0]])->find();
  231. if($menu==false){
  232. return error_show(1003,"菜单信息不存在");
  233. }
  234. $update =["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")];
  235. $result = Db::name("admin_menu")->where($menu)->update($update);
  236. return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
  237. }
  238. /**
  239. * 获取菜单下的所有功能列表
  240. * @return \think\response\Json|void
  241. * @throws \think\db\exception\DataNotFoundException
  242. * @throws \think\db\exception\DbException
  243. * @throws \think\db\exception\ModelNotFoundException
  244. * @throws \think\exception\DbException
  245. */
  246. public function ActionList(){
  247. $post =$this->post;
  248. $pageid = isset($post['id']) ? intval($post['id']) : "";
  249. if($pageid==""){
  250. return error_show(1001,'页面id不能为空');
  251. }
  252. $condition = ['menuid'=>$pageid,"a.is_del"=>0];
  253. $data=Db::name('action')->alias("a")->leftJoin("cfp_action_list l","a.action_code=l.action_code")->field
  254. ("a.*,action_name")->where($condition)->select();
  255. return app_show(0,"获取成功",$data);
  256. }
  257. /** 菜单下功能信息修改* **/
  258. public function ActionSave(){
  259. $post =$this->post;
  260. $actid = isset($post['id']) ? intval($post['id']) : "";
  261. if($actid==""){
  262. return error_show(1001,'功能id不能为空');
  263. }
  264. $menuid = isset($post['menuid']) ? intval($post['menuid']) : "";
  265. if($menuid==""){
  266. return error_show(1001,'页面menuid不能为空');
  267. }
  268. $code = isset($post['action_code']) ? trim($post['action_code']) : "";
  269. $action_name = isset($post['action_name']) ? trim($post['action_name']) : "";
  270. if($action_name==''){
  271. return error_show(1001,'参数 action_name 不能为空');
  272. }
  273. $status = isset($post['status']) ? intval($post['status']) : 1;
  274. if($code==""){
  275. return error_show(1002,'功能code不能为空');
  276. }
  277. $istrue =Db::name("action")->where(['menuid'=>$menuid,"action_code"=>$code,"is_del"=>0])->find();
  278. if($istrue && $istrue['id']!=$actid){
  279. return error_show(1005,'此功能已存在');
  280. }
  281. Db::startTrans();
  282. try{
  283. $actioninfo =Db::name("action_list")->where(["action_code"=>$code,"is_del"=>0])->find();
  284. if($actioninfo){
  285. $update =Db::name("action_list")->where($actioninfo)->update(["action_name"=>$action_name,"updatetime"=>date("Y-m-d H:i:s")]);
  286. }else{
  287. $data=["action_name"=>$action_name,"action_code"=>$code,"addtime"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  288. $update = Db::name("action_list")->insert($data);
  289. }
  290. if($update==false){
  291. Db::rollback();
  292. return error_show(1002,'功能新建失败');
  293. }
  294. $data = ['action_code'=>$code,'status'=>$status,"updatetime"=>date("Y-m-d H:i:s")];
  295. $result=Db::name("action")->where("id","=",$actid)->save($data);
  296. if($result){
  297. Db::commit();
  298. return app_show(0,"更新成功");
  299. }else{
  300. Db::rollback();
  301. return error_show(1004,"更新失败");
  302. }
  303. }catch (\Exception $e){
  304. Db::rollback();
  305. return error_show(1003,$e->getMessage());
  306. }
  307. }
  308. /** 菜单下功能信息状态修改
  309. * @return \think\response\Json|void
  310. * @throws \think\db\exception\DataNotFoundException
  311. * @throws \think\db\exception\DbException
  312. * @throws \think\db\exception\ModelNotFoundException
  313. * @throws \think\exception\DbException
  314. */
  315. public function ActionStatus(){
  316. $post =$this->post;
  317. $actid = isset($post['id']) ? intval($post['id']) : "";
  318. if($actid==""){
  319. return error_show(1001,'功能id不能为空');
  320. }
  321. $status = isset($post['status']) ? intval($post['status']) : 1;
  322. try{
  323. $data = ['status'=>$status,"updatetime"=>date("Y-m-d H:i:s")];
  324. $result=Db::name("action")->where("id","=",$actid)->save($data);
  325. if($result){
  326. return app_show(0,"更新成功");
  327. }else{
  328. return error_show(1004,"更新失败");
  329. }
  330. }catch (\Exception $e){
  331. return error_show(1003,$e->getMessage());
  332. }
  333. }
  334. /** 菜单下功能信息状态修改
  335. * @return \think\response\Json|void
  336. * @throws \think\exception\DbException
  337. */
  338. public function ActionAdd(){
  339. $post =$this->post;
  340. $pageid = isset($post['menuid']) ? intval($post['menuid']) : "";
  341. if($pageid==""){
  342. return error_show(1001,'页面id不能为空');
  343. }
  344. $code = isset($post['action_code']) ? trim($post['action_code']) : "";
  345. $action_name = isset($post['action_name']) ? trim($post['action_name']) : "";
  346. $status = isset($post['status']) ? intval($post['status']) : 1;
  347. if($code==""){
  348. return error_show(1002,'功能code不能为空');
  349. }
  350. if($action_name==""){
  351. return error_show(1002,'功能 action_name 不能为空');
  352. }
  353. Db::startTrans();
  354. try{
  355. $actioninfo =Db::name("action_list")->where(["action_code"=>$code,"is_del"=>0])->find();
  356. if($actioninfo){
  357. $update =Db::name("action_list")->where($actioninfo)->update(["action_name"=>$action_name,"updatetime"=>date("Y-m-d H:i:s")]);
  358. }else{
  359. $data=["action_name"=>$action_name,"action_code"=>$code,"addtime"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  360. $update = Db::name("action_list")->insert($data);
  361. }
  362. if($update==false){
  363. Db::rollback();
  364. return error_show(1002,'功能新建失败');
  365. }
  366. $where = ['menuid'=>$pageid,'action_code'=>$code,"is_del"=>0];
  367. $true =Db::name("action")->where($where)->find();
  368. if($true){
  369. Db::rollback();
  370. return error_show(1003,'此功能已存在');
  371. }else{
  372. $data = ['menuid'=>$pageid,'action_code'=>$code,'status'=>$status,"updatetime"=>date("Y-m-d H:i:s"),"addtime"=>date("Y-m-d H:i:s")];
  373. $in = Db::name("action")->insert($data);
  374. if($in){
  375. Db::commit();
  376. return app_show(0,"添加成功");
  377. }
  378. }
  379. Db::rollback();
  380. return error_show(1005,'功能新建失败');
  381. }catch (\Exception $e){
  382. Db::rollback();
  383. return error_show(1005,$e->getMessage());
  384. }
  385. }
  386. //菜单下功能删除
  387. public function ActionDel(){
  388. $post =$this->post;
  389. $actid = isset($post['id']) ? intval($post['id']) : "";
  390. if($actid==""){
  391. return error_show(1001,'功能id不能为空');
  392. }
  393. $act=Db::name("action")->where([["id","=",$actid],["is_del","=",0]])->find();
  394. if($act==false){
  395. return error_show(1004,'未找到相关数据');
  396. }
  397. try{
  398. $data = ['is_del'=>1,"updatetime"=>date("Y-m-d H:i:s")];
  399. $result=Db::name("action")->where($act)->update($data);
  400. if($result){
  401. return app_show(0,"删除成功");
  402. }else{
  403. return error_show(1004,"删除失败");
  404. }
  405. }catch (\Exception $e){
  406. return error_show(1003,$e->getMessage());
  407. }
  408. }
  409. }