cutego/pkg/util/token_util.go

59 lines
1.2 KiB
Go
Raw Normal View History

2022-03-01 13:50:13 +08:00
package util
import (
2023-01-18 15:40:27 +08:00
"cutego/modules/core/api/v1/response"
2023-01-18 17:21:12 +08:00
"cutego/modules/core/dataobject"
2022-03-01 13:50:13 +08:00
"cutego/pkg/common"
"cutego/pkg/config"
"cutego/pkg/jwt"
2023-01-18 17:09:49 +08:00
"cutego/refs"
2022-03-01 13:50:13 +08:00
"github.com/gin-gonic/gin"
"strings"
)
type UserUtils struct {
}
// GetUserInfo 通过jwt获取当前登录用户
func GetUserInfo(c *gin.Context) *response.UserResponse {
token := c.Request.Header.Get("Authorization")
s := strings.Split(token, " ")
// parseToken 解析token包含的信息
claims, err := jwt.ParseToken(s[1])
if err != nil {
common.ErrorLog(err)
}
info := claims.UserInfo
return &info
}
// CheckLockToken 校验多终端登录锁
func CheckLockToken(c *gin.Context) bool {
if config.AppEnvConfig.Login.Single {
// 获取redis中的token数据
info := GetUserInfo(c)
2023-01-18 17:09:49 +08:00
get, err := refs.RedisDB.GET(info.UserName)
2022-03-01 13:50:13 +08:00
if err != nil {
common.ErrorLog(err)
return false
}
token := c.Request.Header.Get(config.AppEnvConfig.Jwt.Header)
s := strings.Split(token, " ")
if get == s[1] {
return true
} else {
return false
}
}
return true
}
// CheckIsAdmin 判断是否是超级管理员
2023-01-18 17:21:12 +08:00
func CheckIsAdmin(user *dataobject.SysUser) bool {
2022-03-01 13:50:13 +08:00
if user.UserId == 1 {
return true
} else {
return false
}
}