utils.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "math/rand"
  10. )
  11. var (
  12. idCertMatrix = []int{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}
  13. idCertVerifyCode = []byte("10X98765432")
  14. socialCreditMatrix = []int{1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28}
  15. socialCreditMap = map[int]int{'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
  16. 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'J': 18,
  17. 'K': 19, 'L': 20, 'M': 21, 'N': 22, 'P': 23, 'Q': 24, 'R': 25, 'T': 26, 'U': 27,
  18. 'W': 28, 'X': 29, 'Y': 30}
  19. 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'}
  20. 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}
  21. )
  22. func RandomString(length int) string {
  23. str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  24. bytes := []byte(str)
  25. result := []byte{}
  26. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  27. for i := 0; i < length; i++ {
  28. result = append(result, bytes[r.Intn(len(bytes))])
  29. }
  30. return string(result)
  31. }
  32. func CheckIDCert(str string) (realIdCert string, res bool) {
  33. defer func() {
  34. if r := recover(); r != nil {
  35. res = false
  36. }
  37. }()
  38. idCert := strings.ToUpper(str)
  39. idCert = strings.TrimSpace(idCert)
  40. ret := idCert
  41. if len(idCert) != 18 {
  42. return "", false
  43. }
  44. year, err := strconv.Atoi(idCert[6:10])
  45. if err != nil {
  46. return "", false
  47. }
  48. if !(1900 < year && year < 2100) {
  49. return "", false
  50. }
  51. check := 0
  52. lastLetter := 0
  53. for index, value := range []byte(idCert) {
  54. if index == 17 {
  55. lastLetter = int(value)
  56. break
  57. }
  58. if !(value >= 48 && value <= 57) {
  59. return "", false
  60. }
  61. v := value - 48
  62. check = check + idCertMatrix[index]*int(v)
  63. }
  64. if !((lastLetter >= 48 && lastLetter <= 57) || lastLetter == 'X') {
  65. return "", false
  66. }
  67. timeLayout := "20060102" //转化所需模板
  68. loc, _ := time.LoadLocation("Local") //重要:获取时区
  69. _, err = time.ParseInLocation(timeLayout, idCert[6:14], loc) //使用模板在对应时区转化为time.time类型
  70. if err != nil {
  71. return "", false
  72. }
  73. verifyCode := int(idCertVerifyCode[check%11])
  74. if lastLetter == verifyCode {
  75. return ret, true
  76. }
  77. return "", false
  78. }
  79. func VerifyMobileFormat(phone string) bool {
  80. 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}$"
  81. reg := regexp.MustCompile(regular)
  82. return reg.MatchString(phone)
  83. }