Browse Source

Wip

tags/v1.0.0
guanguans 3 years ago
parent
commit
cf2ada4860
4 changed files with 30 additions and 30 deletions
  1. +3
    -8
      checker.go
  2. +12
    -12
      data/chinese_zodiac.go
  3. +13
    -7
      generator.go
  4. +2
    -3
      id_validator.go

+ 3
- 8
checker.go 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 true

return addressInfo["province"] != ""
} }


// 检查出生日期码 // 检查出生日期码
@@ -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
} }


// 检查顺序码 // 检查顺序码


+ 12
- 12
data/chinese_zodiac.go View File

@@ -3,16 +3,16 @@ package data
// 生肖信息 // 生肖信息


var ChineseZodiac = [12]string{ var ChineseZodiac = [12]string{
"子鼠",
"丑牛",
"寅虎",
"卯兔",
"辰龙",
"巳蛇",
"午马",
"未羊",
"申猴",
"酉鸡",
"戌狗",
"亥猪",
"子鼠",
"丑牛",
"寅虎",
"卯兔",
"辰龙",
"巳蛇",
"午马",
"未羊",
"申猴",
"酉鸡",
"戌狗",
"亥猪",
} }

+ 13
- 7
generator.go 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
} }

+ 2
- 3
id_validator.go 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)




Loading…
Cancel
Save