utils.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package utils
  4. import (
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "math/rand"
  10. "fmt"
  11. )
  12. var (
  13. idCertMatrix = []int{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}
  14. idCertVerifyCode = []byte("10X98765432")
  15. socialCreditMatrix = []int{1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28}
  16. socialCreditMap = map[int]int{'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
  17. 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'J': 18,
  18. 'K': 19, 'L': 20, 'M': 21, 'N': 22, 'P': 23, 'Q': 24, 'R': 25, 'T': 26, 'U': 27,
  19. 'W': 28, 'X': 29, 'Y': 30}
  20. 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'}
  21. 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}
  22. )
  23. const (
  24. NUmStr = "0123456789"
  25. CharStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  26. SpecStr = "+=-@#~,.[]()!%^*$"
  27. )
  28. func CheckIDCert(str string) (realIdCert string, res bool) {
  29. defer func() {
  30. if r := recover(); r != nil {
  31. res = false
  32. }
  33. }()
  34. idCert := strings.ToUpper(str)
  35. idCert = strings.TrimSpace(idCert)
  36. ret := idCert
  37. if len(idCert) != 18 {
  38. return "", false
  39. }
  40. year, err := strconv.Atoi(idCert[6:10])
  41. if err != nil {
  42. return "", false
  43. }
  44. if !(1900 < year && year < 2100) {
  45. return "", false
  46. }
  47. check := 0
  48. lastLetter := 0
  49. for index, value := range []byte(idCert) {
  50. if index == 17 {
  51. lastLetter = int(value)
  52. break
  53. }
  54. if !(value >= 48 && value <= 57) {
  55. return "", false
  56. }
  57. v := value - 48
  58. check = check + idCertMatrix[index]*int(v)
  59. }
  60. if !((lastLetter >= 48 && lastLetter <= 57) || lastLetter == 'X') {
  61. return "", false
  62. }
  63. timeLayout := "20060102" //转化所需模板
  64. loc, _ := time.LoadLocation("Local") //重要:获取时区
  65. _, err = time.ParseInLocation(timeLayout, idCert[6:14], loc) //使用模板在对应时区转化为time.time类型
  66. if err != nil {
  67. return "", false
  68. }
  69. verifyCode := int(idCertVerifyCode[check%11])
  70. if lastLetter == verifyCode {
  71. return ret, true
  72. }
  73. return "", false
  74. }
  75. func VerifyMobileFormat(phone string) bool {
  76. 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}$"
  77. reg := regexp.MustCompile(regular)
  78. return reg.MatchString(phone)
  79. }
  80. func GenerateRandomStr(length int, charset string) string {
  81. time.Sleep(1*time.Microsecond)
  82. rand.Seed(time.Now().UnixNano())
  83. //初始化密码切片
  84. var passwd []byte = make([]byte, length, length)
  85. //源字符串
  86. var sourceStr string
  87. //判断字符类型,如果是数字
  88. if charset == "num" {
  89. sourceStr = NUmStr
  90. //如果选的是字符
  91. } else if charset == "char" {
  92. sourceStr = charset
  93. //如果选的是混合模式
  94. } else if charset == "mix" {
  95. sourceStr = fmt.Sprintf("%s%s", NUmStr, CharStr)
  96. //如果选的是高级模式
  97. } else if charset == "advance" {
  98. sourceStr = fmt.Sprintf("%s%s%s", NUmStr, CharStr, SpecStr)
  99. } else {
  100. sourceStr = fmt.Sprintf("%s%s%s", NUmStr, CharStr, SpecStr)
  101. }
  102. //遍历,生成一个随机index索引,
  103. for i := 0; i < length; i++ {
  104. index := rand.Intn(len(sourceStr))
  105. passwd[i] = sourceStr[index]
  106. }
  107. return string(passwd)
  108. }