util

package
v1.3.11 Latest Latest
Warning

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

Go to latest
Published: May 9, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base64Decode

func Base64Decode(encoded string) ([]byte, error)

Base64Decode Base64解码

func Base64Encode

func Base64Encode(data []byte) string

Base64Encode Base64编码

func Decrypt

func Decrypt(signKey string, ciphertext string) (string, error)

Decrypt 使用 SignKey 解密字符串数据 signKey: 64 字符 hex 编码的密钥(对应 32 字节,用于 AES-256) ciphertext: base64 编码的密文 return: 解密后的明文字符串

func DerefString

func DerefString(s *string) string

DerefString 安全地解引用字符串指针,nil 返回空字符串

func Ed25519Verify

func Ed25519Verify(publicKey, message, signature []byte) bool

Ed25519Verify 验证 Ed25519 签名 publicKey: 32 字节的公钥(已解码的二进制格式) message: 待验证的原始消息 signature: 64 字节的签名(已解码的二进制格式) return: 签名是否有效

func Encrypt

func Encrypt(signKey string, plaintext string) (string, error)

Encrypt 使用 SignKey 加密字符串数据 signKey: 64 字符 hex 编码的密钥(对应 32 字节,用于 AES-256) plaintext: 要加密的明文字符串 return: base64 编码的密文

func GenerateUniqueIDSimple

func GenerateUniqueIDSimple() string

GenerateUniqueIDSimple 生成 64 位唯一标识符

func GetFromContext

func GetFromContext[T any](c *gin.Context, key string) (T, bool)

GetFromContext 从上下文获取指定类型的值

func GetSessionOptions

func GetSessionOptions(maxAge int) sessions.Options

func IsLocalhost

func IsLocalhost(urlStr string) bool

IsLocalhost 检查 URL 是否为 localhost

func Request

func Request(ctx context.Context, method, url string, body io.Reader, headers, cookies map[string]string) (*http.Response, error)

func SetToContext

func SetToContext[T any](c *gin.Context, key string, value T)

SetToContext 设置值到上下文

func ValidateAmount

func ValidateAmount(amount decimal.Decimal) error

ValidateAmount 验证金额:必须大于0,且小数位数不超过2位

func ValidateRates

func ValidateRates(rates ...decimal.Decimal) error

ValidateRates 所有 rate 必须在 [0, 1] 范围内,且小数位数不超过2位

Types

type Response

type Response[T any] struct {
	ErrorMsg string `json:"error_msg"`
	Data     T      `json:"data"`
}

Response 通用响应体

func Err

func Err(msg string) Response[any]

Err 构造错误响应

func OK

func OK[T any](data T) Response[T]

OK 构造成功响应

func OKNil

func OKNil() Response[any]

OKNil 构造成功响应(data 为 null)

type ResponseAny

type ResponseAny struct {
	ErrorMsg string      `json:"error_msg" example:""`
	Data     interface{} `json:"data"`
}

ResponseAny 用于 Swagger 文档的响应类型(非泛型) swag 不支持泛型,使用此类型替代 Response[T]

type StringArray

type StringArray []string

StringArray custom type for handling JSON arrays

func (*StringArray) Scan

func (sa *StringArray) Scan(value interface{}) error

func (StringArray) Value

func (sa StringArray) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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