2021-01-11 17:55:24 +08:00
|
|
|
package idvalidator
|
2021-01-10 13:29:14 +08:00
|
|
|
|
|
|
|
import (
|
2021-01-11 17:55:24 +08:00
|
|
|
"errors"
|
2021-01-10 13:29:14 +08:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 检查ID参数
|
2021-01-12 11:18:07 +08:00
|
|
|
func CheckIdArgument(id string) bool {
|
2021-01-11 17:55:24 +08:00
|
|
|
_, err := GenerateCode(id)
|
|
|
|
|
|
|
|
return err == nil
|
2021-01-10 13:29:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 生成数据
|
2021-01-11 17:55:24 +08:00
|
|
|
func GenerateCode(id string) (map[string]string, error) {
|
|
|
|
length := len(id)
|
|
|
|
if length == 15 {
|
|
|
|
return GenerateShortCode(id)
|
2021-01-10 13:29:14 +08:00
|
|
|
}
|
|
|
|
|
2021-01-11 17:55:24 +08:00
|
|
|
if length == 18 {
|
|
|
|
return GenerateLongCode(id)
|
2021-01-10 13:29:14 +08:00
|
|
|
}
|
|
|
|
|
2021-01-11 17:55:24 +08:00
|
|
|
return map[string]string{}, errors.New("Invalid ID card number length.")
|
2021-01-10 13:29:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 生成短数据
|
2021-01-11 17:55:24 +08:00
|
|
|
func GenerateShortCode(id string) (map[string]string, error) {
|
|
|
|
if len(id) != 15 {
|
|
|
|
return map[string]string{}, errors.New("Invalid ID card number length.")
|
|
|
|
}
|
|
|
|
|
2021-01-10 13:29:14 +08:00
|
|
|
mustCompile := regexp.MustCompile("(.{6})(.{6})(.{3})")
|
2021-01-11 17:55:24 +08:00
|
|
|
subMatch := mustCompile.FindStringSubmatch(strings.ToLower(id))
|
2021-01-10 13:29:14 +08:00
|
|
|
|
|
|
|
return map[string]string{
|
|
|
|
"body": subMatch[0],
|
|
|
|
"addressCode": subMatch[1],
|
|
|
|
"birthdayCode": "19" + subMatch[2],
|
|
|
|
"order": subMatch[3],
|
|
|
|
"checkBit": "",
|
|
|
|
"type": "15",
|
2021-01-11 17:55:24 +08:00
|
|
|
}, nil
|
2021-01-10 13:29:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 生成长数据
|
2021-01-11 17:55:24 +08:00
|
|
|
func GenerateLongCode(id string) (map[string]string, error) {
|
|
|
|
if len(id) != 18 {
|
|
|
|
return map[string]string{}, errors.New("Invalid ID card number length.")
|
|
|
|
}
|
2021-01-10 13:29:14 +08:00
|
|
|
mustCompile := regexp.MustCompile("((.{6})(.{8})(.{3}))(.)")
|
2021-01-11 17:55:24 +08:00
|
|
|
subMatch := mustCompile.FindStringSubmatch(strings.ToLower(id))
|
2021-01-10 13:29:14 +08:00
|
|
|
|
|
|
|
return map[string]string{
|
|
|
|
"body": subMatch[1],
|
|
|
|
"addressCode": subMatch[2],
|
|
|
|
"birthdayCode": subMatch[3],
|
|
|
|
"order": subMatch[4],
|
|
|
|
"checkBit": subMatch[5],
|
|
|
|
"type": "18",
|
2021-01-11 17:55:24 +08:00
|
|
|
}, nil
|
2021-01-10 13:29:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 检查地址码
|
|
|
|
func CheckAddressCode(addressCode string, birthdayCode string) bool {
|
2021-01-11 17:55:24 +08:00
|
|
|
return GetAddressInfo(addressCode, birthdayCode)["province"] != ""
|
2021-01-10 13:29:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 检查出生日期码
|
|
|
|
func CheckBirthdayCode(birthdayCode string) bool {
|
|
|
|
year, _ := strconv.Atoi(Substr(birthdayCode, 0, 4))
|
|
|
|
if year < 1800 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-11 17:55:24 +08:00
|
|
|
nowYear := time.Now().Year()
|
|
|
|
if year > nowYear {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := time.Parse("20060102", birthdayCode)
|
2021-01-10 13:29:14 +08:00
|
|
|
|
2021-01-11 17:55:24 +08:00
|
|
|
return err == nil
|
2021-01-10 13:29:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 检查顺序码
|
|
|
|
func CheckOrderCode(orderCode string) bool {
|
|
|
|
return len(orderCode) == 3
|
|
|
|
}
|