This commit is contained in:
2025-10-14 20:41:48 +08:00
parent f4b7e6e2df
commit de519d073b
3 changed files with 62 additions and 11 deletions

View File

@@ -16,7 +16,7 @@ VerifyCode:
AccessKeyID: "LTAI5tKGB3TVJbMHSoZN3yr9" AccessKeyID: "LTAI5tKGB3TVJbMHSoZN3yr9"
AccessKeySecret: "OCQ30GWp4yENMjmfOAaagksE18bp65" AccessKeySecret: "OCQ30GWp4yENMjmfOAaagksE18bp65"
EndpointURL: "dysmsapi.aliyuncs.com" EndpointURL: "dysmsapi.aliyuncs.com"
SignName: "天远数据" SignName: "天远"
TemplateCode: "SMS_302641455" TemplateCode: "SMS_302641455"
ValidTime: 300 ValidTime: 300
Encrypt: Encrypt:

View File

@@ -2,16 +2,16 @@ package query
import ( import (
"context" "context"
"encoding/hex"
"encoding/json"
"fmt"
"time"
"tydata-server/app/main/api/internal/service" "tydata-server/app/main/api/internal/service"
"tydata-server/app/main/model" "tydata-server/app/main/model"
"tydata-server/common/ctxdata" "tydata-server/common/ctxdata"
"tydata-server/common/xerr" "tydata-server/common/xerr"
"tydata-server/pkg/lzkit/crypto" "tydata-server/pkg/lzkit/crypto"
"tydata-server/pkg/lzkit/validator" "tydata-server/pkg/lzkit/validator"
"encoding/hex"
"encoding/json"
"fmt"
"time"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/stores/redis" "github.com/zeromicro/go-zero/core/stores/redis"

View File

@@ -3,9 +3,12 @@ package validator
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/go-playground/validator/v10"
"regexp" "regexp"
"strconv"
"strings" "strings"
"time"
"github.com/go-playground/validator/v10"
) )
var validate *validator.Validate var validate *validator.Validate
@@ -101,12 +104,60 @@ func validDate(fl validator.FieldLevel) bool {
return matched return matched
} }
// 自定义身份证校验
// 自定义身份证校验(增强版)
// 校验规则:
// 1. 格式18位前6位地区码首位不为07-14位出生日期15-17位顺序码18位校验码
// 2. 出生日期必须合法(验证年月日有效性,包括闰年)
// 3. 校验码按照GB 11643-1999标准计算验证
func validIDCard(fl validator.FieldLevel) bool { func validIDCard(fl validator.FieldLevel) bool {
id := fl.Field().String() idCard := fl.Field().String()
validIDPattern := `^\d{17}(\d|X|x)$` // 匹配18位身份证号码
matched, _ := regexp.MatchString(validIDPattern, id) // 1. 基本格式验证:地区码(6位) + 年(4位) + 月(2位) + 日(2位) + 顺序码(3位) + 校验码(1位)
return matched 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 { func validBankCard(fl validator.FieldLevel) bool {