fix: 将开发和生产环境进行区分
This commit is contained in:
@@ -42,6 +42,10 @@ type LoginReq struct {
|
|||||||
Host string `json:"host" binding:"required"`
|
Host string `json:"host" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CourseReq struct {
|
||||||
|
Status ckwk.CourseKind `json:"status"`
|
||||||
|
}
|
||||||
|
|
||||||
type StudyReq struct {
|
type StudyReq struct {
|
||||||
NodeID string `json:"node_id"`
|
NodeID string `json:"node_id"`
|
||||||
StudyID string `json:"study_id"`
|
StudyID string `json:"study_id"`
|
||||||
|
|||||||
@@ -93,6 +93,50 @@ func (h *WKHandler) Logout(ctx *gin.Context) {
|
|||||||
ctx.JSON(200, dto.Ok())
|
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) {
|
func (h *WKHandler) Study(ctx *gin.Context) {
|
||||||
val, ok := ctx.Get("wk_instance")
|
val, ok := ctx.Get("wk_instance")
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
@@ -16,17 +16,34 @@ import (
|
|||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetupRouter() *gin.Engine {
|
var (
|
||||||
|
AllowOrigins []string
|
||||||
|
AllowMethods []string
|
||||||
|
AllowHeaders []string
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
if conf.Mode != gin.ReleaseMode {
|
if conf.Mode != gin.ReleaseMode {
|
||||||
|
AllowOrigins = []string{"*"}
|
||||||
|
AllowMethods = []string{"*"}
|
||||||
|
AllowHeaders = []string{"*"}
|
||||||
gin.SetMode(gin.DebugMode)
|
gin.SetMode(gin.DebugMode)
|
||||||
} else {
|
} else {
|
||||||
|
AllowOrigins = []string{"*.kmux.cn"}
|
||||||
|
AllowMethods = []string{"GET", "POST", "PUT", "DELETE", "PATCH"}
|
||||||
|
AllowHeaders = []string{"X-Session-Id"}
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetupRouter() *gin.Engine {
|
||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
r.Use(cors.New(cors.Config{
|
r.Use(cors.New(cors.Config{
|
||||||
AllowOrigins: []string{"*.kmux.cn*"},
|
AllowOrigins: AllowOrigins,
|
||||||
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH"},
|
AllowMethods: AllowMethods,
|
||||||
AllowHeaders: []string{"*", "X-Session-Id"},
|
AllowHeaders: AllowHeaders,
|
||||||
ExposeHeaders: []string{"Content-Length"},
|
ExposeHeaders: []string{"Content-Length"},
|
||||||
AllowCredentials: true,
|
AllowCredentials: true,
|
||||||
MaxAge: 12 * time.Hour,
|
MaxAge: 12 * time.Hour,
|
||||||
@@ -56,6 +73,8 @@ func SetupRouter() *gin.Engine {
|
|||||||
v2 := api.Group("/v2", sessionMiddleware)
|
v2 := api.Group("/v2", sessionMiddleware)
|
||||||
{
|
{
|
||||||
v2.POST("/logout", wkHandler.Logout)
|
v2.POST("/logout", wkHandler.Logout)
|
||||||
|
v2.POST("/userinfo", wkHandler.UserInfo)
|
||||||
|
v2.POST("/course", wkHandler.Course)
|
||||||
v2.POST("/study", wkHandler.Study)
|
v2.POST("/study", wkHandler.Study)
|
||||||
v2.POST("/record", wkHandler.AllRecord)
|
v2.POST("/record", wkHandler.AllRecord)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user