package utils import ( "bytes" "crypto/md5" "encoding/hex" "encoding/json" "fmt" "io" "math/rand" "net/http" "strings" "time" ) // UnixToDate 10位时间戳转日期字符串 func UnixToDate(timestamp int) string { return time.Unix(int64(timestamp), 0).Format("2006-01-02 15:04:05") } // DateToUnix 日期字符串转10位时间戳 func DateToUnix(str string) int64 { t, err := time.ParseInLocation("2006-01-02 15:04:05", str, time.Local) if err != nil { return 0 } return t.Unix() } // GetUnixTimestamp 获取10位时间戳 func GetUnixTimestamp() int64 { return time.Now().Unix() } // GetUnixNano 获取13位时间戳 func GetUnixNano() int64 { return time.Now().UnixNano() / 1e6 } // GetDate 获取当前日期时间字符串 func GetDate() string { return time.Now().Format("2006-01-02 15:04:05") } // Md5 MD5加密 func Md5(str string) string { m := md5.New() m.Write([]byte(str)) return hex.EncodeToString(m.Sum(nil)) } // 邀请码生成常量 const ( inviteCodeBase = "E8S2DZX9WYLTN6BQF7CP5IK3MJUAR4HV" inviteCodeDecimal = 32 inviteCodePad = "A" inviteCodeLen = 8 ) // EncodeInviteCode 生成邀请码 func EncodeInviteCode(uid uint64) string { id := uid res := "" for id != 0 { mod := id % inviteCodeDecimal id = id / inviteCodeDecimal res += string(inviteCodeBase[mod]) } if len(res) < inviteCodeLen { res += inviteCodePad for i := 0; i < inviteCodeLen-len(res)-1; i++ { res += string(inviteCodeBase[(int(uid)+i)%inviteCodeDecimal]) } } return res } // GetRandomCode 生成指定长度的随机数字字符串 func GetRandomCode(length int) string { var sb strings.Builder r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < length; i++ { fmt.Fprintf(&sb, "%d", r.Intn(10)) } return sb.String() } // GetRandomString 生成指定长度的随机字符串 func GetRandomString(length int) string { const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" result := make([]byte, length) r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < length; i++ { result[i] = chars[r.Intn(len(chars))] } return string(result) } // UniqueStrings 字符串切片去重 func UniqueStrings(s []string) []string { seen := make(map[string]struct{}) result := make([]string, 0, len(s)) for _, v := range s { if _, ok := seen[v]; !ok { seen[v] = struct{}{} result = append(result, v) } } return result } // HTTPGet 发送GET请求 func HTTPGet(url string) (string, error) { client := &http.Client{Timeout: 5 * time.Second} resp, err := client.Get(url) if err != nil { return "", err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return "", err } return string(body), nil } // HTTPPost 发送POST请求 func HTTPPost(url string, data interface{}, contentType string) (string, error) { jsonData, err := json.Marshal(data) if err != nil { return "", err } req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { return "", err } req.Header.Set("Content-Type", contentType) client := &http.Client{Timeout: 5 * time.Second} resp, err := client.Do(req) if err != nil { return "", err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return "", err } return string(body), nil } // Min 返回两个整数中的较小值 func Min(a, b int) int { if a < b { return a } return b } // Max 返回两个整数中的较大值 func Max(a, b int) int { if a > b { return a } return b }