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.
 
 

83 lines
1.7 KiB

  1. package id_validator
  2. import (
  3. "regexp"
  4. "strconv"
  5. "strings"
  6. "time"
  7. )
  8. // 检查ID参数
  9. func CheckIdArgument(id string) bool {
  10. return len(GenerateType(id)) != 0
  11. }
  12. // 生成数据
  13. func GenerateType(id string) map[string]string {
  14. lowerId := strings.ToLower(id)
  15. if len(lowerId) == 15 {
  16. return GenerateShortType(id)
  17. }
  18. if len(lowerId) == 18 {
  19. return GenerateLongType(id)
  20. }
  21. return map[string]string{}
  22. }
  23. // 生成短数据
  24. func GenerateShortType(id string) map[string]string {
  25. mustCompile := regexp.MustCompile("(.{6})(.{6})(.{3})")
  26. subMatch := mustCompile.FindStringSubmatch(id)
  27. return map[string]string{
  28. "body": subMatch[0],
  29. "addressCode": subMatch[1],
  30. "birthdayCode": "19" + subMatch[2],
  31. "order": subMatch[3],
  32. "checkBit": "",
  33. "type": "15",
  34. }
  35. }
  36. // 生成长数据
  37. func GenerateLongType(id string) map[string]string {
  38. mustCompile := regexp.MustCompile("((.{6})(.{8})(.{3}))(.)")
  39. subMatch := mustCompile.FindStringSubmatch(id)
  40. return map[string]string{
  41. "body": subMatch[1],
  42. "addressCode": subMatch[2],
  43. "birthdayCode": subMatch[3],
  44. "order": subMatch[4],
  45. "checkBit": subMatch[5],
  46. "type": "18",
  47. }
  48. }
  49. // 检查地址码
  50. func CheckAddressCode(addressCode string, birthdayCode string) bool {
  51. addressInfo := GetAddressInfo(addressCode, birthdayCode)
  52. return addressInfo["province"] != ""
  53. }
  54. // 检查出生日期码
  55. func CheckBirthdayCode(birthdayCode string) bool {
  56. year, _ := strconv.Atoi(Substr(birthdayCode, 0, 4))
  57. if year < 1800 {
  58. return false
  59. }
  60. _, error := time.Parse("20060102", birthdayCode)
  61. return error == nil
  62. }
  63. // 检查顺序码
  64. func CheckOrderCode(orderCode string) bool {
  65. return len(orderCode) == 3
  66. }