utils.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package utils
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "unsafe"
  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. //单位证件
  23. func CheckSocialCode(str string) (res bool) {
  24. defer func() {
  25. if r := recover(); r != nil {
  26. res = false
  27. }
  28. }()
  29. socialCode := strings.ToUpper(str)
  30. if len(socialCode) != 18 {
  31. return false
  32. }
  33. check := 0
  34. lastLetter := 0
  35. for index, value := range []byte(socialCode) {
  36. if index == 17 {
  37. lastLetter = int(value)
  38. break
  39. }
  40. if value >= 48 && value <= 57 {
  41. //fmt.Println((int(value)-48),socialCreditMatrix[index])
  42. check = check + (int(value)-48)*socialCreditMatrix[index]
  43. } else {
  44. check = check + socialCreditMap[int(value)]*socialCreditMatrix[index]
  45. }
  46. }
  47. diff := 31 - check%31
  48. keys := make([]int, 0)
  49. values := make([]int, 0)
  50. for k, v := range socialCreditMap {
  51. keys = append(keys, k)
  52. values = append(values, v)
  53. }
  54. verifyCode := socialCreditMapKeys[socialCreditMapValues[diff]]
  55. if verifyCode == lastLetter {
  56. return true
  57. } else {
  58. return false
  59. }
  60. }
  61. //身份证
  62. func CheckIDCert(str string) (res bool) {
  63. defer func() {
  64. if r := recover(); r != nil {
  65. res = false
  66. }
  67. }()
  68. idCert := strings.ToUpper(str)
  69. if len(idCert) != 18 {
  70. return false
  71. }
  72. year, err := strconv.Atoi(idCert[6:10])
  73. if err != nil {
  74. return false
  75. }
  76. if !(1900 < year && year < 2100) {
  77. return false
  78. }
  79. check := 0
  80. lastLetter := 0
  81. for index, value := range []byte(idCert) {
  82. if index == 17 {
  83. lastLetter = int(value)
  84. break
  85. }
  86. if !(value >= 48 && value <= 57) {
  87. return false
  88. }
  89. v := value - 48
  90. check = check + idCertMatrix[index]*int(v)
  91. }
  92. if !((lastLetter >= 48 && lastLetter <= 57) || lastLetter == 'X') {
  93. return false
  94. }
  95. timeLayout := "20060102" //转化所需模板
  96. loc, _ := time.LoadLocation("Local") //重要:获取时区
  97. _, err = time.ParseInLocation(timeLayout, idCert[6:14], loc) //使用模板在对应时区转化为time.time类型
  98. if err != nil {
  99. return false
  100. }
  101. verifyCode := int(idCertVerifyCode[check%11])
  102. if lastLetter == verifyCode {
  103. return true
  104. }
  105. return false
  106. }
  107. // StrToBytes 快读的string转byte
  108. func StrToBytes(s string) []byte {
  109. x := (*[2]uintptr)(unsafe.Pointer(&s))
  110. h := [3]uintptr{x[0], x[1], x[1]}
  111. return *(*[]byte)(unsafe.Pointer(&h))
  112. }
  113. func PrintStruct(prefix string, data interface{}) {
  114. bytes, _ := json.Marshal(data)
  115. fmt.Printf("%s, %s\n", prefix, bytes)
  116. }
  117. func VerifyEmailFormat(email string) bool {
  118. pattern := `\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*` //匹配电子邮箱
  119. reg := regexp.MustCompile(pattern)
  120. return reg.MatchString(email)
  121. }