【修改草稿获取用户ID方式】

【增加生成消息中心token关键字】
This commit is contained in:
weizhihong 2023-07-27 16:59:55 +08:00
parent d4f5915a7a
commit 93c41175ee
2 changed files with 10 additions and 9 deletions

View File

@ -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)
}

View File

@ -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
@ -25,6 +27,7 @@ func InitGinJwtMiddleware() (authMiddleware *jwt.GinJWTMiddleware) {
if v, ok := data.(*model.User); ok {
return jwt.MapClaims{
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))
}