MenuController.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2019-2020 Zheng Jie
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package easydo.technology.system.rest;
  17. import com.alibaba.fastjson.JSONObject;
  18. import easydo.technology.exception.BadRequestException;
  19. import easydo.technology.system.domain.Menu;
  20. import easydo.technology.system.dto.MenuDto;
  21. import easydo.technology.system.dto.MenuQueryParam;
  22. import easydo.technology.system.service.MenuService;
  23. import easydo.technology.utils.PageUtil;
  24. import easydo.technology.utils.SecurityUtils;
  25. import lombok.Delegate;
  26. import lombok.RequiredArgsConstructor;
  27. import org.springframework.http.HttpStatus;
  28. import org.springframework.http.ResponseEntity;
  29. import org.springframework.security.access.prepost.PreAuthorize;
  30. import org.springframework.validation.annotation.Validated;
  31. import org.springframework.web.bind.annotation.*;
  32. import javax.servlet.http.HttpServletResponse;
  33. import java.util.*;
  34. import java.util.stream.Collectors;
  35. /**
  36. * @author Zheng Jie
  37. * @date 2018-12-03
  38. */
  39. @RestController
  40. @RequiredArgsConstructor
  41. @RequestMapping("/menus")
  42. public class MenuController {
  43. private final MenuService menuService;
  44. // private final MenuMapper menuMapper;
  45. private static final String ENTITY_NAME = "menu";
  46. @GetMapping
  47. @PreAuthorize("@el.check('menu:list')")
  48. public ResponseEntity<Object> query(MenuQueryParam criteria) throws Exception {
  49. List<MenuDto> menuDtoList = menuService.queryAll(criteria, true);
  50. return new ResponseEntity<>(PageUtil.toPage(menuDtoList, menuDtoList.size()), HttpStatus.OK);
  51. }
  52. @GetMapping(value = "/lazy")
  53. @PreAuthorize("@el.check('menu:list','roles:list')")
  54. public ResponseEntity<Object> query(@RequestParam Long pid, @RequestParam String platformType) {
  55. return new ResponseEntity<>(menuService.getMenus(pid, platformType), HttpStatus.OK);
  56. }
  57. @GetMapping(value = "/build")
  58. public ResponseEntity<Object> buildMenus() {
  59. List<MenuDto> menuDtoList = menuService.findByUser(SecurityUtils.getCurrentUserId());
  60. List<MenuDto> menuDtos = menuService.buildTree(menuDtoList);
  61. return new ResponseEntity<>(menuService.buildMenus(menuDtos), HttpStatus.OK);
  62. }
  63. // @RequestMapping(value = "/appletsMenus")
  64. // @ApiOperation("获取前端所需小程序菜单")
  65. // public ResponseEntity<Object> appletsMenus() {
  66. // List<MenuDto> menuDtoList = menuService.findByUser(SecurityUtils.getCurrentUserId());
  67. // menuDtoList = menuDtoList.stream().filter(model -> model.getPlatformType().equals("easydo-mini-iot")).collect(Collectors.toList());
  68. // return new ResponseEntity<>(menuService.buildMenus(menuDtoList), HttpStatus.OK);
  69. // }
  70. // @GetMapping(value = "/buildTransfer")
  71. // public ResponseEntity<Object> buildTransfer() {
  72. // @ApiOperation("获取前端所需菜单")
  73. // List<MenuDto> menuDtoList = menuService.findByUser(SecurityUtils.getCurrentUserId(), "easydo-transfer");
  74. // menuDtoList = menuDtoList.stream().filter(model -> model.getType() < 2).collect(Collectors.toList());
  75. // List<MenuDto> menuDtos = menuService.buildTree(menuDtoList);
  76. // return new ResponseEntity<>(menuService.buildMenus(menuDtos), HttpStatus.OK);
  77. // }
  78. @PostMapping("/superior")
  79. @PreAuthorize("@el.check('menu:list')")
  80. public ResponseEntity<Object> getSuperior(@RequestBody List<Long> ids) throws Exception {
  81. Set<MenuDto> menuDtos = new LinkedHashSet<>();
  82. if (ids != null && !ids.isEmpty()) {
  83. for (Long id : ids) {
  84. MenuDto menuDto = menuService.findById(id);
  85. menuDtos.addAll(menuService.getSuperior(menuDto, new ArrayList<>()));
  86. }
  87. return new ResponseEntity<>(menuService.buildTree(new ArrayList<>(menuDtos)), HttpStatus.OK);
  88. }
  89. throw new Exception("参数缺失");
  90. }
  91. @PostMapping
  92. @PreAuthorize("@el.check('menu:add')")
  93. public ResponseEntity<Object> create(@Validated @RequestBody Menu resources) {
  94. if (resources.getId() != null) {
  95. throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
  96. }
  97. menuService.save(resources);
  98. return new ResponseEntity<>(resources, HttpStatus.OK);
  99. }
  100. @PutMapping
  101. @PreAuthorize("@el.check('menu:edit')")
  102. public ResponseEntity<Object> update(@Validated(Menu.Update.class) @RequestBody Menu resources) {
  103. menuService.updateById(resources);
  104. return new ResponseEntity<>(resources, HttpStatus.OK);
  105. }
  106. @DeleteMapping
  107. @PreAuthorize("@el.check('menu:del')")
  108. public ResponseEntity<Object> delete(@RequestBody Set<Long> ids) {
  109. Set<Menu> menuSet = new HashSet<>();
  110. List<Menu> menuList = new ArrayList<>();
  111. for (Long id : ids) {
  112. Menu menu = menuService.getById(id);
  113. menuList.add(menu);
  114. }
  115. menuSet = menuService.getDeleteMenus(menuList, menuSet);
  116. ids = menuSet.stream().map(Menu::getId).collect(Collectors.toSet());
  117. menuService.removeByIds(ids);
  118. return new ResponseEntity<>(HttpStatus.OK);
  119. }
  120. @GetMapping(value = "/download")
  121. @PreAuthorize("@el.check('menu:list')")
  122. public void download(HttpServletResponse response, MenuQueryParam criteria) throws Exception {
  123. menuService.download(menuService.queryAll(criteria, false), response);
  124. }
  125. }