utils

package
v0.0.0-...-28725db Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: MIT Imports: 39 Imported by: 0

Documentation

Index

Constants

View Source
const CASBIN_UTIL_ERR_PREFIX = "utils/casbin.go ->"
View Source
const JSON_UTIL_ERR_PREFIX = "utils/json.go ->"
View Source
const REDIS_UTIL_ERR_PREFIX = "utils/redis.go ->"

Variables

View Source
var (
	ErrTokenExpired     = errors.New("token 已过期, 请重新登录")
	ErrTokenNotValidYet = errors.New("token 无效, 请重新登录")
	ErrTokenMalformed   = errors.New("token 不正确, 请重新登录")
	ErrTokenInvalid     = errors.New("这不是一个 token, 请重新登录")
)

定义 token 相关 error

View Source
var Casbin = new(_casbin)
View Source
var Encryptor = new(_encrypt)
View Source
var IP = new(ipUtil)
View Source
var Json = new(_json)
View Source
var Logger *zap.Logger
View Source
var Redis = new(_redis)

对 Redis 库的操作二次封装,统一处理错误

View Source
var Validator = new(ValidateUtil)

Functions

func BindJson

func BindJson[T any](c *gin.Context) (data T)

json bind

func BindValidJson

func BindValidJson[T any](c *gin.Context) (data T)

JSON 绑定验证

func CopyProperties

func CopyProperties[T any](from any) (to T)

拷贝属性 vo->po

func InitCasbin

func InitCasbin(db *gorm.DB) *casbin.CachedEnforcer

func InitLogger

func InitLogger()

func InitMySQLDB

func InitMySQLDB() *gorm.DB

func InitRedis

func InitRedis() *redis.Client

func InitViper

func InitViper()

func MakeMigrate

func MakeMigrate(db *gorm.DB)

迁移数据表,在没有数据表结构变更时候,建议注释不执行 只支持创建表、增加表中没有的字段和索引 为了保护数据,并不支持改变已有的字段类型或删除未被使用的字段

func PathExists

func PathExists(path string) (bool, error)

判断文件目录是否存在

func Validate

func Validate(c *gin.Context, data any)

Types

type MyClaims

type MyClaims struct {
	UserId int    `json:"user_id"`
	Role   string `json:"role"`
	UUID   string `json:"uuid"`
	jwt.RegisteredClaims
}

定义 JWT 中存储的信息

type MyJWT

type MyJWT struct {
	Secret []byte
}

func GetJWT

func GetJWT() *MyJWT

JWT 工具类

func (*MyJWT) GenToken

func (j *MyJWT) GenToken(userId int, role string, uuid string) (string, error)

生成 JWT

func (*MyJWT) ParseToken

func (j *MyJWT) ParseToken(tokenString string) (*MyClaims, error)

解析 JWT

type ValidateUtil

type ValidateUtil struct{}

func (*ValidateUtil) Validate

func (v *ValidateUtil) Validate(data any) string

返回验证错误信息, 为 "" 则无错误

Directories

Path Synopsis
r

Jump to

Keyboard shortcuts

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