common.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package utils
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "fmt"
  6. "math/rand"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // 随机种子
  13. var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
  14. func MD5(data string) string {
  15. m := md5.Sum([]byte(data))
  16. return hex.EncodeToString(m[:])
  17. }
  18. func CheckPwd(pwd string) bool {
  19. compile := `([0-9a-z]+){6,12}|(a-z0-9]+){6,12}`
  20. reg := regexp.MustCompile(compile)
  21. flag := reg.MatchString(pwd)
  22. return flag
  23. }
  24. // 校验邮箱格式
  25. func ValidateEmailFormatat(email string) bool {
  26. reg := regexp.MustCompile(RegularEmail)
  27. return reg.MatchString(email)
  28. }
  29. // 验证是否是手机号
  30. func ValidateMobileFormatat(mobileNum string) bool {
  31. reg := regexp.MustCompile(RegularMobile)
  32. return reg.MatchString(mobileNum)
  33. }
  34. // 计算分页起始页
  35. func StartIndex(page, pagesize int) int {
  36. if page > 1 {
  37. return (page - 1) * pagesize
  38. }
  39. return 0
  40. }
  41. // GetLikeKeywordPars 获取sql查询中的参数切片
  42. func GetLikeKeywordPars(pars []interface{}, keyword string, num int) (newPars []interface{}) {
  43. newPars = pars
  44. if newPars == nil {
  45. newPars = make([]interface{}, 0)
  46. }
  47. for i := 1; i <= num; i++ {
  48. newPars = append(newPars, `%`+keyword+`%`)
  49. }
  50. return
  51. }
  52. func StringsToJSON(str string) string {
  53. rs := []rune(str)
  54. jsons := ""
  55. for _, r := range rs {
  56. rint := int(r)
  57. if rint < 128 {
  58. jsons += string(r)
  59. } else {
  60. jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
  61. }
  62. }
  63. return jsons
  64. }
  65. // 数据没有记录
  66. func ErrNoRow() string {
  67. return "<QuerySeter> no row found"
  68. }
  69. func GetRandStringNoSpecialChar(size int) string {
  70. allLetterDigit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
  71. randomSb := ""
  72. digitSize := len(allLetterDigit)
  73. for i := 0; i < size; i++ {
  74. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  75. }
  76. return randomSb
  77. }
  78. func GetOrmReplaceHolder(num int) string {
  79. var stringBuffer strings.Builder
  80. for i := 0; i < num; i++ {
  81. stringBuffer.WriteString("?")
  82. if i != num-1 {
  83. stringBuffer.WriteString(",")
  84. }
  85. }
  86. return stringBuffer.String()
  87. }
  88. func Unique[T comparable](slice []T) []T {
  89. seen := make(map[T]struct{})
  90. var unique []T
  91. for _, v := range slice {
  92. if _, exists := seen[v]; !exists {
  93. unique = append(unique, v)
  94. seen[v] = struct{}{}
  95. }
  96. }
  97. return unique
  98. }
  99. // GetOrmInReplace 获取orm的in查询替换?的方法
  100. func GetOrmInReplace(num int) string {
  101. template := make([]string, num)
  102. for i := 0; i < num; i++ {
  103. template[i] = "?"
  104. }
  105. return strings.Join(template, ",")
  106. }
  107. func TimeTransferString(format string, t time.Time) string {
  108. str := t.Format(format)
  109. if t.IsZero() {
  110. return ""
  111. }
  112. return str
  113. }
  114. // HideMobileMiddle 隐藏大陆手机号中间四位
  115. func HideMobileMiddle(mobile string) string {
  116. if !ValidateMobileFormatat(mobile) {
  117. return mobile
  118. }
  119. return fmt.Sprintf("%s****%s", mobile[:3], mobile[7:])
  120. }
  121. func SecondsToHMS(seconds int64) string {
  122. duration := time.Duration(seconds) * time.Second
  123. hours := int64(duration.Hours())
  124. minutes := int64(duration.Minutes()) % 60
  125. secs := int64(duration.Seconds()) % 60
  126. var result string
  127. if hours > 0 {
  128. result += fmt.Sprintf("%d时", hours)
  129. }
  130. if minutes > 0 || (hours > 0 && secs > 0) {
  131. result += fmt.Sprintf("%d分", minutes)
  132. }
  133. if secs > 0 || (hours == 0 && minutes == 0) {
  134. result += fmt.Sprintf("%d秒", secs)
  135. }
  136. return result
  137. }