utils.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package utils
  2. import (
  3. "crypto/hmac"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "fmt"
  7. uuid "github.com/satori/go.uuid"
  8. "strings"
  9. "time"
  10. )
  11. func RemoveDuplicates(slice []string) []string {
  12. m := make(map[string]bool)
  13. unique := []string{}
  14. for _, item := range slice {
  15. if _, found := m[item]; !found {
  16. m[item] = true
  17. unique = append(unique, item)
  18. }
  19. }
  20. return unique
  21. }
  22. func AllBlank(str ...string) bool {
  23. for _, s := range str {
  24. if !HasBlank(s) {
  25. return false
  26. }
  27. }
  28. return true
  29. }
  30. func HasBlank(str ...string) bool {
  31. for _, s := range str {
  32. if strings.Trim(s, " ") == "" {
  33. return true
  34. }
  35. }
  36. return false
  37. }
  38. func HasText(str ...string) bool {
  39. for _, s := range str {
  40. if strings.Trim(s, " ") != "" {
  41. return true
  42. }
  43. }
  44. return false
  45. }
  46. func HasNil(objs ...interface{}) bool {
  47. for _, obj := range objs {
  48. if obj == nil {
  49. return true
  50. }
  51. }
  52. return false
  53. }
  54. func MD5(str string) (string, error) {
  55. m := md5.New()
  56. _, err := m.Write([]byte(str))
  57. if err != nil {
  58. return "", err
  59. }
  60. return hex.EncodeToString(m.Sum(nil)), nil
  61. }
  62. func SimpleUUID() string {
  63. return strings.ReplaceAll(GetUUID(), "-", "")
  64. }
  65. func GetUUID() string {
  66. return uuid.NewV4().String()
  67. }
  68. func Hmac(key string, data string) string {
  69. hmacHash := hmac.New(md5.New, []byte(key))
  70. hmacHash.Write([]byte(data))
  71. return hex.EncodeToString(hmacHash.Sum([]byte("")))
  72. }
  73. func GetAllMonthsOfTheCurrentQuarter() []string {
  74. // 获取当前时间
  75. now := time.Now()
  76. currentYear, currentMonth, _ := now.Date()
  77. // 计算季度
  78. quarter := (int(currentMonth)-1)/3 + 1
  79. // 构造季度内的所有月份
  80. months := make([]string, 3)
  81. for i := 0; i < 3; i++ {
  82. monthNum := (quarter-1)*3 + i + 1
  83. if monthNum < 10 {
  84. months[i] = fmt.Sprintf("%d-0%d", currentYear, monthNum)
  85. } else {
  86. months[i] = fmt.Sprintf("%d-%d", currentYear, monthNum)
  87. }
  88. }
  89. return months
  90. }
  91. func GetHistoryMonths() []string {
  92. historyMonth := make([]string, 0)
  93. // 获取当前时间
  94. now := time.Now()
  95. currentMonth := int(now.Month())
  96. // 月份的中文表示
  97. monthNames := []string{"", "一月", "二月", "三月", "四月", "五月", "六月",
  98. "七月", "八月", "九月", "十月", "十一月", "十二月"}
  99. // 遍历从年初到现在的所有月份
  100. for month := 1; month <= currentMonth; month++ {
  101. // 注意:月份Names数组的第0个元素是空的,所以从1开始索引
  102. fmt.Println(monthNames[month])
  103. historyMonth = append(historyMonth, monthNames[month])
  104. }
  105. return historyMonth
  106. }