base

package
v0.0.22 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusError   = 1 // 操作失败
	StatusSuccess = 0 // 操作成功
)

定义操作成功和失败的常量状态码

Variables

This section is empty.

Functions

func ApiData

func ApiData(c *gin.Context, code int, data interface{}, message string)

ApiData 通用的返回函数,用于标准化API响应格式

func BadRequest

func BadRequest(c *gin.Context, code int, data interface{}, message string)

BadRequest 参数错误的返回,使用HTTP 400状态码

func BadRequestError

func BadRequestError(c *gin.Context, message string)

BadRequestError 参数错误的失败返回

func BadRequestWithDetails

func BadRequestWithDetails(c *gin.Context, data interface{}, message string)

BadRequestWithDetails 带详细数据和消息的参数错误返回

func Base64Decrypt

func Base64Decrypt(encryptedPassword string) (string, error)

Base64Decrypt 用于解密加密后的密码

func Base64DecryptWithMagic

func Base64DecryptWithMagic(encryptedPassword string) (string, error)

Base64DecryptWithMagic 解密 与加密过程相反的步骤还原原始密码

func Base64Encrypt

func Base64Encrypt(password string) string

Base64Encrypt 用于加密明文密码

func Base64EncryptWithMagic

func Base64EncryptWithMagic(password string) string

Base64EncryptWithMagic 加密 通过先添加特定盐值、反转字符串再进行base64编码

func DecryptBatch

func DecryptBatch(encryptedSecretKeys []string, encryptionKey []byte) ([]string, error)

DecryptBatch 批量解密

func DecryptSecretKey

func DecryptSecretKey(encryptedSecretKey string, encryptionKey []byte) (string, error)

DecryptSecretKey 使用指定密钥解密数据

func DeleteWithId

func DeleteWithId(l *zap.Logger, funcName string, timeout int, url string, paramsMap map[string]string, headerMap map[string]string) ([]byte, error)

func EncryptBatch

func EncryptBatch(secretKeys []string, encryptionKey []byte) ([]string, error)

EncryptBatch 批量加密

func EncryptSecretKey

func EncryptSecretKey(secretKey string, encryptionKey []byte) (string, error)

EncryptSecretKey 使用指定密钥加密数据

func Error

func Error(c *gin.Context)

Error 操作失败的返回

func ErrorWithDetails

func ErrorWithDetails(c *gin.Context, data interface{}, message string)

ErrorWithDetails 带详细数据和消息的操作失败返回

func ErrorWithMessage

func ErrorWithMessage(c *gin.Context, message string)

ErrorWithMessage 带消息的操作失败返回

func Forbidden

func Forbidden(c *gin.Context, data interface{}, message string)

Forbidden 无权限的返回,使用HTTP 403状态码

func ForbiddenError

func ForbiddenError(c *gin.Context, message string)

ForbiddenError 无权限的失败返回

func GenerateKeyFromPassword

func GenerateKeyFromPassword(password string, salt []byte) ([]byte, error)

GenerateKeyFromPassword 从密码生成密钥(使用PBKDF2)

func GenerateRandomKey

func GenerateRandomKey() ([]byte, error)

GenerateRandomKey 生成随机密钥

func GenerateRandomSalt

func GenerateRandomSalt() ([]byte, error)

GenerateRandomSalt 生成随机盐值

func GetAge

func GetAge(creationTime time.Time) string

GetAge 计算资源创建时间到现在的时间差,返回易读格式

func GetClientIP

func GetClientIP(r *http.Request) string

GetClientIP 获取客户端真实IP地址

func GetCustomParamID

func GetCustomParamID(ctx *gin.Context, paramID string) (int, error)

GetCustomParamID 从查询参数中解析自定义字段的 ID,并进行类型转换

func GetLocalIPs

func GetLocalIPs() ([]string, error)

func GetParamCustomName

func GetParamCustomName(ctx *gin.Context, paramName string) (string, error)

GetParamCustomName 从查询参数中解析 Name,并进行类型转换

func GetParamID

func GetParamID(ctx *gin.Context) (int, error)

GetParamID 从查询参数中解析 ID,并进行类型转换

func GetQueryParam

func GetQueryParam[T any](ctx *gin.Context, key string) (T, error)

GetQueryParam 从查询参数中解析指定类型的值

func GetStringParam

func GetStringParam(ctx *gin.Context, key string) (string, error)

func HandleRequest

func HandleRequest(ctx *gin.Context, req interface{}, action func() (interface{}, error))

HandleRequest 用于统一处理请求绑定和响应

func InternalServerError

func InternalServerError(c *gin.Context, code int, data interface{}, message string)

InternalServerError 服务器内部错误的返回,使用HTTP 500状态码

func InternalServerErrorWithDetails

func InternalServerErrorWithDetails(c *gin.Context, data interface{}, message string)

InternalServerErrorWithDetails 带详细数据和消息的服务器内部错误返回

func MapToStringSlice

func MapToStringSlice(inputMap map[string]string) ([]string, error)

MapToStringSlice 将 map 转换为 []string,要求偶数个元素,key和值依次排列

func NewBusinessError

func NewBusinessError(code error, message string) error

NewBusinessError 创建业务错误

func Ping

func Ping(ipAddr string) bool

Ping 检查指定的 IP 地址是否可达

func PostWithJsonString

func PostWithJsonString(l *zap.Logger, funcName string, timeout int, url string, jsonStr string, paramsMap map[string]string, headerMap map[string]string) ([]byte, error)

PostWithJsonString 发送带 JSON 数据的 POST 请求

func SecureZeroMemory

func SecureZeroMemory(data []byte)

SecureZeroMemory 安全清零内存

func SetUserInfoToContext

func SetUserInfoToContext(ctx context.Context, userInfo *UserInfo) context.Context

SetUserInfoToContext 将用户信息设置到context中

func SetUserInfoToGinContext

func SetUserInfoToGinContext(c *gin.Context, userInfo *UserInfo)

SetUserInfoToGinContext 将用户信息设置到gin.Context中

func StringSliceToMap

func StringSliceToMap(inputSlice []string) (map[string]string, error)

StringSliceToMap 将 []string 转换为 map[string]string,要求输入长度为偶数,奇数索引为 key,偶数索引为 value

func Success

func Success(c *gin.Context)

Success 操作成功的返回

func SuccessWithData

func SuccessWithData(c *gin.Context, data interface{})

SuccessWithData 带数据的操作成功返回

func SuccessWithDetails

func SuccessWithDetails(c *gin.Context, data interface{}, message string)

SuccessWithDetails 带详细数据和消息的操作成功返回

func SuccessWithMessage

func SuccessWithMessage(c *gin.Context, message string)

SuccessWithMessage 带消息的操作成功返回

func Unauthorized

func Unauthorized(c *gin.Context, code int, data interface{}, message string)

Unauthorized 未认证的返回,使用HTTP 401状态码

func UnauthorizedErrorWithDetails

func UnauthorizedErrorWithDetails(c *gin.Context, data interface{}, message string)

UnauthorizedErrorWithDetails 带详细数据和消息的未认证返回

func ValidateEncryptedData

func ValidateEncryptedData(encryptedData string) error

ValidateEncryptedData 验证加密数据格式

func ValidateUniqueResource

func ValidateUniqueResource[T any](ctx context.Context, getResourceFunc func(context.Context, interface{}) (T, error), newResource T, id interface{}) error

ValidateUniqueResource 验证是否存在相同的资源

Types

type ApiResponse

type ApiResponse struct {
	Code    int         `json:"code"`    // 状态码,表示业务逻辑的状态,而非HTTP状态码
	Data    interface{} `json:"data"`    // 响应数据
	Message string      `json:"message"` // 反馈信息
}

ApiResponse 通用的API响应结构体

type BusinessError

type BusinessError struct {
	Code    error
	Message string
}

BusinessError 业务错误结构体

func (*BusinessError) Error

func (e *BusinessError) Error() string

type UserInfo

type UserInfo struct {
	UserID    int    `json:"userId"`
	Username  string `json:"username"`
	IP        string `json:"ip"`
	UserAgent string `json:"userAgent"`
}

UserInfo 用户信息结构体

func GetUserInfoFromContext

func GetUserInfoFromContext(ctx context.Context) *UserInfo

GetUserInfoFromContext 从context.Context中获取用户信息

func GetUserInfoFromGinContext

func GetUserInfoFromGinContext(c *gin.Context) *UserInfo

GetUserInfoFromGinContext 从gin.Context中获取用户信息

func GetUserInfoFromHTTPRequest

func GetUserInfoFromHTTPRequest(r *http.Request) *UserInfo

GetUserInfoFromHTTPRequest 从http.Request中获取用户信息

func (*UserInfo) Clone

func (u *UserInfo) Clone() *UserInfo

Clone 创建用户信息的副本

func (*UserInfo) IsEmpty

func (u *UserInfo) IsEmpty() bool

IsEmpty 检查用户信息是否为空

func (*UserInfo) IsValid

func (u *UserInfo) IsValid() bool

IsValid 检查用户信息是否有效

func (*UserInfo) String

func (u *UserInfo) String() string

String 返回用户信息的字符串表示

Jump to

Keyboard shortcuts

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