diff --git a/app/main/api/etc/main.yaml b/app/main/api/etc/main.yaml index 7801a2b..0bbdb6e 100644 --- a/app/main/api/etc/main.yaml +++ b/app/main/api/etc/main.yaml @@ -16,7 +16,7 @@ VerifyCode: AccessKeyID: "LTAI5tKGB3TVJbMHSoZN3yr9" AccessKeySecret: "OCQ30GWp4yENMjmfOAaagksE18bp65" EndpointURL: "dysmsapi.aliyuncs.com" - SignName: "天远数据" + SignName: "天远查" TemplateCode: "SMS_302641455" ValidTime: 300 Encrypt: diff --git a/app/main/api/internal/logic/query/queryservicelogic.go b/app/main/api/internal/logic/query/queryservicelogic.go index 7014b85..e8b8109 100644 --- a/app/main/api/internal/logic/query/queryservicelogic.go +++ b/app/main/api/internal/logic/query/queryservicelogic.go @@ -2,16 +2,16 @@ package query import ( "context" + "encoding/hex" + "encoding/json" + "fmt" + "time" "tydata-server/app/main/api/internal/service" "tydata-server/app/main/model" "tydata-server/common/ctxdata" "tydata-server/common/xerr" "tydata-server/pkg/lzkit/crypto" "tydata-server/pkg/lzkit/validator" - "encoding/hex" - "encoding/json" - "fmt" - "time" "github.com/pkg/errors" "github.com/zeromicro/go-zero/core/stores/redis" diff --git a/pkg/lzkit/validator/validator.go b/pkg/lzkit/validator/validator.go index 15e17b9..594696f 100644 --- a/pkg/lzkit/validator/validator.go +++ b/pkg/lzkit/validator/validator.go @@ -3,9 +3,12 @@ package validator import ( "errors" "fmt" - "github.com/go-playground/validator/v10" "regexp" + "strconv" "strings" + "time" + + "github.com/go-playground/validator/v10" ) var validate *validator.Validate @@ -101,12 +104,60 @@ func validDate(fl validator.FieldLevel) bool { return matched } -// 自定义身份证校验 + +// 自定义身份证校验(增强版) +// 校验规则: +// 1. 格式:18位,前6位地区码(首位不为0),7-14位出生日期,15-17位顺序码,18位校验码 +// 2. 出生日期必须合法(验证年月日有效性,包括闰年) +// 3. 校验码按照GB 11643-1999标准计算验证 func validIDCard(fl validator.FieldLevel) bool { - id := fl.Field().String() - validIDPattern := `^\d{17}(\d|X|x)$` // 匹配18位身份证号码 - matched, _ := regexp.MatchString(validIDPattern, id) - return matched + idCard := fl.Field().String() + + // 1. 基本格式验证:地区码(6位) + 年(4位) + 月(2位) + 日(2位) + 顺序码(3位) + 校验码(1位) + validIDPattern := `^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[\dXx]$` + matched, _ := regexp.MatchString(validIDPattern, idCard) + if !matched { + return false + } + + // 2. 验证出生日期的合法性 + year, _ := strconv.Atoi(idCard[6:10]) + month, _ := strconv.Atoi(idCard[10:12]) + day, _ := strconv.Atoi(idCard[12:14]) + + // 构造日期并验证是否合法(time包会自动处理闰年等情况) + birthDate := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC) + if birthDate.Year() != year || int(birthDate.Month()) != month || birthDate.Day() != day { + return false // 日期不合法,如2月30日、4月31日等 + } + + // 3. 验证校验码(按照GB 11643-1999标准) + return validateIDCardChecksum(idCard) +} + +// 验证身份证校验码(GB 11643-1999标准) +func validateIDCardChecksum(idCard string) bool { + // 加权因子 + weights := []int{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2} + // 校验码对应值 + checksumChars := []byte{'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'} + + sum := 0 + for i := 0; i < 17; i++ { + num := int(idCard[i] - '0') + sum += num * weights[i] + } + + // 计算校验码 + checksum := checksumChars[sum%11] + lastChar := idCard[17] + + // 支持小写x + if lastChar == 'x' { + lastChar = 'X' + } + + return byte(lastChar) == checksum } func validBankCard(fl validator.FieldLevel) bool {