Skip to content

Commit

Permalink
fix: 修改包名
Browse files Browse the repository at this point in the history
  • Loading branch information
bypanghu committed Nov 25, 2024
1 parent b89d102 commit ee1a42a
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion server/const/const.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package _const
package constant

// gva 常量文件,用于定义一些常量 , 如果修改此处,请自行修改前端中的/src/const/index.js 中对应的
const TOKEN_NAME = "x-token" // token ,此处是存储在localStorage中的token名称
Expand Down
2 changes: 1 addition & 1 deletion server/const/errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package _const
package constant

const (
JWT_NO_AUTH = 6
Expand Down
3 changes: 2 additions & 1 deletion server/middleware/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package middleware

import (
"bytes"
constant "github.com/flipped-aurora/gin-vue-admin/server/const"
"io"
"strconv"
"time"
Expand All @@ -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"
Expand Down
16 changes: 8 additions & 8 deletions server/middleware/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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))
}
}
}
4 changes: 2 additions & 2 deletions server/middleware/limit_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion server/middleware/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 9 additions & 9 deletions server/model/common/response/response.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
}
24 changes: 12 additions & 12 deletions server/utils/claims.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
}
}

Expand All @@ -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是否为规定结构")
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit ee1a42a

Please sign in to comment.