From c30a1fc5c6cf0f458c8b1e700b2c5d7969cf0174 Mon Sep 17 00:00:00 2001 From: guanguans Date: Sun, 10 Jan 2021 14:56:35 +0800 Subject: [PATCH] Finish `GetInfo` method --- data/constellation.go | 122 +++++++++++++++++++++--------------------- helper.go | 31 +++++++++++ id_validator.go | 46 ++++++++++++++++ 3 files changed, 138 insertions(+), 61 deletions(-) diff --git a/data/constellation.go b/data/constellation.go index 53f9703..d77721b 100644 --- a/data/constellation.go +++ b/data/constellation.go @@ -2,65 +2,65 @@ package data // 星座信息 -var Constellation = [12]map[string]string{ - { - "name": "水瓶座", - "start_date": "01-20", - "end_date": "02-18", - }, - { - "name": "双鱼座", - "start_date": "02-19", - "end_date": "03-20", - }, - { - "name": "白羊座", - "start_date": "03-21", - "end_date": "04-19", - }, - { - "name": "金牛座", - "start_date": "04-20", - "end_date": "05-20", - }, - { - "name": "双子座", - "start_date": "05-21", - "end_date": "06-21", - }, - { - "name": "巨蟹座", - "start_date": "06-22", - "end_date": "07-22", - }, - { - "name": "狮子座", - "start_date": "07-23", - "end_date": "08-22", - }, - { - "name": "处女座", - "start_date": "08-23", - "end_date": "09-22", - }, - { - "name": "天秤座", - "start_date": "09-23", - "end_date": "10-23", - }, - { - "name": "天蝎座", - "start_date": "10-24", - "end_date": "11-22", - }, - { - "name": "射手座", - "start_date": "11-23", - "end_date": "12-21", - }, - { - "name": "摩羯座", - "start_date": "12-22", - "end_date": "01-19", - }, +var Constellation = [13]map[string]string{ + 1: { + "name": "水瓶座", + "start_date": "01-20", + "end_date": "02-18", + }, + 2: { + "name": "双鱼座", + "start_date": "02-19", + "end_date": "03-20", + }, + 3: { + "name": "白羊座", + "start_date": "03-21", + "end_date": "04-19", + }, + 4: { + "name": "金牛座", + "start_date": "04-20", + "end_date": "05-20", + }, + 5: { + "name": "双子座", + "start_date": "05-21", + "end_date": "06-21", + }, + 6: { + "name": "巨蟹座", + "start_date": "06-22", + "end_date": "07-22", + }, + 7: { + "name": "狮子座", + "start_date": "07-23", + "end_date": "08-22", + }, + 8: { + "name": "处女座", + "start_date": "08-23", + "end_date": "09-22", + }, + 9: { + "name": "天秤座", + "start_date": "09-23", + "end_date": "10-23", + }, + 10: { + "name": "天蝎座", + "start_date": "10-24", + "end_date": "11-22", + }, + 11: { + "name": "射手座", + "start_date": "11-23", + "end_date": "12-21", + }, + 12: { + "name": "摩羯座", + "start_date": "12-22", + "end_date": "01-19", + }, } diff --git a/helper.go b/helper.go index ad84e11..06dd2ed 100644 --- a/helper.go +++ b/helper.go @@ -2,6 +2,7 @@ package id_validator import ( "strconv" + "strings" "id-validator/data" ) @@ -58,6 +59,36 @@ func GetAddress(addressCode string, birthdayCode string) string { return address } +// 获取星座信息 +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] +} + // Substr 截取字符串 func Substr(source string, start int, end int) string { var r = []rune(source) diff --git a/id_validator.go b/id_validator.go index 59fcaa5..bc5576d 100644 --- a/id_validator.go +++ b/id_validator.go @@ -1,5 +1,12 @@ package id_validator +import ( + "fmt" + "strconv" + + "id-validator/data" +) + // 验证身份证号合法性 func IsValid(id string) bool { if !CheckIdArgument(id) { @@ -21,3 +28,42 @@ func IsValid(id string) bool { return code["checkBit"] == checkBit } + +// 获取身份证信息 +func GetInfo(id string) map[string]string { + // 验证有效性 + if !IsValid(id) { + return map[string]string{} + } + + code := GenerateType(id) + + addressInfo := GetAddressInfo(code["addressCode"], code["birthdayCode"]) + fmt.Println(addressInfo) + address, _ := strconv.Atoi(code["addressCode"]) + abandoned := "0" + if data.AddressCode[address] == "" { + abandoned = "1" + } + // birthday, _ := time.Parse("20060102", code["birthdayCode"]) + + sex := "1" + sexCode, _ := strconv.Atoi(code["order"]) + if (sexCode % 2) == 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 +}