334 lines
9.0 KiB
Go
334 lines
9.0 KiB
Go
package utils
|
|
|
|
import (
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
|
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
|
|
systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func ClearToken(c *gin.Context) {
|
|
// 增加cookie x-token 向来源的web添加
|
|
host, _, err := net.SplitHostPort(c.Request.Host)
|
|
if err != nil {
|
|
host = c.Request.Host
|
|
}
|
|
|
|
if net.ParseIP(host) != nil {
|
|
c.SetCookie("x-token", "", -1, "/", "", false, false)
|
|
} else {
|
|
c.SetCookie("x-token", "", -1, "/", host, false, false)
|
|
}
|
|
}
|
|
|
|
func SetToken(c *gin.Context, token string, maxAge int) {
|
|
// 增加cookie x-token 向来源的web添加
|
|
host, _, err := net.SplitHostPort(c.Request.Host)
|
|
if err != nil {
|
|
host = c.Request.Host
|
|
}
|
|
|
|
if net.ParseIP(host) != nil {
|
|
c.SetCookie("x-token", token, maxAge, "/", "", false, false)
|
|
} else {
|
|
c.SetCookie("x-token", token, maxAge, "/", host, false, false)
|
|
}
|
|
}
|
|
|
|
func ClearUserToken(c *gin.Context) {
|
|
// 增加cookie user-token 向来源的web添加
|
|
host, _, err := net.SplitHostPort(c.Request.Host)
|
|
if err != nil {
|
|
host = c.Request.Host
|
|
}
|
|
|
|
if net.ParseIP(host) != nil {
|
|
c.SetCookie("user-token", "", -1, "/", "", false, false)
|
|
} else {
|
|
c.SetCookie("user-token", "", -1, "/", host, false, false)
|
|
}
|
|
}
|
|
|
|
func SetUserToken(c *gin.Context, token string, maxAge int) {
|
|
// 增加cookie user-token 向来源的web添加
|
|
host, _, err := net.SplitHostPort(c.Request.Host)
|
|
if err != nil {
|
|
host = c.Request.Host
|
|
}
|
|
|
|
if net.ParseIP(host) != nil {
|
|
c.SetCookie("user-token", token, maxAge, "/", "", false, false)
|
|
} else {
|
|
c.SetCookie("user-token", token, maxAge, "/", host, false, false)
|
|
}
|
|
}
|
|
|
|
func GetUserToken(c *gin.Context) string {
|
|
// 首先尝试从 user-token 头部获取
|
|
token := c.Request.Header.Get("user-token")
|
|
|
|
// 如果没有,尝试从 Authorization Bearer 头部获取
|
|
if token == "" {
|
|
authHeader := c.Request.Header.Get("Authorization")
|
|
if authHeader != "" && len(authHeader) > 7 && authHeader[:7] == "Bearer " {
|
|
token = authHeader[7:] // 去掉 "Bearer " 前缀
|
|
}
|
|
}
|
|
|
|
// 如果还是没有,尝试从 cookie 获取
|
|
if token == "" {
|
|
j := NewJWT()
|
|
token, _ = c.Cookie("user-token")
|
|
claims, err := j.ParseToken(token)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在user-token或Authorization Bearer且claims是否为规定结构")
|
|
return token
|
|
}
|
|
SetUserToken(c, token, int((claims.ExpiresAt.Unix()-time.Now().Unix())/60))
|
|
}
|
|
return token
|
|
}
|
|
|
|
func GetToken(c *gin.Context) string {
|
|
token := c.Request.Header.Get("x-token")
|
|
if token == "" {
|
|
j := NewJWT()
|
|
token, _ = c.Cookie("x-token")
|
|
claims, err := j.ParseToken(token)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在x-token且claims是否为规定结构")
|
|
return token
|
|
}
|
|
SetToken(c, token, int((claims.ExpiresAt.Unix()-time.Now().Unix())/60))
|
|
}
|
|
return token
|
|
}
|
|
|
|
func GetClaims(c *gin.Context) (*systemReq.CustomClaims, error) {
|
|
token := GetToken(c)
|
|
j := NewJWT()
|
|
claims, err := j.ParseToken(token)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("从Gin的Context中获取从jwt解析信息失败, 请检查请求头是否存在x-token且claims是否为规定结构")
|
|
}
|
|
return claims, err
|
|
}
|
|
|
|
// GetUserID 从Gin的Context中获取从jwt解析出来的用户ID
|
|
func GetUserID(c *gin.Context) uint {
|
|
if claims, exists := c.Get("claims"); !exists {
|
|
if cl, err := GetClaims(c); err != nil {
|
|
return 0
|
|
} else {
|
|
return cl.BaseClaims.ID
|
|
}
|
|
} else {
|
|
waitUse := claims.(*systemReq.CustomClaims)
|
|
return waitUse.BaseClaims.ID
|
|
}
|
|
}
|
|
|
|
// GetUserUuid 从Gin的Context中获取从jwt解析出来的用户UUID
|
|
func GetUserUuid(c *gin.Context) uuid.UUID {
|
|
if claims, exists := c.Get("claims"); !exists {
|
|
if cl, err := GetClaims(c); err != nil {
|
|
return uuid.UUID{}
|
|
} else {
|
|
return cl.UUID
|
|
}
|
|
} else {
|
|
waitUse := claims.(*systemReq.CustomClaims)
|
|
return waitUse.UUID
|
|
}
|
|
}
|
|
|
|
// GetUserAuthorityId 从Gin的Context中获取从jwt解析出来的用户角色id
|
|
func GetUserAuthorityId(c *gin.Context) uint {
|
|
if claims, exists := c.Get("claims"); !exists {
|
|
if cl, err := GetClaims(c); err != nil {
|
|
return 0
|
|
} else {
|
|
return cl.AuthorityId
|
|
}
|
|
} else {
|
|
waitUse := claims.(*systemReq.CustomClaims)
|
|
return waitUse.AuthorityId
|
|
}
|
|
}
|
|
|
|
// GetUserInfo 从Gin的Context中获取从jwt解析出来的用户角色id
|
|
func GetUserInfo(c *gin.Context) *systemReq.CustomClaims {
|
|
if claims, exists := c.Get("claims"); !exists {
|
|
if cl, err := GetClaims(c); err != nil {
|
|
return nil
|
|
} else {
|
|
return cl
|
|
}
|
|
} else {
|
|
waitUse := claims.(*systemReq.CustomClaims)
|
|
return waitUse
|
|
}
|
|
}
|
|
|
|
// GetUserName 从Gin的Context中获取从jwt解析出来的用户名
|
|
func GetUserName(c *gin.Context) string {
|
|
if claims, exists := c.Get("claims"); !exists {
|
|
if cl, err := GetClaims(c); err != nil {
|
|
return ""
|
|
} else {
|
|
return cl.Username
|
|
}
|
|
} else {
|
|
waitUse := claims.(*systemReq.CustomClaims)
|
|
return waitUse.Username
|
|
}
|
|
}
|
|
|
|
func LoginToken(user system.Login) (token string, claims systemReq.CustomClaims, err error) {
|
|
j := NewJWT()
|
|
claims = j.CreateClaims(systemReq.BaseClaims{
|
|
UUID: user.GetUUID(),
|
|
ID: user.GetUserId(),
|
|
NickName: user.GetNickname(),
|
|
Username: user.GetUsername(),
|
|
AuthorityId: user.GetAuthorityId(),
|
|
})
|
|
token, err = j.CreateToken(claims)
|
|
return
|
|
}
|
|
|
|
// AppUserLogin 小程序用户登录接口
|
|
type AppUserLogin interface {
|
|
GetUUID() uuid.UUID
|
|
GetUserId() uint
|
|
GetOpenID() string
|
|
GetUnionID() string
|
|
GetNickname() string
|
|
GetAvatar() string
|
|
GetPhone() string
|
|
GetGender() int
|
|
GetCity() string
|
|
GetProvince() string
|
|
GetCountry() string
|
|
}
|
|
|
|
// AppUserLoginToken 创建小程序用户登录token
|
|
func AppUserLoginToken(user AppUserLogin) (token string, claims systemReq.AppUserClaims, err error) {
|
|
j := NewJWT()
|
|
claims = j.CreateAppUserClaims(systemReq.AppBaseClaims{
|
|
UUID: user.GetUUID(),
|
|
ID: user.GetUserId(),
|
|
OpenID: user.GetOpenID(),
|
|
UnionID: user.GetUnionID(),
|
|
NickName: user.GetNickname(),
|
|
Avatar: user.GetAvatar(),
|
|
Phone: user.GetPhone(),
|
|
Gender: user.GetGender(),
|
|
City: user.GetCity(),
|
|
Province: user.GetProvince(),
|
|
Country: user.GetCountry(),
|
|
})
|
|
token, err = j.CreateAppUserToken(claims)
|
|
return
|
|
}
|
|
|
|
// 用户端专用Claims获取函数
|
|
|
|
func GetAppUserClaims(c *gin.Context) (*systemReq.AppUserClaims, error) {
|
|
token := GetUserToken(c)
|
|
j := NewJWT()
|
|
claims, err := j.ParseAppUserToken(token)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("从Gin的Context中获取从jwt解析信息失败, 请检查请求头是否存在user-token且claims是否为规定结构")
|
|
}
|
|
return claims, err
|
|
}
|
|
|
|
// GetAppUserID 从Gin的Context中获取从jwt解析出来的用户ID
|
|
func GetAppUserID(c *gin.Context) uint {
|
|
if claims, exists := c.Get("user_claims"); !exists {
|
|
if cl, err := GetAppUserClaims(c); err != nil {
|
|
return 0
|
|
} else {
|
|
return cl.AppBaseClaims.ID
|
|
}
|
|
} else {
|
|
waitUse := claims.(*systemReq.AppUserClaims)
|
|
return waitUse.AppBaseClaims.ID
|
|
}
|
|
}
|
|
|
|
// GetAppUserUuid 从Gin的Context中获取从jwt解析出来的用户UUID
|
|
func GetAppUserUuid(c *gin.Context) uuid.UUID {
|
|
if claims, exists := c.Get("user_claims"); !exists {
|
|
if cl, err := GetAppUserClaims(c); err != nil {
|
|
return uuid.UUID{}
|
|
} else {
|
|
return cl.AppBaseClaims.UUID
|
|
}
|
|
} else {
|
|
waitUse := claims.(*systemReq.AppUserClaims)
|
|
return waitUse.AppBaseClaims.UUID
|
|
}
|
|
}
|
|
|
|
// GetAppUserInfo 从Gin的Context中获取从jwt解析出来的用户信息
|
|
func GetAppUserInfo(c *gin.Context) *systemReq.AppUserClaims {
|
|
if claims, exists := c.Get("user_claims"); !exists {
|
|
if cl, err := GetAppUserClaims(c); err != nil {
|
|
return nil
|
|
} else {
|
|
return cl
|
|
}
|
|
} else {
|
|
waitUse := claims.(*systemReq.AppUserClaims)
|
|
return waitUse
|
|
}
|
|
}
|
|
|
|
// GetAppUserOpenID 从Gin的Context中获取从jwt解析出来的微信OpenID
|
|
func GetAppUserOpenID(c *gin.Context) string {
|
|
if claims, exists := c.Get("user_claims"); !exists {
|
|
if cl, err := GetAppUserClaims(c); err != nil {
|
|
return ""
|
|
} else {
|
|
return cl.AppBaseClaims.OpenID
|
|
}
|
|
} else {
|
|
waitUse := claims.(*systemReq.AppUserClaims)
|
|
return waitUse.AppBaseClaims.OpenID
|
|
}
|
|
}
|
|
|
|
// GetAppUserNickName 从Gin的Context中获取从jwt解析出来的用户昵称
|
|
func GetAppUserNickName(c *gin.Context) string {
|
|
if claims, exists := c.Get("user_claims"); !exists {
|
|
if cl, err := GetAppUserClaims(c); err != nil {
|
|
return ""
|
|
} else {
|
|
return cl.AppBaseClaims.NickName
|
|
}
|
|
} else {
|
|
waitUse := claims.(*systemReq.AppUserClaims)
|
|
return waitUse.AppBaseClaims.NickName
|
|
}
|
|
}
|
|
|
|
// GetAppUserAvatar 从Gin的Context中获取从jwt解析出来的用户头像
|
|
func GetAppUserAvatar(c *gin.Context) string {
|
|
if claims, exists := c.Get("user_claims"); !exists {
|
|
if cl, err := GetAppUserClaims(c); err != nil {
|
|
return ""
|
|
} else {
|
|
return cl.AppBaseClaims.Avatar
|
|
}
|
|
} else {
|
|
waitUse := claims.(*systemReq.AppUserClaims)
|
|
return waitUse.AppBaseClaims.Avatar
|
|
}
|
|
}
|