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
|
|
|
|
)
|
|
|
|
|
|
2021-01-27 11:29:38 +08:00
|
|
|
|
// 获取地址信息
|
|
|
|
|
func getAddressInfo(addressCode string, birthdayCode string) map[string]string {
|
2021-01-10 13:29:14 +08:00
|
|
|
|
addressInfo := map[string]string{
|
|
|
|
|
"province": "",
|
|
|
|
|
"city": "",
|
|
|
|
|
"district": "",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 省级信息
|
2021-01-27 11:29:38 +08:00
|
|
|
|
addressInfo["province"] = getAddress(substr(addressCode, 0, 2)+"0000", birthdayCode)
|
2021-01-10 13:29:14 +08:00
|
|
|
|
|
|
|
|
|
// 用于判断是否是港澳台居民居住证(8字开头)
|
2021-01-27 11:29:38 +08:00
|
|
|
|
firstCharacter := substr(addressCode, 0, 1)
|
2021-01-10 13:29:14 +08:00
|
|
|
|
// 港澳台居民居住证无市级、县级信息
|
|
|
|
|
if firstCharacter == "8" {
|
|
|
|
|
return addressInfo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 市级信息
|
2021-01-27 11:29:38 +08:00
|
|
|
|
addressInfo["city"] = getAddress(substr(addressCode, 0, 4)+"00", birthdayCode)
|
2021-01-10 13:29:14 +08:00
|
|
|
|
|
|
|
|
|
// 县级信息
|
2021-01-27 11:29:38 +08:00
|
|
|
|
addressInfo["district"] = getAddress(addressCode, birthdayCode)
|
2021-01-10 13:29:14 +08:00
|
|
|
|
|
|
|
|
|
return addressInfo
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 17:55:24 +08:00
|
|
|
|
// 获取省市区地址码
|
2021-01-27 11:29:38 +08:00
|
|
|
|
func getAddress(addressCode string, birthdayCode string) string {
|
2021-01-11 17:55:24 +08:00
|
|
|
|
address := ""
|
|
|
|
|
addressCodeInt, _ := strconv.Atoi(addressCode)
|
2021-01-27 11:29:38 +08:00
|
|
|
|
year, _ := strconv.Atoi(substr(birthdayCode, 0, 4))
|
2021-01-11 17:55:24 +08:00
|
|
|
|
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
|
|
|
|
// 获取星座信息
|
2021-01-27 11:29:38 +08:00
|
|
|
|
func getConstellation(birthdayCode string) string {
|
|
|
|
|
monthStr := substr(birthdayCode, 4, 6)
|
|
|
|
|
dayStr := substr(birthdayCode, 6, 8)
|
2021-01-10 14:56:35 +08:00
|
|
|
|
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"]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取生肖信息
|
2021-01-27 11:29:38 +08:00
|
|
|
|
func getChineseZodiac(birthdayCode string) string {
|
2021-01-10 14:56:35 +08:00
|
|
|
|
// 子鼠
|
|
|
|
|
start := 1900
|
2021-01-27 11:29:38 +08:00
|
|
|
|
end, _ := strconv.Atoi(substr(birthdayCode, 0, 4))
|
2021-01-10 14:56:35 +08:00
|
|
|
|
key := (end - start) % 12
|
|
|
|
|
|
|
|
|
|
return data.ChineseZodiac[key]
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 11:29:38 +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])
|
|
|
|
|
}
|