ferry/tools/user.go
2020-08-15 23:34:37 +08:00

65 lines
1.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tools
import (
jwt "ferry/pkg/jwtauth"
"ferry/pkg/logger"
"fmt"
"github.com/gin-gonic/gin"
)
func ExtractClaims(c *gin.Context) jwt.MapClaims {
claims, exists := c.Get("JWT_PAYLOAD")
if !exists {
return make(jwt.MapClaims)
}
return claims.(jwt.MapClaims)
}
func GetUserId(c *gin.Context) int {
data := ExtractClaims(c)
if data["identity"] != nil {
return int((data["identity"]).(float64))
}
logger.Info("********** 路径:" + c.Request.URL.Path + " 请求方法:" + c.Request.Method + " 说明缺少identity")
return 0
}
func GetUserIdStr(c *gin.Context) string {
data := ExtractClaims(c)
if data["identity"] != nil {
return Int64ToString(int64((data["identity"]).(float64)))
}
logger.Info("********** 路径:" + c.Request.URL.Path + " 请求方法:" + c.Request.Method + " 缺少identity")
return ""
}
func GetUserName(c *gin.Context) string {
data := ExtractClaims(c)
if data["nice"] != nil {
return (data["nice"]).(string)
}
fmt.Println("********** 路径:" + c.Request.URL.Path + " 请求方法:" + c.Request.Method + " 缺少nice")
return ""
}
func GetRoleName(c *gin.Context) string {
data := ExtractClaims(c)
if data["rolekey"] != nil {
return (data["rolekey"]).(string)
}
fmt.Println("********** 路径:" + c.Request.URL.Path + " 请求方法:" + c.Request.Method + " 缺少rolekey")
return ""
}
func GetRoleId(c *gin.Context) int {
data := ExtractClaims(c)
if data["roleid"] != nil {
i := int((data["roleid"]).(float64))
return i
}
fmt.Println("********** 路径:" + c.Request.URL.Path + " 请求方法:" + c.Request.Method + " 缺少roleid")
return 0
}