feat: role.

This commit is contained in:
2026-04-28 18:58:24 +08:00
parent 9a95130488
commit aa90b10f06
5 changed files with 155 additions and 20 deletions
+12 -5
View File
@@ -2,6 +2,7 @@ package util
import (
"errors"
"hr_receiver/models"
"time"
"github.com/golang-jwt/jwt/v5"
@@ -9,19 +10,25 @@ import (
var ApiSecret = "your-super-secret-key" // 预共享密钥
type Claims struct {
UserID uint `json:"user_id"`
Username string `json:"username"`
UserID uint `json:"user_id"`
Username string `json:"username"`
Role models.UserRole `json:"role"`
FlavorType models.UserFlavorType `json:"flavorType"`
RegionIDs []uint32 `json:"regionIds"`
jwt.RegisteredClaims
}
// GenerateToken 生成JWT Token
func GenerateToken(userID uint, username string) (string, error) {
func GenerateToken(user *models.User) (string, error) {
expirationTime := time.Now().Add(24 * 30 * time.Hour) // Token有效期24小时
//expirationTime := time.Now().Add(1 * time.Second) // Token有效期24小时
claims := &Claims{
UserID: userID,
Username: username,
UserID: user.ID,
Username: user.Username,
Role: user.Role,
FlavorType: user.FlavorType,
RegionIDs: user.RegionIDs(),
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(expirationTime),
IssuedAt: jwt.NewNumericDate(time.Now()),