feat: 统一开放接口网关微服务初始化工程 (OAuth2.0 + MCP + OpenAPI + Webhook)
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
package com.iot.gateway.admin;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.iot.gateway.common.BizException;
|
||||
import com.iot.gateway.common.Result;
|
||||
import com.iot.gateway.entity.OpenWebhook;
|
||||
import com.iot.gateway.mapper.OpenWebhookMapper;
|
||||
import com.iot.gateway.webhook.WebhookDispatchService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import java.util.Map;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Slf4j
|
||||
@Tag(name = "开放平台管理 - Webhook 通道管理")
|
||||
@RestController
|
||||
@RequestMapping("/api/open/webhook")
|
||||
public class OpenWebhookAdminController {
|
||||
|
||||
@Autowired
|
||||
private OpenWebhookMapper webhookMapper;
|
||||
|
||||
@Autowired
|
||||
private WebhookDispatchService dispatchService;
|
||||
|
||||
@Operation(summary = "Webhook 通道分页列表")
|
||||
@GetMapping("/page")
|
||||
public Result<Page<OpenWebhook>> page(
|
||||
@RequestParam(defaultValue = "1") Integer pageNum,
|
||||
@RequestParam(defaultValue = "10") Integer pageSize,
|
||||
@RequestParam(required = false) Long tenantId,
|
||||
@RequestParam(required = false) String keyword) {
|
||||
|
||||
Page<OpenWebhook> page = new Page<>(pageNum, pageSize);
|
||||
LambdaQueryWrapper<OpenWebhook> qw = new LambdaQueryWrapper<>();
|
||||
if (tenantId != null) {
|
||||
qw.eq(OpenWebhook::getTenantId, tenantId);
|
||||
}
|
||||
if (StringUtils.hasText(keyword)) {
|
||||
qw.and(w -> w.like(OpenWebhook::getChannelName, keyword)
|
||||
.or().like(OpenWebhook::getWebhookUrl, keyword));
|
||||
}
|
||||
qw.orderByDesc(OpenWebhook::getCreateTime);
|
||||
return Result.success(webhookMapper.selectPage(page, qw));
|
||||
}
|
||||
|
||||
@Operation(summary = "新建 Webhook 通道")
|
||||
@PostMapping
|
||||
public Result<OpenWebhook> create(@RequestBody OpenWebhook req) {
|
||||
req.setId(null);
|
||||
if (req.getTenantId() == null) req.setTenantId(1L);
|
||||
if (!StringUtils.hasText(req.getEvents())) req.setEvents("device.alarm");
|
||||
if (req.getStatus() == null) req.setStatus(1);
|
||||
|
||||
webhookMapper.insert(req);
|
||||
return Result.success(req);
|
||||
}
|
||||
|
||||
@Operation(summary = "修改 Webhook 通道")
|
||||
@PutMapping("/{id}")
|
||||
public Result<Void> update(@PathVariable Long id, @RequestBody OpenWebhook req) {
|
||||
OpenWebhook item = webhookMapper.selectById(id);
|
||||
if (item == null) throw new BizException("通道不存在");
|
||||
|
||||
item.setChannelName(req.getChannelName());
|
||||
item.setChannelType(req.getChannelType());
|
||||
item.setWebhookUrl(req.getWebhookUrl());
|
||||
item.setSecret(req.getSecret());
|
||||
if (req.getEvents() != null) item.setEvents(req.getEvents());
|
||||
if (req.getStatus() != null) item.setStatus(req.getStatus());
|
||||
item.setRemark(req.getRemark());
|
||||
|
||||
webhookMapper.updateById(item);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "删除 Webhook 通道")
|
||||
@DeleteMapping("/{id}")
|
||||
public Result<Void> delete(@PathVariable Long id) {
|
||||
webhookMapper.deleteById(id);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "测试 Webhook 推送连通性")
|
||||
@PostMapping("/{id}/test")
|
||||
public Result<Map<String, Object>> testSend(@PathVariable Long id, @RequestBody(required = false) Map<String, String> body) {
|
||||
OpenWebhook item = webhookMapper.selectById(id);
|
||||
if (item == null) throw new BizException("通道不存在");
|
||||
|
||||
String title = body != null && body.containsKey("title") ? body.get("title") : "【IoT平台测试】告警通知";
|
||||
String content = body != null && body.containsKey("content") ? body.get("content") : "这是一条来自开放平台的连通性测试消息。";
|
||||
|
||||
boolean ok = dispatchService.sendDirect(item.getChannelType(), item.getWebhookUrl(), title, content);
|
||||
return Result.success(Map.of("success", ok, "message", ok ? "测试消息已成功送达" : "发送失败,请检查 URL 与网络配置"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user