package utils import ( "crypto/hmac" "crypto/md5" "encoding/hex" "fmt" uuid "github.com/satori/go.uuid" "strings" "time" ) func RemoveDuplicates(slice []string) []string { m := make(map[string]bool) unique := []string{} for _, item := range slice { if _, found := m[item]; !found { m[item] = true unique = append(unique, item) } } return unique } func AllBlank(str ...string) bool { for _, s := range str { if !HasBlank(s) { return false } } return true } func HasBlank(str ...string) bool { for _, s := range str { if strings.Trim(s, " ") == "" { return true } } return false } func HasText(str ...string) bool { for _, s := range str { if strings.Trim(s, " ") != "" { return true } } return false } func HasNil(objs ...interface{}) bool { for _, obj := range objs { if obj == nil { return true } } return false } func MD5(str string) (string, error) { m := md5.New() _, err := m.Write([]byte(str)) if err != nil { return "", err } return hex.EncodeToString(m.Sum(nil)), nil } func SimpleUUID() string { return strings.ReplaceAll(GetUUID(), "-", "") } func GetUUID() string { return uuid.NewV4().String() } func Hmac(key string, data string) string { hmacHash := hmac.New(md5.New, []byte(key)) hmacHash.Write([]byte(data)) return hex.EncodeToString(hmacHash.Sum([]byte(""))) } func GetAllMonthsOfTheCurrentQuarter() []string { // 获取当前时间 now := time.Now() currentYear, currentMonth, _ := now.Date() // 计算季度 quarter := (int(currentMonth)-1)/3 + 1 // 构造季度内的所有月份 months := make([]string, 3) for i := 0; i < 3; i++ { monthNum := (quarter-1)*3 + i + 1 if monthNum < 10 { months[i] = fmt.Sprintf("%d-0%d", currentYear, monthNum) } else { months[i] = fmt.Sprintf("%d-%d", currentYear, monthNum) } } return months } func GetHistoryMonths() []string { historyMonth := make([]string, 0) // 获取当前时间 now := time.Now() currentMonth := int(now.Month()) // 月份的中文表示 monthNames := []string{"", "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"} // 遍历从年初到现在的所有月份 for month := 1; month <= currentMonth; month++ { // 注意:月份Names数组的第0个元素是空的,所以从1开始索引 fmt.Println(monthNames[month]) historyMonth = append(historyMonth, monthNames[month]) } return historyMonth }