id-validator/helper.go

99 lines
2.2 KiB
Go
Raw Normal View History

2021-01-11 17:55:24 +08:00
package idvalidator
2021-01-10 13:29:14 +08:00
import (
2021-01-12 11:36:44 +08:00
"github.com/guanguans/id-validator/data"
2021-01-10 13:29:14 +08:00
"strconv"
2021-01-10 14:56:35 +08:00
"strings"
2021-01-10 13:29:14 +08:00
)
// 检查地址码
func GetAddressInfo(addressCode string, birthdayCode string) map[string]string {
addressInfo := map[string]string{
"province": "",
"city": "",
"district": "",
}
// 省级信息
2021-01-11 17:55:24 +08:00
addressInfo["province"] = GetAddress(Substr(addressCode, 0, 2)+"0000", birthdayCode)
2021-01-10 13:29:14 +08:00
// 用于判断是否是港澳台居民居住证8字开头
firstCharacter := Substr(addressCode, 0, 1)
// 港澳台居民居住证无市级、县级信息
if firstCharacter == "8" {
return addressInfo
}
// 市级信息
2021-01-11 17:55:24 +08:00
addressInfo["city"] = GetAddress(Substr(addressCode, 0, 4)+"00", birthdayCode)
2021-01-10 13:29:14 +08:00
// 县级信息
addressInfo["district"] = GetAddress(addressCode, birthdayCode)
return addressInfo
}
2021-01-11 17:55:24 +08:00
// 获取省市区地址码
2021-01-10 13:29:14 +08:00
func GetAddress(addressCode string, birthdayCode string) string {
2021-01-11 17:55:24 +08:00
address := ""
addressCodeInt, _ := strconv.Atoi(addressCode)
year, _ := strconv.Atoi(Substr(birthdayCode, 0, 4))
for key, val := range data.AddressCodeTimeline[addressCodeInt] {
// if len(val) == 0 {
// continue
// }
2021-01-10 13:29:14 +08:00
startYear, _ := strconv.Atoi(val["start_year"])
2021-01-11 17:55:24 +08:00
if (key == 0 && year < startYear) || year >= startYear {
2021-01-10 13:29:14 +08:00
address = val["address"]
}
}
return address
}
2021-01-10 14:56:35 +08:00
// 获取星座信息
func GetConstellation(birthdayCode string) string {
monthStr := Substr(birthdayCode, 4, 6)
dayStr := Substr(birthdayCode, 6, 8)
month, _ := strconv.Atoi(monthStr)
day, _ := strconv.Atoi(dayStr)
startDate := data.Constellation[month]["start_date"]
startDay, _ := strconv.Atoi(strings.Split(startDate, "-")[1])
if day >= startDay {
return data.Constellation[month]["name"]
}
tmpMonth := month - 1
if month == 1 {
tmpMonth = 12
}
return data.Constellation[tmpMonth]["name"]
}
// 获取生肖信息
func GetChineseZodiac(birthdayCode string) string {
// 子鼠
start := 1900
end, _ := strconv.Atoi(Substr(birthdayCode, 0, 4))
key := (end - start) % 12
return data.ChineseZodiac[key]
}
2021-01-10 13:29:14 +08:00
// Substr 截取字符串
func Substr(source string, start int, end int) string {
2021-01-11 17:55:24 +08:00
r := []rune(source)
2021-01-10 13:29:14 +08:00
length := len(r)
if start < 0 || end > length || start > end {
return ""
}
if start == 0 && end == length {
return source
}
return string(r[start:end])
}