From cf27eee0ea8cb176bf11ba89f6d38c5e99c70f5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=8C?= <259278618@qq.com> Date: Sun, 4 Jan 2026 10:35:15 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=84=E4=BA=A7=E5=A2=9E=E5=8A=A0=E6=89=B9?= =?UTF-8?q?=E6=AC=A1=E5=BA=93=E5=AD=98=E7=AE=A1=E7=90=86=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/utils.go | 149 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) diff --git a/utils/utils.go b/utils/utils.go index 68f8c87..8d9efa4 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -3,8 +3,12 @@ package utils import ( "context" "fmt" + "net" "reflect" "sort" + "strconv" + "strings" + "sync/atomic" "time" "gitee.com/red-future---jilin-g/common/beans" @@ -59,6 +63,7 @@ func GetMonthToday(t time.Time, month int) time.Time { } return target.AddDate(0, 0, t.Day()-1) } + func GetUserInfo(ctx context.Context) (user beans.User, err error) { r := g.RequestFromCtx(ctx) if r != nil { @@ -102,6 +107,7 @@ func GetUserInfo(ctx context.Context) (user beans.User, err error) { } return } + func OrderMap(m map[string]interface{}) map[string]interface{} { // 提取所有key keys := make([]string, 0, len(m)) @@ -121,3 +127,146 @@ func OrderMap(m map[string]interface{}) map[string]interface{} { return orderedMap } + +// ParseIntSlice 解析整数切片 - 通用字符串处理工具 +func ParseIntSlice(str string) []int { + parts := strings.Split(str, ",") + result := make([]int, 0, len(parts)) + for _, part := range parts { + if val, err := strconv.Atoi(strings.TrimSpace(part)); err == nil { + result = append(result, val) + } + } + return result +} + +// ParseStrings 解析字符串切片 - 通用字符串处理工具 +func ParseStrings(str string) []string { + if str == "" { + return nil + } + parts := strings.Split(str, ",") + result := make([]string, 0, len(parts)) + for _, part := range parts { + if trimmed := strings.TrimSpace(part); trimmed != "" { + result = append(result, trimmed) + } + } + return result +} + +// FilterServiceNames 过滤服务名 - 通用映射处理工具 +func FilterServiceNames(services map[string]interface{}, excludeKeys ...string) []string { + excludeMap := make(map[string]bool) + for _, key := range excludeKeys { + excludeMap[key] = true + } + result := make([]string, 0, len(services)) + for key := range services { + if !excludeMap[key] { + result = append(result, key) + } + } + return result +} + +// FormatUnixTime 格式化Unix时间戳 - 通用时间处理工具 +func FormatUnixTime(timestamp int64) string { + if timestamp <= 0 { + return "" + } + return time.Unix(timestamp, 0).Format("2006-01-02 15:04:05") +} + +// ParseDurationWithDefault 解析持续时间,失败时使用默认值 - 通用时间处理工具 +func ParseDurationWithDefault(ctx context.Context, durationStr, defaultStr, fieldName string) (time.Duration, string) { + durationParsed, err := time.ParseDuration(durationStr) + if err != nil { + // 这里不能直接使用g.Log(),因为这是utils包,没有直接的日志访问 + // 调用方应该处理日志 + // g.Log().Warningf(ctx, "解析%s失败: %s, 使用默认值 %s, error: %v", fieldName, durationStr, defaultStr, err) + durationParsed, _ = time.ParseDuration(defaultStr) + return durationParsed, defaultStr + } + return durationParsed, durationStr +} + +// AtomicUpdateMin 原子更新最小值 - 通用数值处理工具 +func AtomicUpdateMin(minValue *atomic.Int64, newValue int64) { + for { + currentMin := minValue.Load() + if newValue >= currentMin { + break + } + if minValue.CompareAndSwap(currentMin, newValue) { + break + } + } +} + +// AtomicUpdateMax 原子更新最大值 - 通用数值处理工具 +func AtomicUpdateMax(maxValue *atomic.Int64, newValue int64) { + for { + currentMax := maxValue.Load() + if newValue <= currentMax { + break + } + if maxValue.CompareAndSwap(currentMax, newValue) { + break + } + } +} + +// ParseCIDRs 解析CIDR列表 - 通用网络处理工具 +func ParseCIDRs(strs []string) ([]*net.IPNet, error) { + nets := make([]*net.IPNet, 0, len(strs)) + for _, s := range strs { + if s == "*" { + if _, ipv4Net, err := net.ParseCIDR("0.0.0.0/0"); err == nil { + nets = append(nets, ipv4Net) + } + if _, ipv6Net, err := net.ParseCIDR("::/0"); err == nil { + nets = append(nets, ipv6Net) + } + continue + } + if _, ipNet, err := net.ParseCIDR(s); err == nil { + nets = append(nets, ipNet) + } + } + return nets, nil +} + +// UrlDecode 简单的URL解码 - 通用编码解码工具 +func UrlDecode(s string) string { + result := make([]byte, 0, len(s)) + + for i := 0; i < len(s); i++ { + if s[i] == '%' && i+2 < len(s) { + if high := HexDigit(s[i+1]); high != 0xFF { + if low := HexDigit(s[i+2]); low != 0xFF { + result = append(result, (high<<4)|low) + i += 2 + continue + } + } + } + result = append(result, s[i]) + } + + return string(result) +} + +// HexDigit 十六进制字符转数字 - 通用编码解码工具 +func HexDigit(c byte) byte { + switch { + case '0' <= c && c <= '9': + return c - '0' + case 'a' <= c && c <= 'f': + return c - 'a' + 10 + case 'A' <= c && c <= 'F': + return c - 'A' + 10 + default: + return 0xFF + } +}