jwt.go 683 字节
package tool

import (
	jwt "github.com/golang-jwt/jwt/v4"
	"time"
)

type UserToken struct {
	UserId     int64  `json:"userId"`
	AdminId    int64  `json:"adminId"`
	CompanyId  int64  `json:"companyId"`
	ClientType string `json:"clientType"`
}

func (tk UserToken) GenerateToken(secret string, expire int64) (string, error) {
	claims := make(jwt.MapClaims)
	claims["exp"] = time.Now().Unix() + expire
	claims["iat"] = time.Now().Unix()
	claims["UserId"] = tk.UserId
	claims["AdminId"] = tk.AdminId
	claims["CompanyId"] = tk.CompanyId
	claims["ClientType"] = tk.ClientType
	token := jwt.New(jwt.SigningMethodHS256)
	token.Claims = claims

	return token.SignedString([]byte(secret))
}