utils.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2019 githup.com. All rights reserved.
  2. // Use of this source code is governed by githup.com.
  3. package utils
  4. import (
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. var (
  11. idCertMatrix = []int{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}
  12. idCertVerifyCode = []byte("10X98765432")
  13. socialCreditMatrix = []int{1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28}
  14. socialCreditMap = map[int]int{'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
  15. 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'J': 18,
  16. 'K': 19, 'L': 20, 'M': 21, 'N': 22, 'P': 23, 'Q': 24, 'R': 25, 'T': 26, 'U': 27,
  17. 'W': 28, 'X': 29, 'Y': 30}
  18. socialCreditMapKeys = []int{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'T', 'U', 'W', 'X', 'Y'}
  19. socialCreditMapValues = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30}
  20. )
  21. func CheckIDCert(str string) (realIdCert string, res bool) {
  22. defer func() {
  23. if r := recover(); r != nil {
  24. res = false
  25. }
  26. }()
  27. idCert := strings.ToUpper(str)
  28. idCert = strings.TrimSpace(idCert)
  29. ret := idCert
  30. if len(idCert) != 18 {
  31. return "", false
  32. }
  33. year, err := strconv.Atoi(idCert[6:10])
  34. if err != nil {
  35. return "", false
  36. }
  37. if !(1900 < year && year < 2100) {
  38. return "", false
  39. }
  40. check := 0
  41. lastLetter := 0
  42. for index, value := range []byte(idCert) {
  43. if index == 17 {
  44. lastLetter = int(value)
  45. break
  46. }
  47. if !(value >= 48 && value <= 57) {
  48. return "", false
  49. }
  50. v := value - 48
  51. check = check + idCertMatrix[index]*int(v)
  52. }
  53. if !((lastLetter >= 48 && lastLetter <= 57) || lastLetter == 'X') {
  54. return "", false
  55. }
  56. timeLayout := "20060102" //转化所需模板
  57. loc, _ := time.LoadLocation("Local") //重要:获取时区
  58. _, err = time.ParseInLocation(timeLayout, idCert[6:14], loc) //使用模板在对应时区转化为time.time类型
  59. if err != nil {
  60. return "", false
  61. }
  62. verifyCode := int(idCertVerifyCode[check%11])
  63. if lastLetter == verifyCode {
  64. return ret, true
  65. }
  66. return "", false
  67. }
  68. func VerifyMobileFormat(phone string) bool {
  69. regular := "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$"
  70. reg := regexp.MustCompile(regular)
  71. return reg.MatchString(phone)
  72. }