add 极光数据

This commit is contained in:
2026-01-06 16:37:31 +08:00
parent 4fcf370dcd
commit e61f03a2dd
19 changed files with 808 additions and 41 deletions

View File

@@ -186,6 +186,7 @@ func validateIDCardChecksum(idCard string) bool {
return byte(lastChar) == checksum
}
// validatePrice 价格验证
func validatePrice(fl validator.FieldLevel) bool {
price := fl.Field().Float()
@@ -559,18 +560,18 @@ func validateReturnURL(fl validator.FieldLevel) bool {
// validateEnterpriseName 企业名称验证器
func validateEnterpriseName(fl validator.FieldLevel) bool {
enterpriseName := fl.Field().String()
// 去除首尾空格
trimmedName := strings.TrimSpace(enterpriseName)
if trimmedName == "" {
return false
}
// 长度验证2-100个字符
if len(trimmedName) < 2 || len(trimmedName) > 100 {
return false
}
// 检查是否包含非法字符(允许中英文括号)
invalidChars := []string{
"`", "~", "!", "@", "#", "$", "%", "^", "&", "*",
@@ -581,13 +582,13 @@ func validateEnterpriseName(fl validator.FieldLevel) bool {
return false
}
}
// 必须包含至少一个中文字符或英文字母
hasValidChar := regexp.MustCompile(`[\p{Han}a-zA-Z]`).MatchString(trimmedName)
if !hasValidChar {
return false
}
// 验证企业名称的基本格式(支持各种类型的企业)
// 支持:有限公司、股份有限公司、工作室、个体工商户、合伙企业等
validSuffixes := []string{
@@ -598,7 +599,7 @@ func validateEnterpriseName(fl validator.FieldLevel) bool {
"Co.,Ltd", "Co., Ltd", "Ltd", "LLC", "Inc", "Corp",
"Company", "Studio", "Workshop", "Enterprise",
}
// 检查是否以合法的企业类型结尾(不强制要求,因为有些企业名称可能没有标准后缀)
// 但如果有后缀,必须是合法的
hasValidSuffix := false
@@ -613,7 +614,7 @@ func validateEnterpriseName(fl validator.FieldLevel) bool {
break
}
}
// 如果名称中包含常见的企业类型关键词,则必须是合法的后缀
enterpriseKeywords := []string{"公司", "工作室", "企业", "集团", "Co", "Ltd", "LLC", "Inc", "Corp", "Company", "Studio", "Workshop", "Enterprise"}
containsKeyword := false
@@ -623,12 +624,12 @@ func validateEnterpriseName(fl validator.FieldLevel) bool {
break
}
}
// 如果包含企业关键词但没有合法后缀,则验证失败
if containsKeyword && !hasValidSuffix {
return false
}
return true
}
@@ -657,7 +658,7 @@ func ValidatePassword(password string) error {
hasUpper := regexp.MustCompile(`[A-Z]`).MatchString(password)
hasLower := regexp.MustCompile(`[a-z]`).MatchString(password)
hasDigit := regexp.MustCompile(`\d`).MatchString(password)
if !hasUpper {
return fmt.Errorf("密码必须包含大写字母")
}
@@ -790,7 +791,7 @@ func ValidateRequired(value interface{}, fieldName string) error {
if value == nil {
return fmt.Errorf("%s不能为空", fieldName)
}
switch v := value.(type) {
case string:
if strings.TrimSpace(v) == "" {
@@ -801,7 +802,7 @@ func ValidateRequired(value interface{}, fieldName string) error {
return fmt.Errorf("%s不能为空", fieldName)
}
}
return nil
}
@@ -821,7 +822,7 @@ func ValidateSliceNotEmpty(slice interface{}, fieldName string) error {
if slice == nil {
return fmt.Errorf("%s不能为空", fieldName)
}
switch v := slice.(type) {
case []string:
if len(v) == 0 {
@@ -832,7 +833,7 @@ func ValidateSliceNotEmpty(slice interface{}, fieldName string) error {
return fmt.Errorf("%s不能为空", fieldName)
}
}
return nil
}
@@ -841,13 +842,13 @@ func ValidateEnterpriseName(enterpriseName string) error {
if enterpriseName == "" {
return fmt.Errorf("企业名称不能为空")
}
// 去除首尾空格
trimmedName := strings.TrimSpace(enterpriseName)
if trimmedName == "" {
return fmt.Errorf("企业名称不能为空")
}
// 长度验证2-100个字符
if len(trimmedName) < 2 {
return fmt.Errorf("企业名称长度不能少于2个字符")
@@ -855,7 +856,7 @@ func ValidateEnterpriseName(enterpriseName string) error {
if len(trimmedName) > 100 {
return fmt.Errorf("企业名称长度不能超过100个字符")
}
// 检查是否包含非法字符(允许中英文括号)
invalidChars := []string{
"`", "~", "!", "@", "#", "$", "%", "^", "&", "*",
@@ -866,13 +867,13 @@ func ValidateEnterpriseName(enterpriseName string) error {
return fmt.Errorf("企业名称不能包含特殊字符: %s", char)
}
}
// 必须包含至少一个中文字符或英文字母
hasValidChar := regexp.MustCompile(`[\p{Han}a-zA-Z]`).MatchString(trimmedName)
if !hasValidChar {
return fmt.Errorf("企业名称必须包含中文字符或英文字母")
}
// 验证企业名称的基本格式(支持各种类型的企业)
// 支持:有限公司、股份有限公司、工作室、个体工商户、合伙企业等
validSuffixes := []string{
@@ -883,7 +884,7 @@ func ValidateEnterpriseName(enterpriseName string) error {
"Co.,Ltd", "Co., Ltd", "Ltd", "LLC", "Inc", "Corp",
"Company", "Studio", "Workshop", "Enterprise",
}
// 检查是否以合法的企业类型结尾
hasValidSuffix := false
for _, suffix := range validSuffixes {
@@ -897,7 +898,7 @@ func ValidateEnterpriseName(enterpriseName string) error {
break
}
}
// 如果名称中包含常见的企业类型关键词,则必须是合法的后缀
enterpriseKeywords := []string{"公司", "工作室", "企业", "集团", "Co", "Ltd", "LLC", "Inc", "Corp", "Company", "Studio", "Workshop", "Enterprise"}
containsKeyword := false
@@ -907,12 +908,12 @@ func ValidateEnterpriseName(enterpriseName string) error {
break
}
}
// 如果包含企业关键词但没有合法后缀,则验证失败
if containsKeyword && !hasValidSuffix {
return fmt.Errorf("企业名称格式不正确,请使用标准的企业类型后缀(如:有限公司、工作室等)")
}
return nil
}
@@ -922,9 +923,9 @@ func ValidateEnterpriseName(enterpriseName string) error {
func NewBusinessValidator() *BusinessValidator {
// 确保全局校验器已初始化
InitGlobalValidator()
return &BusinessValidator{
validator: GetGlobalValidator(), // 使用全局校验器
validator: GetGlobalValidator(), // 使用全局校验器
}
}
@@ -946,29 +947,29 @@ func (bv *BusinessValidator) ValidateField(field interface{}, tag string) error
// validateBase64Image Base64图片格式验证器JPG、BMP、PNG
func validateBase64Image(fl validator.FieldLevel) bool {
base64Str := fl.Field().String()
// 如果为空,由 omitempty 处理
if base64Str == "" {
return true
}
// 去除首尾空格
base64Str = strings.TrimSpace(base64Str)
if base64Str == "" {
return false
}
// 解码 base64 字符串
decoded, err := base64.StdEncoding.DecodeString(base64Str)
if err != nil {
return false
}
// 检查数据长度(至少要有文件头)
if len(decoded) < 4 {
return false
}
// 检查文件头,判断图片格式
// JPG: FF D8 FF
// PNG: 89 50 4E 47
@@ -977,16 +978,16 @@ func validateBase64Image(fl validator.FieldLevel) bool {
// JPG格式
return true
}
if len(decoded) >= 4 && decoded[0] == 0x89 && decoded[1] == 0x50 && decoded[2] == 0x4E && decoded[3] == 0x47 {
// PNG格式
return true
}
if len(decoded) >= 2 && decoded[0] == 0x42 && decoded[1] == 0x4D {
// BMP格式
return true
}
return false
}