This commit is contained in:
guanguans 2021-01-11 11:53:22 +08:00
parent 60b29f571b
commit cf2ada4860
4 changed files with 30 additions and 30 deletions

View File

@ -60,10 +60,8 @@ func GenerateLongType(id string) map[string]string {
// 检查地址码 // 检查地址码
func CheckAddressCode(addressCode string, birthdayCode string) bool { func CheckAddressCode(addressCode string, birthdayCode string) bool {
addressInfo := GetAddressInfo(addressCode, birthdayCode) addressInfo := GetAddressInfo(addressCode, birthdayCode)
if addressInfo["province"] == "" {
return false return addressInfo["province"] != ""
}
return true
} }
// 检查出生日期码 // 检查出生日期码
@ -74,11 +72,8 @@ func CheckBirthdayCode(birthdayCode string) bool {
} }
_, error := time.Parse("20060102", birthdayCode) _, error := time.Parse("20060102", birthdayCode)
if error != nil {
return false
}
return true return error == nil
} }
// 检查顺序码 // 检查顺序码

View File

@ -3,16 +3,16 @@ package data
// 生肖信息 // 生肖信息
var ChineseZodiac = [12]string{ var ChineseZodiac = [12]string{
"子鼠", "子鼠",
"丑牛", "丑牛",
"寅虎", "寅虎",
"卯兔", "卯兔",
"辰龙", "辰龙",
"巳蛇", "巳蛇",
"午马", "午马",
"未羊", "未羊",
"申猴", "申猴",
"酉鸡", "酉鸡",
"戌狗", "戌狗",
"亥猪", "亥猪",
} }

View File

@ -1,15 +1,13 @@
package id_validator package id_validator
import ( import (
"fmt" "id-validator/data"
"math" "math"
"math/rand" "math/rand"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"id-validator/data"
) )
// 生成 Bit 码 // 生成 Bit 码
@ -101,7 +99,7 @@ func GetRandAddressCode(pattern string) string {
mustCompile := regexp.MustCompile(pattern) mustCompile := regexp.MustCompile(pattern)
var keys []string var keys []string
for key, _ := range data.AddressCode { for key := range data.AddressCode {
keyStr := strconv.Itoa(key) keyStr := strconv.Itoa(key)
if mustCompile.MatchString(keyStr) && Substr(keyStr, 4, 6) != "00" { if mustCompile.MatchString(keyStr) && Substr(keyStr, 4, 6) != "00" {
keys = append(keys, keyStr) keys = append(keys, keyStr)
@ -136,7 +134,7 @@ func GeneratorBirthdayCode(birthday string) string {
day, _ = strconv.Atoi(DatePad(strconv.Itoa(randDay), "day")) day, _ = strconv.Atoi(DatePad(strconv.Itoa(randDay), "day"))
} }
birthdayStr := strconv.Itoa(year) + strconv.Itoa(month) + strconv.Itoa(day) birthdayStr := DatePad(strconv.Itoa(year), "year") + DatePad(strconv.Itoa(month), "month") + DatePad(strconv.Itoa(day), "day")
_, error := time.Parse("20060102", birthdayStr) _, error := time.Parse("20060102", birthdayStr)
if error != nil { if error != nil {
randYear := rand.Intn(nowYear-1950) + 1950 randYear := rand.Intn(nowYear-1950) + 1950
@ -149,7 +147,7 @@ func GeneratorBirthdayCode(birthday string) string {
day, _ = strconv.Atoi(DatePad(strconv.Itoa(randDay), "day")) day, _ = strconv.Atoi(DatePad(strconv.Itoa(randDay), "day"))
} }
return strconv.Itoa(year) + strconv.Itoa(month) + strconv.Itoa(day) return DatePad(strconv.Itoa(year), "year") + DatePad(strconv.Itoa(month), "month") + DatePad(strconv.Itoa(day), "day")
} }
// 生成顺序码 // 生成顺序码
@ -170,5 +168,13 @@ func DatePad(date string, category string) string {
padLength = 4 padLength = 4
} }
return fmt.Sprintf("%s%032s", date, "")[:padLength] for i := 0; i < padLength; i++ {
length := len([]rune(date))
if length < padLength {
// date = fmt.Sprintf("%s%s", "0", date)
date = "0" + date
}
}
return date
} }

View File

@ -2,9 +2,8 @@ package id_validator
import ( import (
"errors" "errors"
"strconv"
"id-validator/data" "id-validator/data"
"strconv"
) )
// 验证身份证号合法性 // 验证身份证号合法性
@ -75,7 +74,7 @@ func FakeId(isEighteen bool, address string, birthday string, sex int) string {
// 出生日期码 // 出生日期码
birthdayCode := GeneratorBirthdayCode(birthday) birthdayCode := GeneratorBirthdayCode(birthday)
// fmt.Println(birthdayCode)
// 顺序码 // 顺序码
orderCode := GeneratorOrderCode(sex) orderCode := GeneratorOrderCode(sex)