diff --git a/backend/src/main/java/com/qqchen/deploy/backend/framework/enums/ResponseCode.java b/backend/src/main/java/com/qqchen/deploy/backend/framework/enums/ResponseCode.java index f8a1db4c..3542e187 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/framework/enums/ResponseCode.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/framework/enums/ResponseCode.java @@ -235,7 +235,13 @@ public enum ResponseCode { NOTIFICATION_TEMPLATE_NOT_FOUND(3100, "notification.template.not.found"), NOTIFICATION_TEMPLATE_CODE_EXISTS(3101, "notification.template.code.exists"), NOTIFICATION_TEMPLATE_DISABLED(3102, "notification.template.disabled"), - NOTIFICATION_TEMPLATE_RENDER_ERROR(3103, "notification.template.render.error"); + NOTIFICATION_TEMPLATE_RENDER_ERROR(3103, "notification.template.render.error"), + + // 通知渠道相关错误码 (3120-3139) + NOTIFICATION_CHANNEL_NOT_FOUND(3120, "notification.channel.not.found"), + NOTIFICATION_CHANNEL_DISABLED(3121, "notification.channel.disabled"), + NOTIFICATION_CHANNEL_CONFIG_ERROR(3122, "notification.channel.config.error"), + NOTIFICATION_SEND_FAILED(3123, "notification.send.failed"); private final int code; private final String messageKey; // 国际化消息key diff --git a/backend/src/main/java/com/qqchen/deploy/backend/notification/entity/config/BaseTemplateConfig.java b/backend/src/main/java/com/qqchen/deploy/backend/notification/entity/config/BaseTemplateConfig.java index 6199f4f9..fce5a8f9 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/notification/entity/config/BaseTemplateConfig.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/notification/entity/config/BaseTemplateConfig.java @@ -1,16 +1,29 @@ package com.qqchen.deploy.backend.notification.entity.config; +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.qqchen.deploy.backend.notification.enums.NotificationChannelTypeEnum; import lombok.Data; /** * 模板配置基类 * 用于实体层,与DTO层的BaseTemplateConfigDTO对应 + * + * 使用 PROPERTY 方式,通过 channelType 字段进行多态反序列化 * * @author qqchen * @since 2025-11-12 */ @Data +@JsonTypeInfo( + use = JsonTypeInfo.Id.NAME, + include = JsonTypeInfo.As.PROPERTY, + property = "channelType" +) +@JsonSubTypes({ + @JsonSubTypes.Type(value = WeworkTemplateConfig.class, name = "WEWORK"), + @JsonSubTypes.Type(value = EmailTemplateConfig.class, name = "EMAIL") +}) public abstract class BaseTemplateConfig { /** diff --git a/backend/src/main/java/com/qqchen/deploy/backend/notification/entity/config/EmailTemplateConfig.java b/backend/src/main/java/com/qqchen/deploy/backend/notification/entity/config/EmailTemplateConfig.java index 7c23d85d..fd587ffb 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/notification/entity/config/EmailTemplateConfig.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/notification/entity/config/EmailTemplateConfig.java @@ -16,6 +16,11 @@ import lombok.EqualsAndHashCode; @EqualsAndHashCode(callSuper = true) public class EmailTemplateConfig extends BaseTemplateConfig { + /** + * 渠道类型(用于Jackson反序列化) + */ + private final NotificationChannelTypeEnum channelType = NotificationChannelTypeEnum.EMAIL; + /** * 内容类型 */ diff --git a/backend/src/main/java/com/qqchen/deploy/backend/notification/entity/config/WeworkTemplateConfig.java b/backend/src/main/java/com/qqchen/deploy/backend/notification/entity/config/WeworkTemplateConfig.java index 5c13f502..805c8947 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/notification/entity/config/WeworkTemplateConfig.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/notification/entity/config/WeworkTemplateConfig.java @@ -15,6 +15,11 @@ import lombok.EqualsAndHashCode; @EqualsAndHashCode(callSuper = true) public class WeworkTemplateConfig extends BaseTemplateConfig { + /** + * 渠道类型(用于Jackson反序列化) + */ + private final NotificationChannelTypeEnum channelType = NotificationChannelTypeEnum.WEWORK; + /** * 消息类型 */ diff --git a/backend/src/main/java/com/qqchen/deploy/backend/notification/service/impl/NotificationChannelServiceImpl.java b/backend/src/main/java/com/qqchen/deploy/backend/notification/service/impl/NotificationChannelServiceImpl.java index 8b6b4aa1..c52b9b50 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/notification/service/impl/NotificationChannelServiceImpl.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/notification/service/impl/NotificationChannelServiceImpl.java @@ -126,11 +126,11 @@ public class NotificationChannelServiceImpl // 2. 查询渠道配置 NotificationChannel channel = notificationChannelRepository.findById(request.getChannelId()) - .orElseThrow(() -> new BusinessException(ResponseCode.DATA_NOT_FOUND)); + .orElseThrow(() -> new BusinessException(ResponseCode.NOTIFICATION_CHANNEL_NOT_FOUND)); // 3. 校验渠道状态 if (!channel.getEnabled()) { - throw new BusinessException(ResponseCode.DATA_NOT_FOUND); + throw new BusinessException(ResponseCode.NOTIFICATION_CHANNEL_DISABLED); } // 4. 获取对应的适配器 @@ -153,9 +153,13 @@ public class NotificationChannelServiceImpl adapter.send(config, request); log.info("通知发送成功 - 渠道ID: {}", channel.getId()); + } catch (IllegalArgumentException e) { + // 配置错误(如 Webhook Key 未配置) + log.error("通知渠道配置错误 - 渠道ID: {}, 错误: {}", channel.getId(), e.getMessage()); + throw new BusinessException(ResponseCode.NOTIFICATION_CHANNEL_CONFIG_ERROR, new Object[]{e.getMessage()}); } catch (Exception e) { log.error("通知发送失败 - 渠道ID: {}, 错误: {}", channel.getId(), e.getMessage(), e); - throw new BusinessException(ResponseCode.ERROR, new Object[]{e.getMessage()}); + throw new BusinessException(ResponseCode.NOTIFICATION_SEND_FAILED, new Object[]{e.getMessage()}); } } diff --git a/backend/src/main/resources/messages.properties b/backend/src/main/resources/messages.properties index ee4f37f3..0f49715a 100644 --- a/backend/src/main/resources/messages.properties +++ b/backend/src/main/resources/messages.properties @@ -257,4 +257,10 @@ deploy.record.not.found=部署记录不存在 notification.template.not.found=通知模板不存在或已删除 notification.template.code.exists=模板编码{0}已存在 notification.template.disabled=通知模板已禁用 -notification.template.render.error=模板渲染失败 \ No newline at end of file +notification.template.render.error=模板渲染失败 + +# 通知渠道相关 (3120-3139) +notification.channel.not.found=通知渠道不存在或已删除 +notification.channel.disabled=通知渠道已禁用 +notification.channel.config.error=通知渠道配置错误:{0} +notification.send.failed=通知发送失败:{0} diff --git a/backend/src/main/resources/messages_en_US.properties b/backend/src/main/resources/messages_en_US.properties index dcb6fa6c..c6e6ea12 100644 --- a/backend/src/main/resources/messages_en_US.properties +++ b/backend/src/main/resources/messages_en_US.properties @@ -185,3 +185,15 @@ deploy.permission.denied=No permission to deploy application in this environment deploy.environment.locked=Environment is locked, deployment prohibited deploy.approval.required=Approval required for deployment in this environment deploy.record.not.found=Deployment record not found + +# Notification Template Related (3100-3119) +notification.template.not.found=Notification template not found or has been deleted +notification.template.code.exists=Template code {0} already exists +notification.template.disabled=Notification template is disabled +notification.template.render.error=Template rendering failed + +# Notification Channel Related (3120-3139) +notification.channel.not.found=Notification channel not found or has been deleted +notification.channel.disabled=Notification channel is disabled +notification.channel.config.error=Notification channel configuration error: {0} +notification.send.failed=Notification send failed: {0} diff --git a/backend/src/main/resources/messages_zh_CN.properties b/backend/src/main/resources/messages_zh_CN.properties index 05507ad8..555459d4 100644 --- a/backend/src/main/resources/messages_zh_CN.properties +++ b/backend/src/main/resources/messages_zh_CN.properties @@ -184,4 +184,16 @@ deploy.already.running=该应用正在部署中,请等待当前部署完成 deploy.permission.denied=无权限在此环境部署应用 deploy.environment.locked=环境已锁定,禁止部署 deploy.approval.required=该环境需要审批才能部署 -deploy.record.not.found=部署记录不存在 \ No newline at end of file +deploy.record.not.found=部署记录不存在 + +# 通知模板相关 (3100-3119) +notification.template.not.found=通知模板不存在或已删除 +notification.template.code.exists=模板编码{0}已存在 +notification.template.disabled=通知模板已禁用 +notification.template.render.error=模板渲染失败 + +# 通知渠道相关 (3120-3139) +notification.channel.not.found=通知渠道不存在或已删除 +notification.channel.disabled=通知渠道已禁用 +notification.channel.config.error=通知渠道配置错误:{0} +notification.send.failed=通知发送失败:{0}