using System.ComponentModel.DataAnnotations; using Store.Users.Contracts.Dtos; using Store.Users.Dtos; namespace Store.Users.Services; /// User 服务共用的手动参数检查。DTO 只保留非空规则,其余检查在服务层执行。 internal static class UserInputChecks { public static void Length(string? value, int max, string name, int min = 0) { if (value != null && (value.Length < min || value.Length > max)) throw new ValidationException(min > 0 ? $"{name}长度必须为{min}-{max}个字符" : $"{name}长度不能超过{max}个字符"); } public static void PositiveId(long id) { if (id <= 0) throw new ValidationException("ID必须大于0"); } private static void Group(byte? group) { if (group.HasValue && group.Value is not (3 or 4 or 5 or 8 or 9 or 10 or 11)) throw new ValidationException("用户分组必须为3、4、5、8、9、10或11"); } public static void Edit(UserEditRequest request) { Length(request.NickName, 50, "昵称"); Length(request.Mobile, 30, "手机号"); Length(request.CompanyName, 100, "公司名称"); Length(request.Notes, 255, "备注"); Length(request.CardId, 50, "卡号"); Length(request.Authority, 1000, "权限"); Length(request.ForbiddenPage, 1000, "禁止页面"); Length(request.OemId, 50, "OEM编号"); Group(request.GroupId); if (request.SaLevel.HasValue && request.SaLevel.Value is not (0 or 1)) throw new ValidationException("管理员级别只能为0或1"); if (request.ShopPointsPerMonth < 30) throw new ValidationException("每月续费积分不能低于30"); if (request.PostagePerOrder is < 2.2m or > 999999m) throw new ValidationException("每单费用必须为2.2-999999元"); if (request.RefundPostagePerOrder is < 2.5m or > 999999m) throw new ValidationException("退货费用必须为2.5-999999元"); if (request.GiftOemAmountPerOrder is < 0m or > 999999m) throw new ValidationException("礼物附加费用必须为0-999999元"); if (request.SyncLogisticsPerOrder is < 0m or > 999999m) throw new ValidationException("物流同步费用必须为0-999999元"); } public static void Search(UserSearchRequest request) { if (request.Page is < 1 or > 1000000) throw new ValidationException("页码必须为1-1000000"); if (request.PageSize is < 1 or > 200) throw new ValidationException("每页条数必须为1-200"); Length(request.Keyword, 100, "查询关键字"); Group(request.GroupId); } public static void Records(UserRecordSearchRequest request) { Search(request); Length(request.Operation, 20, "操作类型"); Length(request.ShopId, 50, "店铺ID"); Length(request.OrderId, 100, "订单ID"); if (request.StartTime.HasValue && request.EndTime.HasValue && request.StartTime > request.EndTime) throw new ValidationException("开始时间不能晚于结束时间"); } public static void Renew(ShopRenewRequest request) { Length(request.ShopId.Trim(), 50, "店铺ID"); if (request.RenewDuration is < 1 or > 120) throw new ValidationException("续费时长必须为1-120个月"); Length(request.Remark?.Trim(), 255, "备注"); } public static void Qrcode(UserTopUpQrcodeRequest request) { Length(request.AgentCode, 50, "代理编码"); Length(request.QrcodeBase64, 2 * 1024 * 1024, "二维码"); var content = request.QrcodeBase64; if (content.StartsWith("data:image/", StringComparison.OrdinalIgnoreCase)) { int comma = content.IndexOf(','); if (comma < 0 || !content[..comma].EndsWith(";base64", StringComparison.OrdinalIgnoreCase)) throw new ValidationException("二维码格式错误"); content = content[(comma + 1)..]; } bool valid; try { valid = Convert.FromBase64String(content).Length > 0; } catch (FormatException) { valid = false; } if (!valid) throw new ValidationException("二维码必须为有效的Base64数据"); } }