tools.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package utils
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "math/rand"
  10. "net/http"
  11. "strings"
  12. "time"
  13. )
  14. // UnixToDate 10位时间戳转日期字符串
  15. func UnixToDate(timestamp int) string {
  16. return time.Unix(int64(timestamp), 0).Format("2006-01-02 15:04:05")
  17. }
  18. // DateToUnix 日期字符串转10位时间戳
  19. func DateToUnix(str string) int64 {
  20. t, err := time.ParseInLocation("2006-01-02 15:04:05", str, time.Local)
  21. if err != nil {
  22. return 0
  23. }
  24. return t.Unix()
  25. }
  26. // GetUnixTimestamp 获取10位时间戳
  27. func GetUnixTimestamp() int64 {
  28. return time.Now().Unix()
  29. }
  30. // GetUnixNano 获取13位时间戳
  31. func GetUnixNano() int64 {
  32. return time.Now().UnixNano() / 1e6
  33. }
  34. // GetDate 获取当前日期时间字符串
  35. func GetDate() string {
  36. return time.Now().Format("2006-01-02 15:04:05")
  37. }
  38. // Md5 MD5加密
  39. func Md5(str string) string {
  40. m := md5.New()
  41. m.Write([]byte(str))
  42. return hex.EncodeToString(m.Sum(nil))
  43. }
  44. // 邀请码生成常量
  45. const (
  46. inviteCodeBase = "E8S2DZX9WYLTN6BQF7CP5IK3MJUAR4HV"
  47. inviteCodeDecimal = 32
  48. inviteCodePad = "A"
  49. inviteCodeLen = 8
  50. )
  51. // EncodeInviteCode 生成邀请码
  52. func EncodeInviteCode(uid uint64) string {
  53. id := uid
  54. res := ""
  55. for id != 0 {
  56. mod := id % inviteCodeDecimal
  57. id = id / inviteCodeDecimal
  58. res += string(inviteCodeBase[mod])
  59. }
  60. if len(res) < inviteCodeLen {
  61. res += inviteCodePad
  62. for i := 0; i < inviteCodeLen-len(res)-1; i++ {
  63. res += string(inviteCodeBase[(int(uid)+i)%inviteCodeDecimal])
  64. }
  65. }
  66. return res
  67. }
  68. // GetRandomCode 生成指定长度的随机数字字符串
  69. func GetRandomCode(length int) string {
  70. var sb strings.Builder
  71. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  72. for i := 0; i < length; i++ {
  73. fmt.Fprintf(&sb, "%d", r.Intn(10))
  74. }
  75. return sb.String()
  76. }
  77. // GetRandomString 生成指定长度的随机字符串
  78. func GetRandomString(length int) string {
  79. const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  80. result := make([]byte, length)
  81. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  82. for i := 0; i < length; i++ {
  83. result[i] = chars[r.Intn(len(chars))]
  84. }
  85. return string(result)
  86. }
  87. // UniqueStrings 字符串切片去重
  88. func UniqueStrings(s []string) []string {
  89. seen := make(map[string]struct{})
  90. result := make([]string, 0, len(s))
  91. for _, v := range s {
  92. if _, ok := seen[v]; !ok {
  93. seen[v] = struct{}{}
  94. result = append(result, v)
  95. }
  96. }
  97. return result
  98. }
  99. // HTTPGet 发送GET请求
  100. func HTTPGet(url string) (string, error) {
  101. client := &http.Client{Timeout: 5 * time.Second}
  102. resp, err := client.Get(url)
  103. if err != nil {
  104. return "", err
  105. }
  106. defer resp.Body.Close()
  107. body, err := io.ReadAll(resp.Body)
  108. if err != nil {
  109. return "", err
  110. }
  111. return string(body), nil
  112. }
  113. // HTTPPost 发送POST请求
  114. func HTTPPost(url string, data interface{}, contentType string) (string, error) {
  115. jsonData, err := json.Marshal(data)
  116. if err != nil {
  117. return "", err
  118. }
  119. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
  120. if err != nil {
  121. return "", err
  122. }
  123. req.Header.Set("Content-Type", contentType)
  124. client := &http.Client{Timeout: 5 * time.Second}
  125. resp, err := client.Do(req)
  126. if err != nil {
  127. return "", err
  128. }
  129. defer resp.Body.Close()
  130. body, err := io.ReadAll(resp.Body)
  131. if err != nil {
  132. return "", err
  133. }
  134. return string(body), nil
  135. }
  136. // Min 返回两个整数中的较小值
  137. func Min(a, b int) int {
  138. if a < b {
  139. return a
  140. }
  141. return b
  142. }
  143. // Max 返回两个整数中的较大值
  144. func Max(a, b int) int {
  145. if a > b {
  146. return a
  147. }
  148. return b
  149. }