fix: 修改包名
This commit is contained in:
parent
b89d1021c9
commit
ee1a42a106
|
@ -1,4 +1,4 @@
|
|||
package _const
|
||||
package constant
|
||||
|
||||
// gva 常量文件,用于定义一些常量 , 如果修改此处,请自行修改前端中的/src/const/index.js 中对应的
|
||||
const TOKEN_NAME = "x-token" // token ,此处是存储在localStorage中的token名称
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package _const
|
||||
package constant
|
||||
|
||||
const (
|
||||
JWT_NO_AUTH = 6
|
||||
|
|
|
@ -2,6 +2,7 @@ package middleware
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
constant "github.com/flipped-aurora/gin-vue-admin/server/const"
|
||||
"io"
|
||||
"strconv"
|
||||
"time"
|
||||
|
@ -25,7 +26,7 @@ func ErrorToEmail() gin.HandlerFunc {
|
|||
if claims.Username != "" {
|
||||
username = claims.Username
|
||||
} else {
|
||||
id, _ := strconv.Atoi(c.Request.Header.Get("x-user-id"))
|
||||
id, _ := strconv.Atoi(c.Request.Header.Get(constant.REQUEST_USER_ID))
|
||||
user, err := userService.FindUserById(id)
|
||||
if err != nil {
|
||||
username = "Unknown"
|
||||
|
|
|
@ -2,7 +2,7 @@ package middleware
|
|||
|
||||
import (
|
||||
"errors"
|
||||
_const "github.com/flipped-aurora/gin-vue-admin/server/const"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/const"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/utils"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
|
@ -56,14 +56,14 @@ func JWTAuth() gin.HandlerFunc {
|
|||
// response.FailWithDetailed(gin.H{"reload": true}, err.Error(), c)
|
||||
// c.Abort()
|
||||
//}
|
||||
c.Set(_const.JWT_CLAIMS, claims)
|
||||
c.Set(constant.JWT_CLAIMS, claims)
|
||||
if claims.ExpiresAt.Unix()-time.Now().Unix() < claims.BufferTime {
|
||||
dr, _ := utils.ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime)
|
||||
claims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(dr))
|
||||
newToken, _ := j.CreateTokenByOldToken(token, *claims)
|
||||
newClaims, _ := j.ParseToken(newToken)
|
||||
c.Header(_const.NEW_TOKEN_NAME, newToken)
|
||||
c.Header(_const.NEW_TOKEN_EXPIRES_AT, strconv.FormatInt(newClaims.ExpiresAt.Unix(), 10))
|
||||
c.Header(constant.NEW_TOKEN_NAME, newToken)
|
||||
c.Header(constant.NEW_TOKEN_EXPIRES_AT, strconv.FormatInt(newClaims.ExpiresAt.Unix(), 10))
|
||||
utils.SetToken(c, newToken, int(dr.Seconds()))
|
||||
if global.GVA_CONFIG.System.UseMultipoint {
|
||||
// 记录新的活跃jwt
|
||||
|
@ -72,11 +72,11 @@ func JWTAuth() gin.HandlerFunc {
|
|||
}
|
||||
c.Next()
|
||||
|
||||
if newToken, exists := c.Get(_const.NEW_TOKEN_NAME); exists {
|
||||
c.Header(_const.NEW_TOKEN_NAME, newToken.(string))
|
||||
if newToken, exists := c.Get(constant.NEW_TOKEN_NAME); exists {
|
||||
c.Header(constant.NEW_TOKEN_NAME, newToken.(string))
|
||||
}
|
||||
if newExpiresAt, exists := c.Get(_const.NEW_TOKEN_EXPIRES_AT); exists {
|
||||
c.Header(_const.NEW_TOKEN_EXPIRES_AT, newExpiresAt.(string))
|
||||
if newExpiresAt, exists := c.Get(constant.NEW_TOKEN_EXPIRES_AT); exists {
|
||||
c.Header(constant.NEW_TOKEN_EXPIRES_AT, newExpiresAt.(string))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package middleware
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
_const "github.com/flipped-aurora/gin-vue-admin/server/const"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/const"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
|
@ -27,7 +27,7 @@ type LimitConfig struct {
|
|||
func (l LimitConfig) LimitWithTime() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if err := l.CheckOrMark(l.GenerationKey(c), l.Expire, l.Limit); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"code": _const.ERROR, "msg": err})
|
||||
c.JSON(http.StatusOK, gin.H{"code": constant.ERROR, "msg": err})
|
||||
c.Abort()
|
||||
return
|
||||
} else {
|
||||
|
|
|
@ -3,6 +3,7 @@ package middleware
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
constant "github.com/flipped-aurora/gin-vue-admin/server/const"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
@ -60,7 +61,7 @@ func OperationRecord() gin.HandlerFunc {
|
|||
if claims != nil && claims.BaseClaims.ID != 0 {
|
||||
userId = int(claims.BaseClaims.ID)
|
||||
} else {
|
||||
id, err := strconv.Atoi(c.Request.Header.Get("x-user-id"))
|
||||
id, err := strconv.Atoi(c.Request.Header.Get(constant.REQUEST_USER_ID))
|
||||
if err != nil {
|
||||
userId = 0
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package response
|
||||
|
||||
import (
|
||||
_const "github.com/flipped-aurora/gin-vue-admin/server/const"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/const"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
@ -23,37 +23,37 @@ func Result(code int, data interface{}, msg string, c *gin.Context) {
|
|||
}
|
||||
|
||||
func Ok(c *gin.Context) {
|
||||
Result(_const.SUCCESS, map[string]interface{}{}, "操作成功", c)
|
||||
Result(constant.SUCCESS, map[string]interface{}{}, "操作成功", c)
|
||||
}
|
||||
|
||||
func OkWithMessage(message string, c *gin.Context) {
|
||||
Result(_const.SUCCESS, map[string]interface{}{}, message, c)
|
||||
Result(constant.SUCCESS, map[string]interface{}{}, message, c)
|
||||
}
|
||||
|
||||
func OkWithData(data interface{}, c *gin.Context) {
|
||||
Result(_const.SUCCESS, data, "成功", c)
|
||||
Result(constant.SUCCESS, data, "成功", c)
|
||||
}
|
||||
|
||||
func OkWithDetailed(data interface{}, message string, c *gin.Context) {
|
||||
Result(_const.SUCCESS, data, message, c)
|
||||
Result(constant.SUCCESS, data, message, c)
|
||||
}
|
||||
|
||||
func Fail(c *gin.Context) {
|
||||
Result(_const.ERROR, map[string]interface{}{}, "操作失败", c)
|
||||
Result(constant.ERROR, map[string]interface{}{}, "操作失败", c)
|
||||
}
|
||||
|
||||
func FailWithMessage(message string, c *gin.Context) {
|
||||
Result(_const.ERROR, map[string]interface{}{}, message, c)
|
||||
Result(constant.ERROR, map[string]interface{}{}, message, c)
|
||||
}
|
||||
|
||||
func NoAuth(message string, c *gin.Context) {
|
||||
c.JSON(http.StatusUnauthorized, Response{
|
||||
_const.JWT_NO_AUTH,
|
||||
constant.JWT_NO_AUTH,
|
||||
nil,
|
||||
message,
|
||||
})
|
||||
}
|
||||
|
||||
func FailWithDetailed(data interface{}, message string, c *gin.Context) {
|
||||
Result(_const.ERROR, data, message, c)
|
||||
Result(constant.ERROR, data, message, c)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
_const "github.com/flipped-aurora/gin-vue-admin/server/const"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/const"
|
||||
"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"
|
||||
|
@ -19,9 +19,9 @@ func ClearToken(c *gin.Context) {
|
|||
}
|
||||
|
||||
if net.ParseIP(host) != nil {
|
||||
c.SetCookie(_const.REQUSET_COOKIE_TOKEN_NAME, "", -1, "/", "", false, false)
|
||||
c.SetCookie(constant.REQUSET_COOKIE_TOKEN_NAME, "", -1, "/", "", false, false)
|
||||
} else {
|
||||
c.SetCookie(_const.REQUSET_COOKIE_TOKEN_NAME, "", -1, "/", host, false, false)
|
||||
c.SetCookie(constant.REQUSET_COOKIE_TOKEN_NAME, "", -1, "/", host, false, false)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -33,17 +33,17 @@ func SetToken(c *gin.Context, token string, maxAge int) {
|
|||
}
|
||||
|
||||
if net.ParseIP(host) != nil {
|
||||
c.SetCookie(_const.REQUSET_COOKIE_TOKEN_NAME, token, maxAge, "/", "", false, false)
|
||||
c.SetCookie(constant.REQUSET_COOKIE_TOKEN_NAME, token, maxAge, "/", "", false, false)
|
||||
} else {
|
||||
c.SetCookie(_const.REQUSET_COOKIE_TOKEN_NAME, token, maxAge, "/", host, false, false)
|
||||
c.SetCookie(constant.REQUSET_COOKIE_TOKEN_NAME, token, maxAge, "/", host, false, false)
|
||||
}
|
||||
}
|
||||
|
||||
func GetToken(c *gin.Context) string {
|
||||
token, _ := c.Cookie(_const.REQUSET_COOKIE_TOKEN_NAME)
|
||||
token, _ := c.Cookie(constant.REQUSET_COOKIE_TOKEN_NAME)
|
||||
if token == "" {
|
||||
j := NewJWT()
|
||||
token = c.Request.Header.Get(_const.REQUEST_HEAER_TOKEN_NAME)
|
||||
token = c.Request.Header.Get(constant.REQUEST_HEAER_TOKEN_NAME)
|
||||
claims, err := j.ParseToken(token)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在x-token且claims是否为规定结构")
|
||||
|
@ -66,7 +66,7 @@ func GetClaims(c *gin.Context) (*systemReq.CustomClaims, error) {
|
|||
|
||||
// GetUserID 从Gin的Context中获取从jwt解析出来的用户ID
|
||||
func GetUserID(c *gin.Context) uint {
|
||||
if claims, exists := c.Get(_const.JWT_CLAIMS); !exists {
|
||||
if claims, exists := c.Get(constant.JWT_CLAIMS); !exists {
|
||||
if cl, err := GetClaims(c); err != nil {
|
||||
return 0
|
||||
} else {
|
||||
|
@ -80,7 +80,7 @@ func GetUserID(c *gin.Context) uint {
|
|||
|
||||
// GetUserUuid 从Gin的Context中获取从jwt解析出来的用户UUID
|
||||
func GetUserUuid(c *gin.Context) uuid.UUID {
|
||||
if claims, exists := c.Get(_const.JWT_CLAIMS); !exists {
|
||||
if claims, exists := c.Get(constant.JWT_CLAIMS); !exists {
|
||||
if cl, err := GetClaims(c); err != nil {
|
||||
return uuid.UUID{}
|
||||
} else {
|
||||
|
@ -94,7 +94,7 @@ func GetUserUuid(c *gin.Context) uuid.UUID {
|
|||
|
||||
// GetUserAuthorityId 从Gin的Context中获取从jwt解析出来的用户角色id
|
||||
func GetUserAuthorityId(c *gin.Context) uint {
|
||||
if claims, exists := c.Get(_const.JWT_CLAIMS); !exists {
|
||||
if claims, exists := c.Get(constant.JWT_CLAIMS); !exists {
|
||||
if cl, err := GetClaims(c); err != nil {
|
||||
return 0
|
||||
} else {
|
||||
|
@ -108,7 +108,7 @@ func GetUserAuthorityId(c *gin.Context) uint {
|
|||
|
||||
// GetUserInfo 从Gin的Context中获取从jwt解析出来的用户角色id
|
||||
func GetUserInfo(c *gin.Context) *systemReq.CustomClaims {
|
||||
if claims, exists := c.Get(_const.JWT_CLAIMS); !exists {
|
||||
if claims, exists := c.Get(constant.JWT_CLAIMS); !exists {
|
||||
if cl, err := GetClaims(c); err != nil {
|
||||
return nil
|
||||
} else {
|
||||
|
@ -122,7 +122,7 @@ func GetUserInfo(c *gin.Context) *systemReq.CustomClaims {
|
|||
|
||||
// GetUserName 从Gin的Context中获取从jwt解析出来的用户名
|
||||
func GetUserName(c *gin.Context) string {
|
||||
if claims, exists := c.Get(_const.JWT_CLAIMS); !exists {
|
||||
if claims, exists := c.Get(constant.JWT_CLAIMS); !exists {
|
||||
if cl, err := GetClaims(c); err != nil {
|
||||
return ""
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue