utils

package
v1.9.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2026 License: Apache-2.0 Imports: 50 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Array struct {
	Diff         func(array1, array2 []any) (slice []any)
	Intersect    func(array1, array2 []any) (slice []any)
	Filter       func(array []string) (slice []string)
	Remove       func(array []string, args ...string) (slice []string)
	Unique       func(array []any) (slice []any)
	Empty        func(array []any) (slice []any)
	Reverse      func(array []any) (slice []any)
	MapWithField func(array []map[string]any, field any) (slice []any)
	Paging       func(rows []any, page, limit int) (code int, count int, data []any)
}
View Source
var Cases = &CasesClass{
	Params: CasesParams{
		IsUpper: false,
	},
}
View Source
var In struct {
	Array func(value any, array []any) (ok bool)
}
View Source
var Jwt = &JwtClass{
	Body: dto.JwtBody{
		Key:     "inis-jwt-key",
		Issuer:  "inis-issuer",
		Subject: "inis-subject",
		Expired: 7 * 24 * 60 * 60,
	},
}
View Source
var Map struct {
	WithField    func(data map[string]any, field []string) (result map[string]any)
	WithoutField func(data map[string]any, field []string) (result map[string]any)
	ToURL        func(data map[string]any) (result string)
	Keys         func(data map[string]any) (result []string)
	Values       func(data map[string]any) (result []any)
	Trim         func(data map[string]any) (result map[string]any)
}
View Source
var MimeMap = map[string]string{
	"js":   "application/javascript",
	"json": "application/json",
	"xml":  "application/xml",
	"css":  "text/css",
	"html": "text/html",
	"txt":  "text/plain",
	"gif":  "image/gif",
	"png":  "image/png",
	"jpg":  "image/jpeg",
	"jpeg": "image/jpeg",
	"svg":  "image/svg+xml",
	"ico":  "image/x-icon",
	"pdf":  "application/pdf",
	"zip":  "application/zip",
	"rar":  "application/x-rar-compressed",
	"gz":   "application/x-gzip",
	"tar":  "application/x-tar",
	"7z":   "application/x-7z-compressed",
	"mp3":  "audio/mpeg",
	"mp4":  "video/mp4",
	"avi":  "video/x-msvideo",
	"doc":  "application/msword",
	"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
	"xls":  "application/vnd.ms-excel",
	"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
	"ppt":  "application/vnd.ms-powerpoint",
	"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
	"csv":  "text/csv",
	"md":   "text/markdown",
}
View Source
var Url = &UrlClass{}
View Source
var Validate = func(model any) *validator {
	return &validator{
		model: model,
		rule:  ValidateRules,
	}
}

Validate - 验证器入口

Functions

func ArrayDiff

func ArrayDiff[T any](array1, array2 []T) (slice []T)

ArrayDiff - 数组差集

func ArrayEmpty

func ArrayEmpty[T any](array []T) (slice []T)

ArrayEmpty - 数组去空

func ArrayFilter

func ArrayFilter(array []string) (slice []string)

ArrayFilter - 数组过滤

func ArrayIntersect

func ArrayIntersect[T any](array1, array2 []T) (slice []T)

ArrayIntersect - 交集

func ArrayMapWithField

func ArrayMapWithField(array []map[string]any, field any) (slice []any)

ArrayMapWithField - 数组Map保留指定字段

func ArrayPaging

func ArrayPaging[T any](rows []T, page, limit int) (code int, count int, data []T)

ArrayPaging - 分页处理

func ArrayRemove

func ArrayRemove(array []string, args ...string) []string

ArrayRemove - 数组删除

func ArrayReverse

func ArrayReverse[T any](array []T) (slice []T)

ArrayReverse - 数组反转

func ArrayUnique

func ArrayUnique[T any](array []T) (slice []T)

ArrayUnique - 数组去重

func Calc

func Calc(input any) (output float64)

Calc - 计算器

func Caller

func Caller() *caller

Caller 获取代码调用者

func CustomProcessApi

func CustomProcessApi(url string, api string) (result string)

func Default

func Default[T any](param T, value ...T) (result T)

Default - 设置默认值

func EnvToml

func EnvToml(args ...string) (result any)

EnvToml - 读取配置文件

func ForMap

func ForMap[T any](slice []T, fun func(item T) (result T)) (newSlice []T)

ForMap - 遍历数组,返回新数组

func GetCaller

func GetCaller() (funcName string, fileName string, line int)

func InArray

func InArray[T any](value T, array []T) (ok bool)

InArray - 判断某个值是否在数组中

func InMapKey

func InMapKey(key string, array map[string]any) bool

InMapKey 在 map key 中

func InMapValue

func InMapValue(value any, array map[string]string) bool

InMapValue 在 map value 中

func MapKeys

func MapKeys[T map[string]any](data T) (result []string)

MapKeys - 获取 map 的所有 key

func MapMerge

func MapMerge(map1 map[any]any, map2 map[any]any) map[any]any

MapMerge map合并

func MapMergeString

func MapMergeString(map1 map[string]string, map2 map[string]string) map[string]string

MapMergeString map合并

func MapToURL

func MapToURL(params map[string]any) (result string)

MapToURL - 无序 map 转 有序 URL

func MapTrim

func MapTrim[T map[string]any](data T) (result T)

MapTrim - 去除 map 中的空值

func MapValues

func MapValues[T map[string]any](data T) (result []any)

MapValues - 获取 map 的所有 value

func MapWithField

func MapWithField[T map[string]any](data T, field []string) (result T)

MapWithField - 从map中提取指定字段

func MapWithoutField

func MapWithoutField[T map[string]any](data T, field []string) (result T)

MapWithoutField - 从map中排除指定字段

func Redirect

func Redirect(url any) (result string)

Redirect - 获取重定向地址

func Replace

func Replace(value any, params map[string]any) (result string)

Replace - 字符串替换

func Ternary

func Ternary[T any](IF bool, TRUE T, FALSE T) T

Ternary - 三元运算符

func ValidateRules

func ValidateRules(name string, value any, rule string, message map[string]string) (err error)

ValidateRules - 验证规则 *

  • @rule - 内置规则 - 如下:
  • required:必填
  • min:最小值
  • max:最大值
  • email:是否为邮箱
  • number:是否为数字
  • float:是否为浮点数
  • bool:是否为布尔值
  • slice:是否为切片 - (数组)
  • jsonStr:是否为 JSON 字符串
  • accepted:验证某个字段是否为为 yes, on, 或是 1
  • date:是否为日期
  • alpha:只能包含字母
  • alphaNum:只能包含字母和数字
  • alphaDash:只能包含字母、数字和下划线_及破折号-
  • chs:只能包含汉字
  • chsAlpha:只能包含汉字、字母
  • chsAlphaNum:只能包含汉字、字母和数字
  • chsDash:只能是汉字、字母、数字和下划线_及破折号-
  • cntrl:是否为控制字符 - (换行、缩进、空格)
  • graph:是否为可见字符 - (除空格外的所有可打印字符)
  • lower:是否为小写字母
  • upper:是否为大写字母
  • space:是否为空白字符 - (空格、制表符、换页符等)
  • xdigit:是否为十六进制字符 - (0-9、a-f、A-F)
  • activeUrl:是否为有效的域名或者IP
  • url:是否为有效的URL地址
  • ip:是否为IP地址
  • mobile:是否为手机号
  • idCard:是否为身份证号
  • MacAddr:是否为MAC地址
  • zip:是否为邮政编码 *

Types

type AesClass

type AesClass struct {
	Key string
	Iv  string
}
var AES *AesClass

AES - 对称加密

func (*AesClass) DeKeyIv

func (this *AesClass) DeKeyIv(ciphertext string) (key, iv string, err error)

DeKeyIv - 从32位字符串还原 AES 密钥和 IV

func (*AesClass) Decrypt

func (this *AesClass) Decrypt(text any) (result *dto.AesResp)

Decrypt 解密

func (*AesClass) EnKeyIv

func (this *AesClass) EnKeyIv(key, iv string) (ciphertext string, err error)

EnKeyIv - 还原 AES 密钥和 IV

func (*AesClass) Encrypt

func (this *AesClass) Encrypt(text any) (result *dto.AesResp)

Encrypt 加密

func (*AesClass) NewAes

func (this *AesClass) NewAes(key, iv any) *AesClass

type AsciiClass

type AsciiClass struct{}
var Ascii *AsciiClass

func (*AsciiClass) ToString

func (this *AsciiClass) ToString(params map[string]any, omitempty bool) (result string)

ToString - 根据ASCII码排序

type AsyncClass

type AsyncClass[T any] struct {
	// 读写锁
	Mutex sync.RWMutex
	// 等待组
	Wait sync.WaitGroup
	// 数据
	Data T
}

func Async

func Async[T any]() *AsyncClass[T]

Async - 异步数据

func (*AsyncClass[T]) Get

func (this *AsyncClass[T]) Get(key string) any

Get - 获取数据

func (*AsyncClass[T]) Has

func (this *AsyncClass[T]) Has(key string) (ok bool)

Has - 判断是否存在

func (*AsyncClass[T]) Result

func (this *AsyncClass[T]) Result() T

Result - 获取所有数据

func (*AsyncClass[T]) Set

func (this *AsyncClass[T]) Set(key string, val any)

Set - 设置数据

type CasesClass

type CasesClass struct {
	// 参数
	Params CasesParams
}

func (*CasesClass) Camel

func (this *CasesClass) Camel(name any) string

Camel 骆驼命名法

func (*CasesClass) IsUpper

func (this *CasesClass) IsUpper(yes bool) *CasesClass

IsUpper 是否大写

func (*CasesClass) Kebab

func (this *CasesClass) Kebab(name any) string

Kebab 烤肉串式命名法

func (*CasesClass) Snake

func (this *CasesClass) Snake(name any) string

Snake 蛇式命名法

func (*CasesClass) Space

func (this *CasesClass) Space(name any) string

Space 空格命名法

type CasesParams

type CasesParams struct {
	// 是否大写
	IsUpper bool
}

type CurlClass

type CurlClass struct {
	// contains filtered or unexported fields
}

CurlClass - Curl 结构体

func Curl

func Curl(request ...CurlRequest) *CurlClass

Curl - 发起请求 - 入口

func (*CurlClass) Body

func (this *CurlClass) Body(body any) *CurlClass

Body - 定义请求体

func (*CurlClass) Client

func (this *CurlClass) Client(client *http.Client) *CurlClass

Client - 定义请求客户端

func (*CurlClass) Data

func (this *CurlClass) Data(key string, value any) *CurlClass

Data - 定义请求数据

func (*CurlClass) Datas

func (this *CurlClass) Datas(data map[string]any) *CurlClass

Datas - 批量定义请求数据

func (*CurlClass) Delete

func (this *CurlClass) Delete(url string) *CurlClass

Delete - 发起 DELETE 请求

func (*CurlClass) Get

func (this *CurlClass) Get(url string) *CurlClass

Get - 发起 GET 请求

func (*CurlClass) Header

func (this *CurlClass) Header(key any, value any) *CurlClass

Header - 定义请求头

func (*CurlClass) Headers

func (this *CurlClass) Headers(headers map[string]any) *CurlClass

Headers - 批量定义请求头

func (*CurlClass) Method

func (this *CurlClass) Method(method string) *CurlClass

Method - 定义请求类型 - 默认 GET

func (*CurlClass) Patch

func (this *CurlClass) Patch(url string) *CurlClass

Patch - 发起 PATCH 请求

func (*CurlClass) Post

func (this *CurlClass) Post(url string) *CurlClass

Post - 发起 POST 请求

func (*CurlClass) Put

func (this *CurlClass) Put(url string) *CurlClass

Put - 发起 PUT 请求

func (*CurlClass) Query

func (this *CurlClass) Query(key any, value any) *CurlClass

Query - 定义请求参数

func (*CurlClass) Querys

func (this *CurlClass) Querys(params map[string]any) *CurlClass

Querys - 批量定义请求参数

func (*CurlClass) Send

func (this *CurlClass) Send() *CurlResponse

Send - 发起请求

func (*CurlClass) Url

func (this *CurlClass) Url(url string) *CurlClass

Url - 定义请求地址

type CurlRequest

type CurlRequest struct {
	Body    any
	Url     string
	Method  string
	Client  *http.Client
	Data    map[string]any
	Query   map[string]any
	Headers map[string]any
}

CurlRequest - 发起请求的结构体

type CurlResponse

type CurlResponse struct {
	StatusCode int
	Request    *http.Request
	Headers    *http.Header
	Body       *io.ReadCloser
	Byte       []byte
	Text       string
	Json       map[string]any
	Error      error
}

CurlResponse - 响应的结构体

type DateClass

type DateClass struct {
	Time time.Time
}
var Date *DateClass

func (*DateClass) Format

func (this *DateClass) Format(layout string) string

Format 格式化日期

func (*DateClass) FormatSeconds

func (this *DateClass) FormatSeconds(seconds any) string

FormatSeconds 将秒数格式化为友好的时间字符串

type EnvClass

type EnvClass struct {
	// contains filtered or unexported fields
}

func Env

func Env() *EnvClass

func (*EnvClass) All

func (this *EnvClass) All() (result map[string]any)

func (*EnvClass) Get

func (this *EnvClass) Get(key any, def ...any) (result any)

func (*EnvClass) Mode

func (this *EnvClass) Mode(mode string) *EnvClass

func (*EnvClass) Path

func (this *EnvClass) Path(path string) *EnvClass

func (*EnvClass) TomlAll

func (this *EnvClass) TomlAll() (result any)

type FileCacheClient

type FileCacheClient struct {
	// contains filtered or unexported fields
}

func NewFileCache

func NewFileCache(dir any, expire any, prefix ...any) (*FileCacheClient, error)

NewFileCache - 新建文件缓存 *

  • @param dir 缓存目录
  • @param prefix 缓存名前缀
  • @return *FileCacheClient, error
  • @example:
  • 1. cache, err := facade.NewFileCacheClient("runtime/cache")
  • 2. cache, err := facade.NewFileCacheClient("runtime/cache", "cache_")

func (*FileCacheClient) Clear

func (this *FileCacheClient) Clear() (ok bool)

Clear 清空缓存

func (*FileCacheClient) ClearE

func (this *FileCacheClient) ClearE() (err error)

ClearE 清空缓存

func (*FileCacheClient) Del

func (this *FileCacheClient) Del(key any) (ok bool)

Del 从缓存中删除key对应的数据

func (*FileCacheClient) DelE

func (this *FileCacheClient) DelE(key any) (err error)

DelE 从缓存中删除key对应的数据

func (*FileCacheClient) DelPrefix

func (this *FileCacheClient) DelPrefix(prefix ...any) (ok bool)

DelPrefix 从缓存中删除指定前缀的数据

func (*FileCacheClient) DelPrefixE

func (this *FileCacheClient) DelPrefixE(prefix ...any) (err error)

DelPrefixE 删除指定前缀的缓存

func (*FileCacheClient) DelTags

func (this *FileCacheClient) DelTags(tags ...any) (ok bool)

DelTags 从缓存中删除指定标签的数据

func (*FileCacheClient) DelTagsE

func (this *FileCacheClient) DelTagsE(tag ...any) (err error)

DelTagsE 删除指定标签的缓存

func (*FileCacheClient) Get

func (this *FileCacheClient) Get(key any) (result []byte)

Get 从缓存中获取key对应的数据

func (*FileCacheClient) GetInfo

func (this *FileCacheClient) GetInfo(key any) (info map[string]any)

GetInfo 获取缓存信息

func (*FileCacheClient) GetKeys

func (this *FileCacheClient) GetKeys() (slice []string)

GetKeys 获取所有缓存的key

func (*FileCacheClient) Has

func (this *FileCacheClient) Has(key any) (exist bool)

Has 检查缓存中是否存在key对应的数据

func (*FileCacheClient) Set

func (this *FileCacheClient) Set(key any, value []byte, expire ...any) (ok bool)

Set 将key-value数据加入到缓存中

func (*FileCacheClient) SetE

func (this *FileCacheClient) SetE(key any, value []byte, expire int64) (err error)

SetE 将key-value数据加入到缓存中

type FileCacheClientItem

type FileCacheClientItem struct {
	// contains filtered or unexported fields
}

type FileClass

type FileClass struct {
	Fs afero.Fs
}
var FileV2 *FileClass

func (*FileClass) AutoExtract

func (this *FileClass) AutoExtract(sourceURL, dest string) error

AutoExtract - 自动解压函数,支持本地和网络压缩包

func (*FileClass) Chmod

func (this *FileClass) Chmod(dest string, mode int64)

Chmod - 修改文件或目录权限

func (*FileClass) Delete

func (this *FileClass) Delete(dest string) error

Delete - 删除文件或目录

func (*FileClass) Download

func (this *FileClass) Download(url string) (fileName string, body io.ReadCloser, length int64, err error)

Download - 下载网络压缩包

func (*FileClass) Exist

func (this *FileClass) Exist(dest string) (ok bool)

Exist 检查文件或目录是否存在

func (*FileClass) List

func (this *FileClass) List(dest string) (files []FileInfo, err error)

List - 查询目录下的目录和文件

func (*FileClass) Read

func (this *FileClass) Read(dest string) (content []byte, err error)

Read - 读取文件内容

func (*FileClass) Watcher

func (this *FileClass) Watcher(filePath string, callback func(event fsnotify.Event)) (*FileWatcher, error)

Watcher 创建文件监听器

func (*FileClass) Write

func (this *FileClass) Write(dest string, content []byte) (err error)

CreateFile - 创建文件并写入内容

func (*FileClass) WriteFile

func (this *FileClass) WriteFile(dest string, fileInfo archives.FileInfo) error

WriteFile - 写入文件内容

type FileInfo

type FileInfo struct {
	Path    string      `json:"path"`
	Name    string      `json:"name"`
	Size    int64       `json:"size"`
	Mode    fs.FileMode `json:"mode"`
	ModTime time.Time   `json:"modTime"`
	IsDir   bool        `json:"isDir"`
	Sys     any         `json:"sys"`
}

FileInfo - 文件信息结构体

type FileRequest

type FileRequest struct {
	// 文件名
	Name string
	// 文件路径(包含文件名)
	Path string
	// 目录路径(不包含文件名)
	Dir string
	// 文件后缀
	Ext string
	// 限制行数
	Limit int
	// 读取偏移量
	Page int
	// 返回结果格式
	Format string
	// 是否包含子目录
	Sub bool
	// 域名 - 用于拼接文件路径
	Domain string
	// 前缀 - 用于过滤前缀
	Prefix string
}

FileRequest - File 请求

type FileResponse

type FileResponse struct {
	Error  error
	Result any
	Text   string
	Byte   []byte
	Slice  []any
}

FileResponse - File 响应

type FileStruct

type FileStruct struct {
	// contains filtered or unexported fields
}

FileStruct - File 结构体

func File

func File(request ...FileRequest) *FileStruct

File - 文件系统

func (*FileStruct) Byte

func (this *FileStruct) Byte(path ...any) (result *FileResponse)

Byte 获取文件字节

func (*FileStruct) Dir

func (this *FileStruct) Dir(dir any) *FileStruct

Dir 设置目录路径(不包含文件名,如:/tmp)

func (*FileStruct) DirInfo

func (this *FileStruct) DirInfo(dir ...any) (result *FileResponse)

DirInfo 获取目录信息

func (*FileStruct) Domain

func (this *FileStruct) Domain(domain any) *FileStruct

Domain 设置域名(用于拼接文件路径)

func (*FileStruct) Download

func (this *FileStruct) Download(path ...any) (result *FileResponse)

Download 下载文件 *

  • @param path1 远程文件路径(下载地址)
  • @param path2 本地文件路径(保存路径,包含文件名)
  • @return *FileResponse
  • @example:
  • 1. item := utils.File().Download("https://inis.cn/name.zip", "public/test.zip")
  • 2. item := utils.File().Dir("public").Name("test.zip").Download("https://inis.cn/name.zip")
  • 3. item := utils.File(utils.FileRequest{ Path: "https://inis.cn/name.zip", Name: "test.zip", Dir: "public", }).Download()

func (*FileStruct) EnZip

func (this *FileStruct) EnZip() (result *FileResponse)

EnZip 压缩文件 *

  • @return *FileResponse
  • @example:
  • 1. item := utils.File().Dir("public").Name("name.zip").EnZip()
  • 2. item := utils.File().Dir("public").Path("public/name.zip").EnZip()
  • 3. item := utils.File(utils.FileRequest{ Path: "public/name.zip", Dir: "public", }).EnZip()

func (*FileStruct) Exist

func (this *FileStruct) Exist(path ...any) (ok bool)

Exist 判断文件是否存在

func (*FileStruct) Ext

func (this *FileStruct) Ext(ext any) *FileStruct

Ext 设置文件后缀(如:.txt)

func (*FileStruct) Limit

func (this *FileStruct) Limit(limit any) *FileStruct

Limit 设置限制行数

func (*FileStruct) Line

func (this *FileStruct) Line(path ...any) (result *FileResponse)

Line 按行读取文件

func (*FileStruct) List

func (this *FileStruct) List(path ...any) (result *FileResponse)

List 获取指定目录下的所有文件

func (*FileStruct) Name

func (this *FileStruct) Name(name any) *FileStruct

Name 设置文件名(不包含路径,如:test.txt)

func (*FileStruct) Page

func (this *FileStruct) Page(page any) *FileStruct

Page 设置读取偏移量

func (*FileStruct) Path

func (this *FileStruct) Path(path any) *FileStruct

Path 设置文件路径(包含文件名,如:/tmp/test.txt)

func (*FileStruct) Prefix

func (this *FileStruct) Prefix(prefix any) *FileStruct

Prefix 设置前缀(用于过滤前缀)

func (*FileStruct) Remove

func (this *FileStruct) Remove(path ...any) (result *FileResponse)

Remove 删除文件或目录

func (*FileStruct) Rename

func (this *FileStruct) Rename(path ...any) (result *FileResponse)

Rename 重命名文件

func (*FileStruct) Save

func (this *FileStruct) Save(reader io.Reader, path ...string) (result *FileResponse)

Save 保存文件

func (*FileStruct) UnZip

func (this *FileStruct) UnZip() (result *FileResponse)

UnZip 解压文件 *

  • @return *FileResponse
  • @example:
  • 1. item := utils.File().Dir("public").Name("name.zip").UnZip()
  • 2. item := utils.File().Dir("public").Path("public/name.zip").UnZip()
  • 3. item := utils.File(utils.FileRequest{ Path: "public/name.zip", Dir: "public", }).UnZip()

type FileWatcher

type FileWatcher struct {
	// contains filtered or unexported fields
}

FileWatcher 文件监听器

func (*FileWatcher) Start

func (this *FileWatcher) Start()

Start 开始监听文件变更

func (*FileWatcher) Stop

func (this *FileWatcher) Stop()

Stop 停止监听

type FormatClass

type FormatClass struct{}
var Format *FormatClass

func (*FormatClass) Query

func (this *FormatClass) Query(data any) (result string)

Query 转 Query 格式

type GenClass

type GenClass struct{}
var Gen *GenClass

Gen - 生成

func (*GenClass) BatchNo added in v1.9.9

func (this *GenClass) BatchNo(prefix string, value any) string

BatchNo - 批次号

func (*GenClass) Domain

func (this *GenClass) Domain() string

Domain 生成随机域名

func (*GenClass) IP

func (this *GenClass) IP() string

IP 生成随机公网IP地址 排除内网、保留地址等非公网IP范围

func (*GenClass) SerialDate

func (this *GenClass) SerialDate(prefix any, length int) string

SerialDate 生成年月日 + (当天累计毫秒数)的字符串

  • prefix: 前缀字符串
  • length: 结果总长度(包括前缀),如果结果长度超过指定长度,会被截断;如果结果长度不足指定长度,会在累计毫秒数前面补0
  • 格式: 前缀 + 年月日(8位) + 当天累计毫秒数(8位,固定长度,不足部分前面补0)
  • 建议:除去前缀部分,长度至少为16位(8位日期 + 8位累计毫秒数),否则可能会导致结果不唯一

func (*GenClass) SerialNo

func (this *GenClass) SerialNo(prefix any, length int) string

SerialNo 生成指定前缀和长度的序列号 *

  • prefix: 前缀字符串
  • len: 序列号总长度
  • 格式: 前缀 + 日期(8位) + 时间(6位) + 随机数(动态长度)
  • 当指定长度小于前缀+日期+时间的长度时,会截断到指定长度

func (*GenClass) SnowFlakeID

func (this *GenClass) SnowFlakeID(node int64) string

SnowFlakeID 生成雪花ID字符串

func (*GenClass) UA

func (this *GenClass) UA() string

UA 生成随机用户代理字符串

func (*GenClass) UUID

func (this *GenClass) UUID() string

UUID 生成UUID字符串

type GetClass

type GetClass struct{}
var Get *GetClass

Get - 获取

func (*GetClass) CharLength

func (this *GetClass) CharLength(str string) (length int)

CharLength - 获取字符串字符长度(支持中文)

func (*GetClass) Env

func (this *GetClass) Env() (config *any)

Env - 获取环境变量

func (*GetClass) HostProtocol

func (this *GetClass) HostProtocol(url string) (isTLS bool, host string)

HostProtocol 使用正则表达式检查地址是否为TLS并返回去除协议头后的内容 输入: 地址字符串(可能包含http://, https://或不包含协议头) 返回: (是否为TLS, 去除协议头后的地址, 错误)

func (*GetClass) Ip

func (this *GetClass) Ip(key ...string) (result any)

Ip - 获取客户端IP

func (*GetClass) LastMonthTimestamp

func (this *GetClass) LastMonthTimestamp() (start int64, end int64)

LastMonthTimestamp 获取上月开始和结束时间戳

func (*GetClass) LastWeekTimestamp

func (this *GetClass) LastWeekTimestamp() (start int64, end int64)

LastWeekTimestamp 获取上周开始和结束时间戳

func (*GetClass) Mac

func (this *GetClass) Mac() (result string)

Mac - 获取本机MAC

func (*GetClass) MonthTimestamp

func (this *GetClass) MonthTimestamp() (start int64, end int64)

MonthTimestamp 获取本月开始和结束时间戳

func (*GetClass) Pid

func (this *GetClass) Pid() (result int)

Pid - 获取进程ID

func (*GetClass) Pwd

func (this *GetClass) Pwd() (result string)

Pwd - 获取当前目录

func (*GetClass) TodayTimestamp

func (this *GetClass) TodayTimestamp() (start int64, end int64)

TodayTimestamp - 获取今天开始和结束时间戳

func (*GetClass) Type

func (this *GetClass) Type(value any) (result string)

Type - 获取数据类型

func (*GetClass) WeekTimestamp

func (this *GetClass) WeekTimestamp() (start int64, end int64)

WeekTimestamp 获取本周开始和结束时间戳

func (*GetClass) YearTimestamp

func (this *GetClass) YearTimestamp() (start int64, end int64)

YearTimestamp 获取今年开始和结束时间戳

func (*GetClass) YesterdayTimestamp

func (this *GetClass) YesterdayTimestamp() (start int64, end int64)

YesterdayTimestamp - 获取昨天开始和结束时间戳

type HashClass

type HashClass struct{}
var Hash *HashClass

Hash - 哈希加密

func (*HashClass) Number

func (this *HashClass) Number(length any) (result string)

Number 生成指定长度的随机数 *

  • @param length 长度
  • @return result 随机数
  • @example:
  • 1. number := facade.Hash.Number(6)

func (*HashClass) Sum32

func (this *HashClass) Sum32(text any) (result string)

Sum32 - 哈希加密

func (*HashClass) Token

func (this *HashClass) Token(value any, length int) (result string)

Token 生成指定长度的指纹令牌 *

  • @param value 值
  • @param length 令牌长度,默认长度为 16
  • @example:
  • token := utils.Hash.Token("test", 16)

type IdentifyClass

type IdentifyClass struct{}
var Identify *IdentifyClass

Identify - 识别

func (*IdentifyClass) EmailOrPhone

func (this *IdentifyClass) EmailOrPhone(input string) (mode string, err error)

EmailOrPhone 识别邮箱或手机号

type IsClass

type IsClass struct{}
var Is *IsClass

Is - 是否为

func (*IsClass) Accepted

func (this *IsClass) Accepted(value any) (ok bool)

Accepted - 验证某个字段是否为为 yes, on, 或是 1

func (*IsClass) ActiveUrl

func (this *IsClass) ActiveUrl(value any) (ok bool)

ActiveUrl - 是否为有效的域名或者IP

func (*IsClass) Alpha

func (this *IsClass) Alpha(value any) (ok bool)

Alpha - 只能包含字母

func (*IsClass) AlphaDash

func (this *IsClass) AlphaDash(value any) (ok bool)

AlphaDash - 只能包含字母、数字和下划线_及破折号-

func (*IsClass) AlphaNum

func (this *IsClass) AlphaNum(value any) (ok bool)

AlphaNum - 只能包含字母和数字

func (*IsClass) Array

func (this *IsClass) Array(value any) (ok bool)

Array - 是否为数组

func (*IsClass) Bool

func (this *IsClass) Bool(value any) (ok bool)

Bool - 是否为bool

func (*IsClass) Chs

func (this *IsClass) Chs(value any) (ok bool)

Chs - 是否为汉字

func (*IsClass) ChsAlpha

func (this *IsClass) ChsAlpha(value any) (ok bool)

ChsAlpha - 只能是汉字、字母

func (*IsClass) ChsAlphaNum

func (this *IsClass) ChsAlphaNum(value any) (ok bool)

ChsAlphaNum - 只能是汉字、字母和数字

func (*IsClass) ChsDash

func (this *IsClass) ChsDash(value any) (ok bool)

ChsDash - 只能是汉字、字母、数字和下划线_及破折号-

func (*IsClass) Cntrl

func (this *IsClass) Cntrl(value any) (ok bool)

Cntrl - 是否为控制字符 - (换行、缩进、空格)

func (*IsClass) Date

func (this *IsClass) Date(date any) (ok bool)

Date - 是否为日期类型

func (*IsClass) Domain

func (this *IsClass) Domain(domain any) (ok bool)

Domain - 是否为域名

func (*IsClass) Email

func (this *IsClass) Email(email any) (ok bool)

Email - 是否为邮箱

func (*IsClass) Empty

func (this *IsClass) Empty(args ...any) (ok bool)

Empty - 是否为空 - 全部为空才返回 true

func (*IsClass) False

func (this *IsClass) False(value any) (ok bool)

False - 是否为假

func (*IsClass) Float

func (this *IsClass) Float(value any) (ok bool)

Float - 是否为浮点数

func (*IsClass) Graph

func (this *IsClass) Graph(value any) (ok bool)

Graph - 是否为可见字符 - (除空格外的所有可打印字符)

func (*IsClass) IdCard

func (this *IsClass) IdCard(value any) (ok bool)

IdCard - 是否为有效的身份证号码

func (*IsClass) Ip

func (this *IsClass) Ip(ip any) (ok bool)

Ip - 是否为IP

func (*IsClass) JsonString

func (this *IsClass) JsonString(value any) (ok bool)

JsonString - 是否为json字符串

func (*IsClass) Lower

func (this *IsClass) Lower(value any) (ok bool)

Lower - 是否为小写字母

func (*IsClass) MacAddr

func (this *IsClass) MacAddr(value any) (ok bool)

MacAddr - 是否为有效的MAC地址

func (*IsClass) Map

func (this *IsClass) Map(value any) (ok bool)

Map - 是否为map

func (*IsClass) MapAny

func (this *IsClass) MapAny(value any) (ok bool)

MapAny - 是否为[]map[string]any

func (*IsClass) Mobile

func (this *IsClass) Mobile(value any) (ok bool)

Mobile - 是否为手机号

func (*IsClass) Null

func (this *IsClass) Null(value any) (ok bool)

Null - 是否为 nil

func (*IsClass) Number

func (this *IsClass) Number(value any) (ok bool)

Number - 是否为数字

func (*IsClass) Phone

func (this *IsClass) Phone(phone any) (ok bool)

Phone - 是否为手机号

func (*IsClass) Slice

func (this *IsClass) Slice(value any) (ok bool)

Slice - 是否为切片

func (*IsClass) SliceSlice

func (this *IsClass) SliceSlice(value any) (ok bool)

SliceSlice - 是否为二维切片

func (*IsClass) Space

func (this *IsClass) Space(value any) (ok bool)

Space - 是否为空白字符 - (空格、制表符、换页符等)

func (*IsClass) String

func (this *IsClass) String(value any) (ok bool)

String - 是否为字符串

func (*IsClass) True

func (this *IsClass) True(value any) (ok bool)

True - 是否为真

func (*IsClass) Upper

func (this *IsClass) Upper(value any) (ok bool)

Upper - 是否为大写字母

func (*IsClass) Url

func (this *IsClass) Url(url any) (ok bool)

Url - 是否为URL

func (*IsClass) Xdigit

func (this *IsClass) Xdigit(value any) (ok bool)

Xdigit - 是否为十六进制字符 - (0-9、a-f、A-F)

func (*IsClass) Zip

func (this *IsClass) Zip(value any) (ok bool)

Zip - 是否为有效的邮政编码

type JsonClass

type JsonClass struct{}
var Json *JsonClass

Json - JSON 处理

func (*JsonClass) Decode

func (this *JsonClass) Decode(data any) (result any)

Decode 解码

func (*JsonClass) Encode

func (this *JsonClass) Encode(data any) (result string)

Encode 编码

func (*JsonClass) Get

func (this *JsonClass) Get(jsonString any, key any) (result any, err error)

Get 获取json中的值 - 支持多级

func (*JsonClass) String

func (this *JsonClass) String(data any) (result string)

String map转json字符串

func (*JsonClass) Unmarshal

func (this *JsonClass) Unmarshal(data []byte, result any) (err error)

Unmarshal 解码

func (*JsonClass) Valid

func (this *JsonClass) Valid(data any) (result bool)

Valid - 验证JSON数据

type JwtClass

type JwtClass struct {
	// 参数
	Body dto.JwtBody `json:"body"`
	// 目标
	Dest any `json:"dest"`
	// 记录配置 Hash 值,用于检测配置文件是否有变化
	Hash string `json:"hash"`
}

func (*JwtClass) Decode

func (this *JwtClass) Decode(token string) (resp dto.JwtResp, err error)

Decode - 解析JWT

func (*JwtClass) Encode

func (this *JwtClass) Encode(data any) (resp dto.JwtResp, err error)

Encode - 创建JWT

func (*JwtClass) GetExpired

func (this *JwtClass) GetExpired() time.Time

GetExpired - 获取JWT过期时间

func (*JwtClass) GetHash

func (this *JwtClass) GetHash() string

GetHash - 获取Hash值

func (*JwtClass) SetBody

func (this *JwtClass) SetBody(body dto.JwtBody) *JwtClass

SetBody -

func (*JwtClass) SetHash

func (this *JwtClass) SetHash(hash string) *JwtClass

SetHash - 设置Hash值

func (*JwtClass) Unmarshal

func (this *JwtClass) Unmarshal(dest any) *JwtClass

Unmarshal - 解析JWT

type LangClass

type LangClass struct {
	Directory string // 语言包目录
	Lang      string // 当前语言
	Mode      string // 文件类型
}
var Lang *LangClass

func (*LangClass) AcceptLanguage

func (this *LangClass) AcceptLanguage(value any) (string, []Language, error)

AcceptLanguage - 解析请求头的 Accept-Language

func (*LangClass) New

func (this *LangClass) New(model ...LangClass) *LangClass

New 实例化

func (*LangClass) Value

func (this *LangClass) Value(key any, args ...any) (result any)

type Language

type Language struct {
	Language string
	Quality  float64
}

type MaskClass

type MaskClass struct{}
var Mask *MaskClass

Mask - 脱敏

func (*MaskClass) BankCard

func (this *MaskClass) BankCard(bankCard string) string

BankCard - 银行卡号脱敏

func (*MaskClass) Custom

func (this *MaskClass) Custom(str string, start, end int) string

Custom - 自定义脱敏

func (*MaskClass) Email

func (this *MaskClass) Email(email string) string

Email - 邮箱脱敏

func (*MaskClass) IDCard

func (this *MaskClass) IDCard(idCard string) string

IDCard - 身份证号脱敏

func (*MaskClass) Password

func (this *MaskClass) Password(password string) string

Password - 密码脱敏

func (*MaskClass) Phone

func (this *MaskClass) Phone(phone string) string

Phone - 手机号脱敏

type Md5Class

type Md5Class struct{}
var Md5 *Md5Class

Md5 - MD5 加密

func (*Md5Class) Encrypt

func (this *Md5Class) Encrypt(value string) string

Encrypt 计算字符串的 MD5 哈希值(返回十六进制字符串)

type MimeClass

type MimeClass struct{}
var Mime *MimeClass

func (*MimeClass) Type

func (this *MimeClass) Type(suffix any) (mime string)

Type 获取后缀对应的 mime

type NetClass

type NetClass struct{}
var Net *NetClass

Net - 网络

func (*NetClass) Tcping

func (this *NetClass) Tcping(host any, opts ...map[string]any) (ok bool, detail []map[string]any)

type ParseClass

type ParseClass struct{}
var Parse *ParseClass

Parse - 解析

func (*ParseClass) Domain

func (this *ParseClass) Domain(value any) (domain string)

Domain - 解析域名

func (*ParseClass) HtmlToText

func (this *ParseClass) HtmlToText(html string, length int, isLine bool) (text string)

HtmlToText - 去除 HTML 标签、解码实体、压缩空白,并按 Unicode 字符截取前 length 个字符

func (*ParseClass) HtmlToTextRetainImg

func (this *ParseClass) HtmlToTextRetainImg(html string, length int, isLine bool) (text string)

HtmlToTextRetainImg - 去除 HTML 标签、解码实体、压缩空白,保留 img 标签并添加 emoji 类,并按 Unicode 字符截取前 length 个字符

func (*ParseClass) Params

func (this *ParseClass) Params(params map[string]any) (result map[string]any)

Params - 解析参数 把 Content-Type = application/x-www-form-urlencoded 的参数解析成 map[string]any

func (*ParseClass) ParamsBefore

func (this *ParseClass) ParamsBefore(params url.Values) (result map[string]any)

ParamsBefore - 解析参数 把 Content-Type = application/x-www-form-urlencoded 的参数解析成 object.deep.age = 10 的格式

func (*ParseClass) ProcessImgTags

func (this *ParseClass) ProcessImgTags(content string) string

ProcessImgTags - 处理 img 标签,添加或追加 emoji 类

type PasswordStruct

type PasswordStruct struct{}
var Password *PasswordStruct

Password - 密码

func (*PasswordStruct) Create

func (this *PasswordStruct) Create(password any) (result string)

Create - 创建密码

func (*PasswordStruct) Verify

func (this *PasswordStruct) Verify(encode any, password any) (ok bool)

Verify - 验证密码

type ProgressReader

type ProgressReader struct {
	Reader     io.Reader
	Total      int64
	Reading    int64
	OnProgress func(read int64)
	LastUpdate time.Time
}

ProgressReader 进度读取器,用于显示下载进度

func (*ProgressReader) Read

func (this *ProgressReader) Read(b []byte) (n int, err error)

type RSAClass

type RSAClass struct{}
var RSA *RSAClass

func (*RSAClass) Decrypt

func (this *RSAClass) Decrypt(privateKey, text string) (result *RSAResponse)

Decrypt 解密

func (*RSAClass) Encrypt

func (this *RSAClass) Encrypt(publicKey, text string) (result *RSAResponse)

Encrypt 加密

func (*RSAClass) Generate

func (this *RSAClass) Generate(bits any) (result *RSAResponse)

Generate 生成 RSA 密钥对 *

  • @name Generate 生成 RSA 密钥对
  • @param bits 位数 1024, 2048, 4096(一般:2048)

func (*RSAClass) PrivatePem

func (this *RSAClass) PrivatePem(key string) (cert string)

PrivatePem - 输出完整的 PEM 格式私钥证书

func (*RSAClass) PublicPem

func (this *RSAClass) PublicPem(key string) (cert string)

PublicPem - 输出完整的 PEM 格式公钥证书

type RSAResponse

type RSAResponse struct {
	// 私钥
	PrivateKey string
	// 公钥
	PublicKey string
	// 错误信息
	Error error
	// 文本
	Text string
}

type RandClass

type RandClass struct{}
var Rand *RandClass

Rand - 随机数

func (*RandClass) Code

func (this *RandClass) Code(length any, mode ...string) (result string)

Code - 生成随机验证码 number:数字, letter:字母, mix:混合

func (*RandClass) Int

func (this *RandClass) Int(max any, min ...any) (result int)

Int - 生成随机整数

func (*RandClass) MapSlice

func (this *RandClass) MapSlice(slice []map[string]any) (result []map[string]any)

MapSlice - 打乱切片顺序

func (*RandClass) Number

func (this *RandClass) Number(length any) (result string)

Number - 生成指定长度的随机数

func (*RandClass) Slice

func (this *RandClass) Slice(slice []any, limit any) (result []any)

Slice - 返回随机的指定长度的切片

func (*RandClass) String

func (this *RandClass) String(length any, chars ...string) (result string)

String - 生成随机字符串

type Resp

type Resp struct {
	// 消息
	Msg string `json:"msg"`
	// 状态码
	Code int `json:"code"`
	// 数据
	Data any `json:"data"`
}

Resp - 返回数据结构

type StructClass

type StructClass struct{}
var Struct *StructClass

Struct - 操作结构体

func (*StructClass) Defaults

func (this *StructClass) Defaults(dest any) error

Defaults 可以处理任意结构体指针,为所有带有 default 标签的字段设置默认值

func (*StructClass) Del

func (this *StructClass) Del(obj any, key string)

Del - 删除结构体的字段

func (*StructClass) Fields

func (this *StructClass) Fields(dest any) []string

Fields - 获取结构体的字段名

func (*StructClass) Get

func (this *StructClass) Get(obj any, key string) (result any)

Get - 动态获取结构体的值

func (*StructClass) Has

func (this *StructClass) Has(obj any, key string) (ok bool)

Has - 判断结构体是否存在某个字段

func (*StructClass) Keys

func (this *StructClass) Keys(obj any) (slice []string)

Keys - 获取结构体的字段

func (*StructClass) Len

func (this *StructClass) Len(obj any) (length int)

Len - 获取结构体的长度

func (*StructClass) Map

func (this *StructClass) Map(obj any) (result map[string]any)

Map - 将结构体转换为map

func (*StructClass) Set

func (this *StructClass) Set(obj any, key string, val any)

Set - 动态给结构体赋值

func (*StructClass) Slice

func (this *StructClass) Slice(obj any) (slice []any)

Slice - 将结构体转换为切片

func (*StructClass) ToAsciiString

func (this *StructClass) ToAsciiString(obj any) (result string)

ToAsciiString - 将结构体转换为ASCII字符串

func (*StructClass) ToStringMap

func (this *StructClass) ToStringMap(obj any) (result map[string]any)

ToStringMap - 将结构体转换为map[string]any

func (*StructClass) Values

func (this *StructClass) Values(obj any) (slice []any)

Values - 获取结构体的值

type UnityClass

type UnityClass struct{}
var Unity *UnityClass

Unity - 统一规范化

func (*UnityClass) Float

func (this *UnityClass) Float(value ...any) (array []float64)

Float 参数归一化

func (*UnityClass) Ids

func (this *UnityClass) Ids(param ...any) (ids []any)

Ids 参数归一化

func (*UnityClass) Int

func (this *UnityClass) Int(value ...any) (array []int)

Int 参数归一化

func (*UnityClass) Int64

func (this *UnityClass) Int64(value ...any) (array []int64)

Int64 参数归一化

func (*UnityClass) Join

func (this *UnityClass) Join(elems any, unique bool, sep ...string) string

Join - 数组转字符串

func (*UnityClass) Keys

func (this *UnityClass) Keys(param any, reg ...any) (keys []any)

Keys 参数归一化

type UrlClass

type UrlClass struct {
}

func (*UrlClass) Encoded

func (this *UrlClass) Encoded(params map[string]any) string

Encoded - 将 map 编码为 URL 查询字符串 - x-www-form-urlencoded

func (*UrlClass) EncodedKeys

func (this *UrlClass) EncodedKeys(params map[string]any) []string

EncodedKeys - 获取 URL 查询字符串的键

type VersionClass

type VersionClass struct{}
var Version *VersionClass

Version - 版本

func (*VersionClass) Compare

func (this *VersionClass) Compare(v1, v2 any) (result int)

Compare - 版本号比对 *

  • @param v1 string - 小版本号
  • @param v2 string - 大版本号
  • @return int - 0: 相等,1: v1 < v2,-1: v1 > v2
  • @example:
  • utils.Version.Compare("1.2.0", "1.0.0") // 1

func (*VersionClass) Go

func (this *VersionClass) Go() (result string)

Go - 获取当前go版本号

type ViperClass

type ViperClass struct {
	// 配置文件路径
	Path string
	// 配置文件类型
	Mode string
	// 文件名
	Name string
	// 文件内容
	Content string
}

func Viper

func Viper(model ...ViperClass) *ViperClass

func (*ViperClass) Read

func (this *ViperClass) Read() (result ViperResponse)

func (*ViperClass) SetMode

func (this *ViperClass) SetMode(mode string) *ViperClass

func (*ViperClass) SetName

func (this *ViperClass) SetName(name string) *ViperClass

func (*ViperClass) SetPath

func (this *ViperClass) SetPath(path string) *ViperClass

type ViperResponse

type ViperResponse struct {
	// 配置文件内容
	Result map[string]any
	// 错误信息
	Error error
	// viper实例
	Viper *viper.Viper
}

func (*ViperResponse) Get

func (this *ViperResponse) Get(key string, def ...any) (result any)

func (*ViperResponse) Set

func (this *ViperResponse) Set(key string, value any) (result ViperResponse)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL