Migrate all methods in the model package to the service package

This commit is contained in:
Granty1
2020-04-08 11:07:27 +08:00
parent 2c35adaffc
commit 5d222585dd
49 changed files with 1348 additions and 1261 deletions

View File

@@ -5,9 +5,10 @@ import (
"gin-vue-admin/global"
"gin-vue-admin/global/response"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
"gin-vue-admin/service"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
uuid "github.com/satori/go.uuid"
"time"
)
@@ -15,7 +16,7 @@ func JWTAuth() gin.HandlerFunc {
return func(c *gin.Context) {
// 我们这里jwt鉴权取头部信息 x-token 登录时回返回token信息 这里前端需要把token存储到cookie或者本地localSstorage中 不过需要跟后端协商过期时间 可以约定刷新令牌或者重新登录
token := c.Request.Header.Get("x-token")
ModelToken := model.JwtBlacklist{
modelToken := model.JwtBlacklist{
Jwt: token,
}
if token == "" {
@@ -25,7 +26,7 @@ func JWTAuth() gin.HandlerFunc {
c.Abort()
return
}
if ModelToken.IsBlacklist(token) {
if service.IsBlacklist(token,modelToken) {
response.Result(response.ERROR, gin.H{
"reload": true,
}, "您的帐户异地登陆或令牌失效", c)
@@ -65,13 +66,7 @@ var (
TokenInvalid error = errors.New("Couldn't handle this token:")
)
type CustomClaims struct {
UUID uuid.UUID
ID uint
NickName string
AuthorityId string
jwt.StandardClaims
}
func NewJWT() *JWT {
return &JWT{
@@ -80,14 +75,14 @@ func NewJWT() *JWT {
}
//创建一个token
func (j *JWT) CreateToken(claims CustomClaims) (string, error) {
func (j *JWT) CreateToken(claims request. CustomClaims) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(j.SigningKey)
}
//解析 token
func (j *JWT) ParseToken(tokenString string) (*CustomClaims, error) {
token, err := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(token *jwt.Token) (i interface{}, e error) {
func (j *JWT) ParseToken(tokenString string) (*request.CustomClaims, error) {
token, err := jwt.ParseWithClaims(tokenString, &request.CustomClaims{}, func(token *jwt.Token) (i interface{}, e error) {
return j.SigningKey, nil
})
if err != nil {
@@ -105,7 +100,7 @@ func (j *JWT) ParseToken(tokenString string) (*CustomClaims, error) {
}
}
if token != nil {
if claims, ok := token.Claims.(*CustomClaims); ok && token.Valid {
if claims, ok := token.Claims.(*request.CustomClaims); ok && token.Valid {
return claims, nil
}
return nil, TokenInvalid
@@ -122,13 +117,13 @@ func (j *JWT) RefreshToken(tokenString string) (string, error) {
jwt.TimeFunc = func() time.Time {
return time.Unix(0, 0)
}
token, err := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
token, err := jwt.ParseWithClaims(tokenString, &request.CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
return j.SigningKey, nil
})
if err != nil {
return "", err
}
if claims, ok := token.Claims.(*CustomClaims); ok && token.Valid {
if claims, ok := token.Claims.(*request.CustomClaims); ok && token.Valid {
jwt.TimeFunc = time.Now
claims.StandardClaims.ExpiresAt = time.Now().Add(1 * time.Hour).Unix()
return j.CreateToken(*claims)