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.
 
 

101 lines
2.3 KiB

  1. package id_validator
  2. import (
  3. "errors"
  4. "id-validator/data"
  5. "strconv"
  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. }
  58. // 生成假数据
  59. func FakeId(isEighteen bool, address string, birthday string, sex int) string {
  60. // 生成地址码
  61. addressCode := GeneratorAddressCode(address)
  62. // 出生日期码
  63. birthdayCode := GeneratorBirthdayCode(birthday)
  64. // fmt.Println(birthdayCode)
  65. // 顺序码
  66. orderCode := GeneratorOrderCode(sex)
  67. if !isEighteen {
  68. return addressCode + Substr(birthdayCode, 2, 6) + orderCode
  69. }
  70. body := addressCode + birthdayCode + orderCode
  71. return body + GeneratorCheckBit(body)
  72. }
  73. // 15位升级18位号码
  74. func UpgradeId(id string) (string, error) {
  75. if !IsValid(id) {
  76. return "", errors.New("Not Valid ID card number.")
  77. }
  78. code := GenerateShortType(id)
  79. body := code["addressCode"] + code["birthdayCode"] + code["order"]
  80. return body + GeneratorCheckBit(body), nil
  81. }