45 lines
1.9 KiB
C#
45 lines
1.9 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
namespace Store.Users.Contracts.Dtos;
|
|
|
|
/// <summary>用户提交的充值备注。</summary>
|
|
public sealed class UserTopUpMessageRequest
|
|
{
|
|
/// <summary>目标账户;省略时为本人,指定时检查管理权限。</summary>
|
|
[StringLength(20)]
|
|
public string? UserName { get; set; }
|
|
[Required(ErrorMessage = "充值备注不能为空"), StringLength(1000)] public string Message { get; set; } = "";
|
|
}
|
|
|
|
public sealed class UserTopUpResultRequest
|
|
{
|
|
[Range(1, long.MaxValue, ErrorMessage = "ID必须大于0")]
|
|
public long Id { get; set; }
|
|
[Required(ErrorMessage = "处理结果不能为空"), StringLength(1000)] public string Result { get; set; } = "";
|
|
}
|
|
|
|
/// <summary>充值二维码;非空、长度与 Base64 格式由 DTO 检查。</summary>
|
|
public sealed class UserTopUpQrcodeRequest : IValidatableObject
|
|
{
|
|
[Required(ErrorMessage = "代理编码不能为空"), StringLength(50)] public string AgentCode { get; set; } = "";
|
|
[Required(ErrorMessage = "二维码不能为空"), StringLength(2097152)] public string QrcodeBase64 { get; set; } = "";
|
|
|
|
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
|
{
|
|
var content = QrcodeBase64;
|
|
if (content.StartsWith("data:image/", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
int comma = content.IndexOf(',');
|
|
if (comma < 0 || !content[..comma].EndsWith(";base64", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
yield return new ValidationResult("二维码格式错误", [nameof(QrcodeBase64)]);
|
|
yield break;
|
|
}
|
|
content = content[(comma + 1)..];
|
|
}
|
|
bool valid;
|
|
try { valid = Convert.FromBase64String(content).Length > 0; }
|
|
catch (FormatException) { valid = false; }
|
|
if (!valid) yield return new ValidationResult("二维码必须为有效的Base64数据", [nameof(QrcodeBase64)]);
|
|
}
|
|
}
|