From 5acb536281749dd475eb7bbd63173bdc0fe50606 Mon Sep 17 00:00:00 2001 From: zhilv Date: Sat, 28 Mar 2026 19:27:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B0=86=E5=BC=80=E5=8F=91=E5=92=8C?= =?UTF-8?q?=E7=94=9F=E4=BA=A7=E7=8E=AF=E5=A2=83=E8=BF=9B=E8=A1=8C=E5=8C=BA?= =?UTF-8?q?=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/dto/ckwk.go | 4 ++++ internal/handler/ckwk.go | 44 +++++++++++++++++++++++++++++++++++++++ internal/router/router.go | 27 ++++++++++++++++++++---- 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/internal/dto/ckwk.go b/internal/dto/ckwk.go index c5bdbbd..7eed3c5 100644 --- a/internal/dto/ckwk.go +++ b/internal/dto/ckwk.go @@ -42,6 +42,10 @@ type LoginReq struct { Host string `json:"host" binding:"required"` } +type CourseReq struct { + Status ckwk.CourseKind `json:"status"` +} + type StudyReq struct { NodeID string `json:"node_id"` StudyID string `json:"study_id"` diff --git a/internal/handler/ckwk.go b/internal/handler/ckwk.go index beb241c..9b4cba5 100644 --- a/internal/handler/ckwk.go +++ b/internal/handler/ckwk.go @@ -93,6 +93,50 @@ func (h *WKHandler) Logout(ctx *gin.Context) { ctx.JSON(200, dto.Ok()) } +func (h *WKHandler) UserInfo(ctx *gin.Context) { + val, ok := ctx.Get("wk_instance") + if !ok { + ctx.JSON(200, dto.Error(-1, "登录已过期")) + return + } + wk := val.(*ckwk.WK) + + userinfo, err := wk.UserInfoGet() + if err != nil { + ctx.JSON(200, dto.Error(-1, err.Error())) + return + } + + ctx.JSON(200, dto.Success(map[string]any{ + "user": userinfo, + })) +} + +func (h *WKHandler) Course(ctx *gin.Context) { + var req dto.CourseReq + if err := ctx.ShouldBindJSON(&req); err != nil { + ctx.JSON(200, dto.Error(-1, "请求参数错误")) + return + } + + val, ok := ctx.Get("wk_instance") + if !ok { + ctx.JSON(200, dto.Error(-1, "登录已过期")) + return + } + wk := val.(*ckwk.WK) + + courses, err := wk.CourseGet(req.Status) + if err != nil { + ctx.JSON(200, dto.Error(-1, err.Error())) + return + } + + ctx.JSON(200, dto.Success(map[string]any{ + "courses": courses, + })) +} + func (h *WKHandler) Study(ctx *gin.Context) { val, ok := ctx.Get("wk_instance") if !ok { diff --git a/internal/router/router.go b/internal/router/router.go index 45ea1ea..787f9d2 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -16,17 +16,34 @@ import ( "go.uber.org/zap" ) -func SetupRouter() *gin.Engine { +var ( + AllowOrigins []string + AllowMethods []string + AllowHeaders []string +) + +func init() { if conf.Mode != gin.ReleaseMode { + AllowOrigins = []string{"*"} + AllowMethods = []string{"*"} + AllowHeaders = []string{"*"} gin.SetMode(gin.DebugMode) } else { + AllowOrigins = []string{"*.kmux.cn"} + AllowMethods = []string{"GET", "POST", "PUT", "DELETE", "PATCH"} + AllowHeaders = []string{"X-Session-Id"} gin.SetMode(gin.ReleaseMode) } +} + +func SetupRouter() *gin.Engine { + r := gin.Default() + r.Use(cors.New(cors.Config{ - AllowOrigins: []string{"*.kmux.cn*"}, - AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH"}, - AllowHeaders: []string{"*", "X-Session-Id"}, + AllowOrigins: AllowOrigins, + AllowMethods: AllowMethods, + AllowHeaders: AllowHeaders, ExposeHeaders: []string{"Content-Length"}, AllowCredentials: true, MaxAge: 12 * time.Hour, @@ -56,6 +73,8 @@ func SetupRouter() *gin.Engine { v2 := api.Group("/v2", sessionMiddleware) { v2.POST("/logout", wkHandler.Logout) + v2.POST("/userinfo", wkHandler.UserInfo) + v2.POST("/course", wkHandler.Course) v2.POST("/study", wkHandler.Study) v2.POST("/record", wkHandler.AllRecord) }