InterfaceInfoController.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package space.anyi.openAPI.controller;
  2. import cn.hutool.core.util.IdUtil;
  3. import io.swagger.annotations.ApiImplicitParam;
  4. import io.swagger.annotations.ApiImplicitParams;
  5. import org.springframework.security.access.prepost.PreAuthorize;
  6. import org.springframework.web.bind.annotation.*;
  7. import space.anyi.openAPI.model.PageVO;
  8. import space.anyi.openAPI.model.ResponseResult;
  9. import space.anyi.openAPI.model.commom.RequestID;
  10. import space.anyi.openAPI.model.interfaceInfo.InterfaceInfo;
  11. import space.anyi.openAPI.model.interfaceInfo.InterfaceStatus;
  12. import space.anyi.openAPI.model.interfaceInfo.dto.RequestAddInterfaceInfo;
  13. import space.anyi.openAPI.model.interfaceInfo.dto.RequestPageInterfaceInfo;
  14. import space.anyi.openAPI.model.interfaceInfo.dto.RequestUpdateInterfaceInfo;
  15. import space.anyi.openAPI.model.interfaceInfo.vo.InterfaceInfoVO;
  16. import space.anyi.openAPI.service.InterfaceInfoService;
  17. import space.anyi.openAPI.util.BeanCopyUtil;
  18. import space.anyi.openAPI.util.SecurityUtils;
  19. import javax.annotation.Resource;
  20. import java.util.List;
  21. import java.util.Objects;
  22. /**
  23. * @ProjectName: yangyi-open-api-platform
  24. * @FileName: InterfaceInfoController
  25. * @Author: 杨逸
  26. * @Data:2025/10/14 19:52
  27. * @Description:
  28. */
  29. @RestController
  30. @RequestMapping("/interfaceInfo")
  31. public class InterfaceInfoController {
  32. @Resource
  33. private InterfaceInfoService interfaceInfoService;
  34. @PostMapping
  35. @PreAuthorize("@ps.hasRole('管理员')")
  36. public ResponseResult add(@RequestBody RequestAddInterfaceInfo requestAddInterfaceInfo){
  37. long id = IdUtil.getSnowflake(1, 1).nextId();
  38. InterfaceInfo interfaceInfo = BeanCopyUtil.copyBean(requestAddInterfaceInfo, InterfaceInfo.class);
  39. interfaceInfo.setId(id);
  40. Long userId = SecurityUtils.getUserId();
  41. interfaceInfo.setUserId(userId);
  42. interfaceInfoService.save(interfaceInfo);
  43. return ResponseResult.okResult();
  44. }
  45. @DeleteMapping()
  46. @PreAuthorize("@ps.hasRole('管理员')")
  47. public ResponseResult delete(List<Long> ids){
  48. interfaceInfoService.removeByIds(ids);
  49. return ResponseResult.okResult();
  50. }
  51. @PutMapping
  52. @PreAuthorize("@ps.hasRole('管理员')")
  53. public ResponseResult update(@RequestBody RequestUpdateInterfaceInfo requestUpdateInterfaceInfo){
  54. InterfaceInfo interfaceInfo = BeanCopyUtil.copyBean(requestUpdateInterfaceInfo, InterfaceInfo.class);
  55. interfaceInfoService.updateById(interfaceInfo);
  56. return ResponseResult.okResult();
  57. }
  58. @GetMapping
  59. public ResponseResult list(RequestPageInterfaceInfo requestPageInterfaceInfo){
  60. PageVO pageVO = interfaceInfoService.listPage(requestPageInterfaceInfo);
  61. return ResponseResult.okResult(pageVO);
  62. }
  63. @PutMapping("/online")
  64. @PreAuthorize("@ps.hasRole('管理员')")
  65. public ResponseResult onlineInterface(@RequestBody RequestID requestID){
  66. //校验
  67. //接口是否存在
  68. InterfaceInfo interfaceInfo = interfaceInfoService.getById(requestID.getId());
  69. if (Objects.isNull(interfaceInfo)) {
  70. return ResponseResult.errorResult(ResponseResult.AppHttpCodeEnum.INTERFACE_NOT_FOUND);
  71. }
  72. //是否为当前用户创建
  73. if (!SecurityUtils.getUserId().equals(interfaceInfo.getUserId())) {
  74. return ResponseResult.errorResult(ResponseResult.AppHttpCodeEnum.NO_OPERATOR_AUTH);
  75. }
  76. //todo:是否可以调用
  77. //更新状态
  78. interfaceInfo.setStatus(InterfaceStatus.online.getStatus());
  79. interfaceInfoService.updateById(interfaceInfo);
  80. return ResponseResult.okResult();
  81. }
  82. @PutMapping("/offline")
  83. @PreAuthorize("@ps.hasRole('管理员')")
  84. public ResponseResult offlineInterface(@RequestBody RequestID requestID){
  85. //校验
  86. //接口是否存在
  87. InterfaceInfo interfaceInfo = interfaceInfoService.getById(requestID.getId());
  88. if (Objects.isNull(interfaceInfo)) {
  89. return ResponseResult.errorResult(ResponseResult.AppHttpCodeEnum.INTERFACE_NOT_FOUND);
  90. }
  91. //是否为当前用户创建
  92. if (!SecurityUtils.getUserId().equals(interfaceInfo.getUserId())) {
  93. return ResponseResult.errorResult(ResponseResult.AppHttpCodeEnum.NO_OPERATOR_AUTH);
  94. }
  95. //更新状态
  96. interfaceInfo.setStatus(InterfaceStatus.offline.getStatus());
  97. interfaceInfoService.updateById(interfaceInfo);
  98. return ResponseResult.okResult();
  99. }
  100. @PreAuthorize("@ps.hasRole('用户')")
  101. @GetMapping("/{id}")
  102. @ApiImplicitParams({
  103. @ApiImplicitParam(name = "id", value = "接口信息id", required = true, dataType = "long", paramType = "path")
  104. })
  105. public ResponseResult getInterfaceInfoById(@PathVariable Long id){
  106. InterfaceInfo interfaceInfo = interfaceInfoService.getById(id);
  107. InterfaceInfoVO vo = BeanCopyUtil.copyBean(interfaceInfo, InterfaceInfoVO.class);
  108. return ResponseResult.okResult(vo);
  109. }
  110. }