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