using FluentValidation; using MessageService.Domain.Enums; using MessageService.WebApi.Application.Message; namespace MessageService.WebApi.Controllers.Message { public class MessageSendRequest { // 基础元数据 public Guid ClientMsgId { get; init; } public Guid TargetId { get; init; } public ChatType ChatType { get; init; } public MessageType MsgType { get; init; } // 业务属性 public Guid? QuoteMessageId { get; init; } public Dictionary? Ext { get; init; } // 载荷数据(根据 MsgType 选择性填充) public string? Text { get; init; } public string? Url { get; init; } public int? Width { get; init; } public int? Height { get; init; } public string? Thumb { get; init; } public int? Duration { get; init; } public SendMsgCommand ToCommand(Guid senderId) { return new SendMsgCommand( senderId, TargetId, ChatType, MsgType, ClientMsgId, QuoteMessageId, Ext, Text, Url, Width, Height, Thumb, Duration ); } } public class MessageSendRequestValidator : AbstractValidator { public MessageSendRequestValidator() { RuleFor(r => r.ClientMsgId) .NotNull() .NotEmpty(); RuleFor(r => r.TargetId) .NotEmpty() .NotNull(); // 文本消息:text 必填 When(r => r.MsgType == MessageType.Text, () => { RuleFor(r => r.Text).NotEmpty().WithMessage("文本消息的 text 字段不能为空"); }); // 图片/视频/语音消息:url 必填 When(r => r.MsgType == MessageType.Image || r.MsgType == MessageType.Video || r.MsgType == MessageType.Voice || r.MsgType == MessageType.File, () => { RuleFor(r => r.Url).NotEmpty().WithMessage("媒体消息的 url 字段不能为空"); }); } } }