diff --git a/api/drafting.go b/api/drafting.go index 1f21f08..1d313bc 100644 --- a/api/drafting.go +++ b/api/drafting.go @@ -7,6 +7,7 @@ import ( jwt "github.com/appleboy/gin-jwt/v2" "github.com/gin-gonic/gin" "go.uber.org/zap" + "joylink.club/bj-rtsts-server/db/model" "joylink.club/bj-rtsts-server/dto" "joylink.club/bj-rtsts-server/middleware" "joylink.club/bj-rtsts-server/service" @@ -68,7 +69,8 @@ func pageQueryDrafting(c *gin.Context) { // @Failure 500 {object} dto.ErrorDto // @Router /api/v1/drafting [post] func createDrafting(c *gin.Context) { - createId := middleware.ParseContextUserId(c) + user, _ := c.Get(middleware.IdentityKey) + createId := user.(model.User).ID req := dto.DraftingDto{} if err := c.ShouldBind(&req); err != nil { panic("传入参数为空") @@ -101,13 +103,14 @@ func saveAsDrafting(c *gin.Context) { panic("必要参数id不存在") } zap.S().Debug("传入参数id为" + id) - createId := middleware.ParseContextUserId(c) + user, _ := c.Get(middleware.IdentityKey) + createrId := user.(model.User).ID req := dto.DraftingDto{} if err := c.ShouldBind(&req); err != nil { zap.S().Warn("保存数据出错", err) } int64Id, _ := strconv.ParseInt(id, 10, 64) - service.SaveAsDrafting(createId, int32(int64Id), &req) + service.SaveAsDrafting(createrId, int32(int64Id), &req) c.Status(http.StatusOK) } diff --git a/middleware/jwt.go b/middleware/jwt.go index 4157f7f..d607080 100644 --- a/middleware/jwt.go +++ b/middleware/jwt.go @@ -2,6 +2,7 @@ package middleware import ( "log" + "strconv" "time" jwt "github.com/appleboy/gin-jwt/v2" @@ -12,6 +13,7 @@ import ( ) const IdentityKey = "id" +const CentrifugoKey = "sub" // centrifugo 消息传递服务器token验证需要的主键 func InitGinJwtMiddleware() (authMiddleware *jwt.GinJWTMiddleware) { // the jwt middleware @@ -24,7 +26,8 @@ func InitGinJwtMiddleware() (authMiddleware *jwt.GinJWTMiddleware) { PayloadFunc: func(data interface{}) jwt.MapClaims { if v, ok := data.(*model.User); ok { return jwt.MapClaims{ - IdentityKey: v.ID, + IdentityKey: v.ID, + CentrifugoKey: strconv.Itoa(int(v.ID)), } } return jwt.MapClaims{} @@ -79,8 +82,3 @@ func InitGinJwtMiddleware() (authMiddleware *jwt.GinJWTMiddleware) { return } - -func ParseContextUserId(c *gin.Context) int32 { - claims := jwt.ExtractClaims(c) - return int32(claims[IdentityKey].(float64)) -}