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.
 
 

70 lines
1.6 KiB

  1. package id_validator
  2. import (
  3. "fmt"
  4. "strconv"
  5. "id-validator/data"
  6. )
  7. // 验证身份证号合法性
  8. func IsValid(id string) bool {
  9. if !CheckIdArgument(id) {
  10. return false
  11. }
  12. code := GenerateType(id)
  13. if !CheckAddressCode(code["addressCode"], code["birthdayCode"]) || !CheckBirthdayCode(code["birthdayCode"]) || !CheckOrderCode(code["order"]) {
  14. return false
  15. }
  16. // 15位身份证不含校验码
  17. if code["type"] == "15" {
  18. return true
  19. }
  20. // 验证:校验码
  21. checkBit := GeneratorCheckBit(code["body"])
  22. return code["checkBit"] == checkBit
  23. }
  24. // 获取身份证信息
  25. func GetInfo(id string) map[string]string {
  26. // 验证有效性
  27. if !IsValid(id) {
  28. return map[string]string{}
  29. }
  30. code := GenerateType(id)
  31. addressInfo := GetAddressInfo(code["addressCode"], code["birthdayCode"])
  32. fmt.Println(addressInfo)
  33. address, _ := strconv.Atoi(code["addressCode"])
  34. abandoned := "0"
  35. if data.AddressCode[address] == "" {
  36. abandoned = "1"
  37. }
  38. // birthday, _ := time.Parse("20060102", code["birthdayCode"])
  39. sex := "1"
  40. sexCode, _ := strconv.Atoi(code["order"])
  41. if (sexCode % 2) == 0 {
  42. sex = "0"
  43. }
  44. info := map[string]string{
  45. "addressCode": code["addressCode"],
  46. "abandoned": abandoned,
  47. "address": addressInfo["province"] + addressInfo["city"] + addressInfo["district"],
  48. // "addressTree": addressInfo,
  49. // "birthdayCode": birthday,
  50. "constellation": GetConstellation(code["birthdayCode"]),
  51. "chineseZodiac": GetChineseZodiac(code["birthdayCode"]),
  52. "sex": sex,
  53. "length": code["type"],
  54. "checkBit": code["checkBit"],
  55. }
  56. return info
  57. }