From cf2ada4860b01bcc6bec5b39978a79ff47cd6d19 Mon Sep 17 00:00:00 2001 From: guanguans Date: Mon, 11 Jan 2021 11:53:22 +0800 Subject: [PATCH] Wip --- checker.go | 11 +++-------- data/chinese_zodiac.go | 24 ++++++++++++------------ generator.go | 20 +++++++++++++------- id_validator.go | 5 ++--- 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/checker.go b/checker.go index 4a86bc3..b42fd91 100644 --- a/checker.go +++ b/checker.go @@ -60,10 +60,8 @@ func GenerateLongType(id string) map[string]string { // 检查地址码 func CheckAddressCode(addressCode string, birthdayCode string) bool { addressInfo := GetAddressInfo(addressCode, birthdayCode) - if addressInfo["province"] == "" { - return false - } - return true + + return addressInfo["province"] != "" } // 检查出生日期码 @@ -74,11 +72,8 @@ func CheckBirthdayCode(birthdayCode string) bool { } _, error := time.Parse("20060102", birthdayCode) - if error != nil { - return false - } - return true + return error == nil } // 检查顺序码 diff --git a/data/chinese_zodiac.go b/data/chinese_zodiac.go index 89133e7..6d8a705 100644 --- a/data/chinese_zodiac.go +++ b/data/chinese_zodiac.go @@ -3,16 +3,16 @@ package data // 生肖信息 var ChineseZodiac = [12]string{ - "子鼠", - "丑牛", - "寅虎", - "卯兔", - "辰龙", - "巳蛇", - "午马", - "未羊", - "申猴", - "酉鸡", - "戌狗", - "亥猪", + "子鼠", + "丑牛", + "寅虎", + "卯兔", + "辰龙", + "巳蛇", + "午马", + "未羊", + "申猴", + "酉鸡", + "戌狗", + "亥猪", } diff --git a/generator.go b/generator.go index 6e89a35..8e1e454 100644 --- a/generator.go +++ b/generator.go @@ -1,15 +1,13 @@ package id_validator import ( - "fmt" + "id-validator/data" "math" "math/rand" "regexp" "strconv" "strings" "time" - - "id-validator/data" ) // 生成 Bit 码 @@ -101,7 +99,7 @@ func GetRandAddressCode(pattern string) string { mustCompile := regexp.MustCompile(pattern) var keys []string - for key, _ := range data.AddressCode { + for key := range data.AddressCode { keyStr := strconv.Itoa(key) if mustCompile.MatchString(keyStr) && Substr(keyStr, 4, 6) != "00" { keys = append(keys, keyStr) @@ -136,7 +134,7 @@ func GeneratorBirthdayCode(birthday string) string { 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) if error != nil { randYear := rand.Intn(nowYear-1950) + 1950 @@ -149,7 +147,7 @@ func GeneratorBirthdayCode(birthday string) string { 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 } - 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 } diff --git a/id_validator.go b/id_validator.go index 423b652..f96d7d8 100644 --- a/id_validator.go +++ b/id_validator.go @@ -2,9 +2,8 @@ package id_validator import ( "errors" - "strconv" - "id-validator/data" + "strconv" ) // 验证身份证号合法性 @@ -75,7 +74,7 @@ func FakeId(isEighteen bool, address string, birthday string, sex int) string { // 出生日期码 birthdayCode := GeneratorBirthdayCode(birthday) - + // fmt.Println(birthdayCode) // 顺序码 orderCode := GeneratorOrderCode(sex)