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))
}