This commit is contained in:
guanguans 2021-01-11 18:48:50 +08:00
parent 26d7f5f009
commit 827b5e6010

View File

@ -2,10 +2,26 @@ package idvalidator
import ( import (
"errors" "errors"
"id-validator/data"
"strconv" "strconv"
"time"
"id-validator/data"
) )
// 身份证信息
type IdInfo struct {
AddressCode int
Abandoned int
Address string
AddressTree []string
Birthday time.Time
Constellation string
ChineseZodiac string
Sex int
Length int
CheckBit int
}
// 验证身份证号合法性 // 验证身份证号合法性
func IsValid(id string) bool { func IsValid(id string) bool {
code, err := GenerateCode(id) code, err := GenerateCode(id)
@ -28,42 +44,56 @@ func IsValid(id string) bool {
} }
// 获取身份证信息 // 获取身份证信息
func GetInfo(id string) map[string]string { func GetInfo(id string) (IdInfo, error) {
// 验证有效性 // 验证有效性
if !IsValid(id) { if !IsValid(id) {
return map[string]string{} return IdInfo{}, errors.New("Not Valid ID card number.")
} }
code, _ := GenerateCode(id) code, _ := GenerateCode(id)
addressCode, _ := strconv.Atoi(code["addressCode"])
// 地址信息
addressInfo := GetAddressInfo(code["addressCode"], code["birthdayCode"]) addressInfo := GetAddressInfo(code["addressCode"], code["birthdayCode"])
// fmt.Println(addressInfo) var addressTree []string
address, _ := strconv.Atoi(code["addressCode"]) for _, val := range addressInfo {
abandoned := "0" addressTree = append(addressTree, val)
if data.AddressCode[address] == "" {
abandoned = "1"
} }
// birthday, _ := time.Parse("20060102", code["birthdayCode"])
sex := "1" // 是否废弃
var abandoned int
if data.AddressCode[addressCode] == "" {
abandoned = 1
}
// 生日
birthday, _ := time.Parse("20060102", code["birthdayCode"])
// 性别
sex := 1
sexCode, _ := strconv.Atoi(code["order"]) sexCode, _ := strconv.Atoi(code["order"])
if (sexCode % 2) == 0 { if (sexCode % 2) == 0 {
sex = "0" sex = 0
}
info := map[string]string{
"addressCode": code["addressCode"],
"abandoned": abandoned,
"address": addressInfo["province"] + addressInfo["city"] + addressInfo["district"],
// "addressTree": addressInfo,
// "birthdayCode": birthday,
"constellation": GetConstellation(code["birthdayCode"]),
"chineseZodiac": GetChineseZodiac(code["birthdayCode"]),
"sex": sex,
"length": code["type"],
"checkBit": code["checkBit"],
} }
return info // 长度
length, _ := strconv.Atoi(code["type"])
// Bit码
checkBit, _ := strconv.Atoi(code["checkBit"])
return IdInfo{
AddressCode: addressCode,
Abandoned: abandoned,
Address: addressInfo["province"] + addressInfo["city"] + addressInfo["district"],
AddressTree: addressTree,
Birthday: birthday,
Constellation: GetConstellation(code["birthdayCode"]),
ChineseZodiac: GetChineseZodiac(code["birthdayCode"]),
Sex: sex,
Length: length,
CheckBit: checkBit,
}, nil
} }
// 生成假身份证号码 // 生成假身份证号码