This commit is contained in:
陈兔子
2025-09-12 02:33:18 +08:00
parent c30e4a3c6d
commit eaaac7b20a
25 changed files with 135 additions and 27 deletions

View File

@@ -1,8 +1,9 @@
package utils
import (
"github.com/spf13/cast"
"strings"
"github.com/spf13/cast"
)
// ArrayFilter - 数组过滤

View File

@@ -1,9 +1,10 @@
package utils
import (
"github.com/spf13/cast"
"reflect"
"sync"
"github.com/spf13/cast"
)
type AsyncClass[T any] struct {

View File

@@ -1,12 +1,13 @@
package utils
import (
"github.com/spf13/cast"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"github.com/spf13/cast"
)
// ForMap - 遍历数组,返回新数组

View File

@@ -2,7 +2,6 @@ package utils
import (
"fmt"
"github.com/spf13/cast"
"os"
"path"
"path/filepath"
@@ -11,6 +10,8 @@ import (
"strings"
"sync"
"time"
"github.com/spf13/cast"
)
type FileCacheClientItem struct {

View File

@@ -1,11 +1,12 @@
package utils
import (
"regexp"
"strings"
"github.com/spf13/cast"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"regexp"
"strings"
)
var Cases *CasesClass

View File

@@ -13,10 +13,11 @@ import (
"encoding/pem"
"errors"
"fmt"
"github.com/spf13/cast"
"hash/fnv"
rand2 "math/rand"
"time"
"github.com/spf13/cast"
)
type HashClass struct{}

View File

@@ -4,12 +4,13 @@ import (
"bytes"
"errors"
"fmt"
"github.com/spf13/cast"
"io"
"mime/multipart"
"net/http"
"net/url"
"strings"
"github.com/spf13/cast"
)
// CurlRequest - 发起请求的结构体

View File

@@ -2,8 +2,9 @@ package utils
import (
"fmt"
"github.com/spf13/cast"
"time"
"github.com/spf13/cast"
)
var Date *DateClass

View File

@@ -2,10 +2,11 @@ package utils
import (
"fmt"
"github.com/spf13/cast"
"github.com/spf13/viper"
"strings"
"sync"
"github.com/spf13/cast"
"github.com/spf13/viper"
)
// EnvToml - 读取配置文件

View File

@@ -4,13 +4,14 @@ import (
"archive/zip"
"bufio"
"errors"
"github.com/spf13/cast"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"github.com/spf13/cast"
)
// FileStruct - File 结构体

View File

@@ -1,9 +1,10 @@
package utils
import (
"github.com/spf13/cast"
"sort"
"strings"
"github.com/spf13/cast"
)
var Format *FormatClass

64
utils/gen.go Normal file
View File

@@ -0,0 +1,64 @@
package utils
import (
"fmt"
"math/rand"
"time"
"github.com/spf13/cast"
)
// Gen - 生成
var Gen *GenClass
type GenClass struct{}
// SerialNo 生成指定前缀和长度的序列号
// prefix: 前缀字符串
// len: 序列号总长度
// 格式: 前缀 + 日期(8位) + 时间(6位) + 随机数(动态长度)
// 当指定长度小于前缀+日期+时间的长度时,会截断到指定长度
func (this *GenClass) SerialNo(prefix string, length int) string {
// 种子
seed := Hash.Sum32(fmt.Sprintf("%s-%d-%d", Get.Mac(), Get.Pid(), time.Now().UnixNano()))
// 使用当前时间戳创建随机数生成器
source := rand.New(rand.NewSource(cast.ToInt64(seed)))
// 获取当前日期时间
now := time.Now()
datePart := now.Format("20060102") // 8位日期
timePart := now.Format("150405") // 6位时间
// 计算固定部分的总长度
fixedPart := prefix + datePart + timePart
fixedLength := len(fixedPart)
var serialNo string
// 如果指定长度小于等于固定部分长度,直接截断到指定长度
if length <= fixedLength {
serialNo = fixedPart[:length]
} else {
// 计算需要的随机数长度
randomLength := length - fixedLength
// 生成指定长度的随机数
// 计算10的randomLength次方作为随机数的上限
MAX := 1
for i := 0; i < randomLength; i++ {
MAX *= 10
}
// 生成随机数并格式化到指定长度
randomPart := fmt.Sprintf("%0" + fmt.Sprintf("%dd", randomLength), source.Intn(MAX))
// 组合所有部分
serialNo = fixedPart + randomPart
}
return serialNo
}

View File

@@ -2,11 +2,13 @@ package utils
import (
"fmt"
"github.com/spf13/cast"
"net"
"os"
"strings"
"sync"
"github.com/spf13/cast"
"github.com/spf13/viper"
)
// Get - 获取
@@ -115,3 +117,22 @@ func (this *GetClass) Pwd() (result string) {
}
return dir
}
// Env - 获取环境变量
func (this *GetClass) Env() (config *any) {
// 初始化 Viper
viper.SetConfigFile(".env")
viper.SetConfigType("env")
// 读取配置文件
if err := viper.ReadInConfig(); err != nil {
return
}
// 反序列化配置
if err := viper.Unmarshal(&config); err != nil {
return
}
return config
}

View File

@@ -1,9 +1,10 @@
package utils
import (
"github.com/spf13/cast"
"reflect"
"regexp"
"github.com/spf13/cast"
)
// Is - 是否为

View File

@@ -3,10 +3,11 @@ package utils
import (
JSON "encoding/json"
"fmt"
json "github.com/json-iterator/go"
"github.com/spf13/cast"
"sort"
"strings"
json "github.com/json-iterator/go"
"github.com/spf13/cast"
)
// Json - JSON 处理

View File

@@ -2,9 +2,10 @@ package utils
import (
"fmt"
"github.com/spf13/cast"
"strconv"
"strings"
"github.com/spf13/cast"
)
var Lang *LangClass

View File

@@ -1,8 +1,9 @@
package utils
import (
"github.com/spf13/cast"
"strings"
"github.com/spf13/cast"
)
var Mime *MimeClass

View File

@@ -1,10 +1,11 @@
package utils
import (
"github.com/spf13/cast"
"net"
"sync"
"time"
"github.com/spf13/cast"
)
// Net - 网络

View File

@@ -1,11 +1,12 @@
package utils
import (
"github.com/spf13/cast"
"net/url"
"regexp"
"strings"
"sync"
"github.com/spf13/cast"
)
// 读写锁 - 防止并发写入

View File

@@ -2,9 +2,10 @@ package utils
import (
"fmt"
"github.com/spf13/cast"
"math/rand"
"time"
"github.com/spf13/cast"
)
// Rand - 随机数

View File

@@ -1,8 +1,9 @@
package utils
import (
"github.com/spf13/cast"
"reflect"
"github.com/spf13/cast"
)
// Struct - 操作结构体

View File

@@ -1,9 +1,10 @@
package utils
import (
"github.com/spf13/cast"
"regexp"
"strings"
"github.com/spf13/cast"
)
// Unity - 统一规范化

View File

@@ -4,9 +4,10 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/spf13/cast"
"reflect"
"strings"
"github.com/spf13/cast"
)
type validator struct {

View File

@@ -1,11 +1,12 @@
package utils
import (
"github.com/spf13/cast"
"regexp"
"runtime"
"strconv"
"strings"
"github.com/spf13/cast"
)
// Version - 版本

View File

@@ -1,9 +1,10 @@
package utils
import (
"os"
"github.com/spf13/cast"
"github.com/spf13/viper"
"os"
)
type ViperClass struct {