utils

package
v0.0.0-...-6daead3 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SuccessCode            = 200
	SuccessMessage         = "success"
	ErrorCode              = 0
	ErrorAuthorizationCode = 401
	ErrorMessage           = "fail"
)

Variables

View Source
var TokenExpired = errors.New("token is expired")

Functions

func ComparePassword

func ComparePassword(hashPassword, password string) error

ComparePassword 密码校验

func Fail

func Fail(statusCode int, message string, c *gin.Context)

Fail 失败, 返回失败信息

func FailAuthorization

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

func FailDefault

func FailDefault(c *gin.Context)

FailDefault Fail 失败, 无返回数据

func FailWithMessage

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

FailWithMessage 失败, 返回失败信息

func FormatJwtExpiresTime

func FormatJwtExpiresTime(dur int) *jwt.NumericDate

func HashPassword

func HashPassword(password string) (string, error)

HashPassword 密码加密

func Ok

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

Ok 成功, 有返回数据

func OkDefault

func OkDefault(c *gin.Context)

OkDefault 成功, 无返回数据

func Resp

func Resp(statusCode int, data any, message string, c *gin.Context)

Types

type JwtCustomClaims

type JwtCustomClaims struct {
	UserId   string `json:"userId"`
	Username string `json:"username"`
	jwt.RegisteredClaims
}

type JwtSign

type JwtSign struct {
	SingingKey []byte
}

func New

func New() *JwtSign

New 创建JwtSign实例

func (*JwtSign) CreateClaims

func (*JwtSign) CreateClaims(userId string, username string) *JwtCustomClaims

CreateClaims 创建claims

func (*JwtSign) CreateToken

func (j *JwtSign) CreateToken(userId string, username string) (string, error)

CreateToken 创建Token

func (*JwtSign) ParseToken

func (j *JwtSign) ParseToken(token string) (*JwtCustomClaims, error)

ParseToken 解析(验证)Token

func (*JwtSign) RefreshToken

func (j *JwtSign) RefreshToken(token string) (userId string, nToken string, err error)

RefreshToken 更新token

type Response

type Response struct {
	StatusCode int    `json:"statusCode"`
	Data       any    `json:"data"`
	Message    string `json:"message"`
}

Response 定义系统返回报文格式

Jump to

Keyboard shortcuts

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