diff --git a/api/controller/Public.go b/api/controller/Public.go index 7b38ac1..dab5475 100644 --- a/api/controller/Public.go +++ b/api/controller/Public.go @@ -7,6 +7,7 @@ import ( "hepa-calc-api/api/service" "hepa-calc-api/global" "hepa-calc-api/utils" + "strconv" ) type Public struct{} @@ -52,7 +53,29 @@ func (b *Public) GetPhoneCode(c *gin.Context) { // GetIndex 获取首页数据 func (b *Public) GetIndex(c *gin.Context) { - userId := c.GetInt64("UserId") // 用户id + publicRequest := requests.PublicRequest{} + req := publicRequest.GetIndex + if err := c.ShouldBind(&req); err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + // 参数验证 + if err := global.Validate.Struct(req); err != nil { + responses.FailWithMessage(utils.Translate(err), c) + return + } + + var userId int64 + if req.UserId != "" { + // 将 id 转换为 int64 类型 + var err error + userId, err = strconv.ParseInt(req.UserId, 10, 64) + if err != nil { + responses.Fail(c) + return + } + } publicService := service.PublicService{} g, err := publicService.GetIndex(userId) diff --git a/api/requests/Public.go b/api/requests/Public.go index 698a6c4..453fdc8 100644 --- a/api/requests/Public.go +++ b/api/requests/Public.go @@ -3,6 +3,7 @@ package requests type PublicRequest struct { GetPhoneCode // 获取手机验证码 GetOssSign // 获取oss签名 + GetIndex // 获取首页数据 } // GetPhoneCode 获取手机验证码 @@ -15,3 +16,8 @@ type GetPhoneCode struct { type GetOssSign struct { Scene int `json:"scene" form:"scene" validate:"required,oneof=1 2 3 4" label:"场景"` // (1:头像) } + +// GetIndex 获取首页数据 +type GetIndex struct { + UserId string `json:"user_id" form:"user_id" label:"用户标识"` +} diff --git a/api/service/Question.go b/api/service/Question.go index cbc19db..bc7b802 100644 --- a/api/service/Question.go +++ b/api/service/Question.go @@ -2,6 +2,7 @@ package service import ( "errors" + "fmt" "gorm.io/gorm" "hepa-calc-api/api/dao" "hepa-calc-api/api/model" @@ -49,6 +50,7 @@ func (r *QuestionService) GetGuessUserLIkeList(userId int64) (m []*model.Questio var questions []*model.Question + fmt.Println(userId) if userId != 0 { maps := make(map[string]interface{}) maps["user_id"] = userId