You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.2 KiB

  1. package idvalidator
  2. import (
  3. "github.com/guanguans/id-validator/data"
  4. "strconv"
  5. "strings"
  6. )
  7. // 获取地址信息
  8. func getAddressInfo(addressCode string, birthdayCode string) map[string]string {
  9. addressInfo := map[string]string{
  10. "province": "",
  11. "city": "",
  12. "district": "",
  13. }
  14. // 省级信息
  15. addressInfo["province"] = getAddress(substr(addressCode, 0, 2)+"0000", birthdayCode)
  16. // 用于判断是否是港澳台居民居住证(8字开头)
  17. firstCharacter := substr(addressCode, 0, 1)
  18. // 港澳台居民居住证无市级、县级信息
  19. if firstCharacter == "8" {
  20. return addressInfo
  21. }
  22. // 市级信息
  23. addressInfo["city"] = getAddress(substr(addressCode, 0, 4)+"00", birthdayCode)
  24. // 县级信息
  25. addressInfo["district"] = getAddress(addressCode, birthdayCode)
  26. return addressInfo
  27. }
  28. // 获取省市区地址码
  29. func getAddress(addressCode string, birthdayCode string) string {
  30. address := ""
  31. addressCodeInt, _ := strconv.Atoi(addressCode)
  32. year, _ := strconv.Atoi(substr(birthdayCode, 0, 4))
  33. for key, val := range data.AddressCodeTimeline[addressCodeInt] {
  34. // if len(val) == 0 {
  35. // continue
  36. // }
  37. startYear, _ := strconv.Atoi(val["start_year"])
  38. if (key == 0 && year < startYear) || year >= startYear {
  39. address = val["address"]
  40. }
  41. }
  42. return address
  43. }
  44. // 获取星座信息
  45. func getConstellation(birthdayCode string) string {
  46. monthStr := substr(birthdayCode, 4, 6)
  47. dayStr := substr(birthdayCode, 6, 8)
  48. month, _ := strconv.Atoi(monthStr)
  49. day, _ := strconv.Atoi(dayStr)
  50. startDate := data.Constellation[month]["start_date"]
  51. startDay, _ := strconv.Atoi(strings.Split(startDate, "-")[1])
  52. if day >= startDay {
  53. return data.Constellation[month]["name"]
  54. }
  55. tmpMonth := month - 1
  56. if month == 1 {
  57. tmpMonth = 12
  58. }
  59. return data.Constellation[tmpMonth]["name"]
  60. }
  61. // 获取生肖信息
  62. func getChineseZodiac(birthdayCode string) string {
  63. // 子鼠
  64. start := 1900
  65. end, _ := strconv.Atoi(substr(birthdayCode, 0, 4))
  66. key := (end - start) % 12
  67. return data.ChineseZodiac[key]
  68. }
  69. // substr 截取字符串
  70. func substr(source string, start int, end int) string {
  71. r := []rune(source)
  72. length := len(r)
  73. if start < 0 || end > length || start > end {
  74. return ""
  75. }
  76. if start == 0 && end == length {
  77. return source
  78. }
  79. return string(r[start:end])
  80. }