diff --git a/config.yaml b/config.yaml index 603b842..d320b74 100644 --- a/config.yaml +++ b/config.yaml @@ -6,8 +6,8 @@ database: port: 3306 params: "charset=utf8mb4&parseTime=True&loc=Local" redis: - host: "localhost" - port: 30079 + host: "43.142.81.151" + port: 6379 username: "default" password: "" jwtsecret: "clka1af83af15vhyt8s652avre" diff --git a/controllers/article/getarticle.go b/controllers/article/getarticle.go index 6dfb3a4..f9d2917 100644 --- a/controllers/article/getarticle.go +++ b/controllers/article/getarticle.go @@ -49,6 +49,7 @@ type ArticleResponse struct { EndTime string `json:"end_time"` IsEnded bool `json:"is_ended"` PublisherID string `json:"publisher_id"` + PublisherName string `json:"publisher_name"` CreateTime string `json:"create_time"` Options []ArticleOptionResp `json:"options"` UserHasVoted bool `json:"user_has_voted"` @@ -64,7 +65,7 @@ type UserReq struct { func ArticleListget(c *gin.Context) { var req UserReq - // 1. 解析请求参数,uid为空也能通过校验 + // 1. 解析请求参数 if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "error": "参数解析失败", @@ -86,13 +87,45 @@ func ArticleListget(c *gin.Context) { return } - // 3. 根据uid是否存在(包括空字符串)决定是否查询投票记录 - userVotesMap := make(map[int64][]int64) - // 判断uid是否有效(非空字符串) - uidValid := req.Uid != "" + // 3. 批量查询所有发布者的用户名(核心修改:获取uid对应的username) + // 3.1 收集所有发布者的uid(去重) + uniquePublishUids := make(map[string]struct{}) + for _, article := range articles { + uniquePublishUids[article.PublishUserID] = struct{}{} + } + // 转换为切片便于查询 + publishUidList := make([]string, 0, len(uniquePublishUids)) + for uid := range uniquePublishUids { + publishUidList = append(publishUidList, uid) + } + // 3.2 从用户表查询这些uid对应的用户名(假设用户表为`users`,字段为`user_id`和`username`) + type userInfo struct { + UserID string `gorm:"column:uid"` // 对应发布者的PublishUserID + Username string `gorm:"column:username"` // 发布者用户名 + } + var publishUsers []userInfo + if len(publishUidList) > 0 { + if err := databaseInit.UserDB.Table("user_info"). + Where("uid IN (?)", publishUidList). + Find(&publishUsers).Error; err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": "查询发布者信息失败: " + err.Error(), + }) + return + } + } + + // 3.3 构建uid到username的映射(方便快速查询) + uidToUsername := make(map[string]string) + for _, user := range publishUsers { + uidToUsername[user.UserID] = user.Username + } + + // 4. 查询用户投票记录(原有逻辑,保持不变) + userVotesMap := make(map[int64][]int64) + uidValid := req.Uid != "" if uidValid { - // 3.1 仅当uid有效时查询用户投票记录 var userAllVotes []UserVote if err := databaseInit.UserDB.Table("user_votes"). Where("user_id = ?", req.Uid). @@ -102,33 +135,29 @@ func ArticleListget(c *gin.Context) { }) return } - // 构建投票记录映射 for _, vote := range userAllVotes { userVotesMap[vote.VoteArticleID] = append(userVotesMap[vote.VoteArticleID], vote.OptionID) } } - // 3.2 当uid为空时,不执行任何投票查询,保持映射为空 - // 4. 组装响应数据 + // 5. 组装响应数据(添加用户名) var responseList []ArticleResponse for _, article := range articles { - // 处理投票状态 + // 5.1 处理投票状态(原有逻辑) var votedOptionIDs []int64 var userHasVoted bool if uidValid { votedOptionIDs, userHasVoted = userVotesMap[article.ArticleID] } else { - // uid为空时,默认未投票 votedOptionIDs = []int64{} userHasVoted = false } - // 处理选项列表 + // 5.2 处理选项列表(原有逻辑) var optionList []ArticleOptionResp for _, opt := range article.Options { isVoted := false if uidValid { - // 仅在uid有效时检查是否投票 for _, votedID := range votedOptionIDs { if opt.ID == votedID { isVoted = true @@ -136,7 +165,6 @@ func ArticleListget(c *gin.Context) { } } } - // uid为空时isVoted保持false optionList = append(optionList, ArticleOptionResp{ ID: opt.ID, Name: opt.OptionContent, @@ -145,7 +173,13 @@ func ArticleListget(c *gin.Context) { }) } - // 组装文章响应 + // 5.3 获取发布者用户名(核心修改:从映射中查询) + publisherName := uidToUsername[article.PublishUserID] + if publisherName == "" { + publisherName = "未知用户" // 处理用户不存在的情况 + } + + // 5.4 组装最终响应 articleData := ArticleResponse{ ID: article.ArticleID, Title: article.Title, @@ -154,6 +188,7 @@ func ArticleListget(c *gin.Context) { EndTime: article.EndTime.Format("2006-01-02 15:04:05"), IsEnded: article.IsEnded, PublisherID: article.PublishUserID, + PublisherName: publisherName, // 填充用户名 CreateTime: article.CreateTime.Format("2006-01-02 15:04:05"), Options: optionList, UserHasVoted: userHasVoted, @@ -162,7 +197,7 @@ func ArticleListget(c *gin.Context) { responseList = append(responseList, articleData) } - // 5. 返回响应 + // 6. 返回响应 c.JSON(http.StatusOK, gin.H{ "success": true, "message": fmt.Sprintf("查询成功,共 %d 篇文章", len(responseList)), diff --git a/controllers/ping/getping.go b/controllers/ping/getping.go new file mode 100644 index 0000000..79976cb --- /dev/null +++ b/controllers/ping/getping.go @@ -0,0 +1,7 @@ +package ping + +import "github.com/gin-gonic/gin" + +func GetPing(c *gin.Context) { + c.JSON(200, gin.H{"message": "pong"}) +} diff --git a/opt/ttk/ttk-darwin-amd64.dmg b/opt/ttk/ttk-darwin-amd64.dmg index 9fa3201..15c7be2 100644 Binary files a/opt/ttk/ttk-darwin-amd64.dmg and b/opt/ttk/ttk-darwin-amd64.dmg differ diff --git a/opt/ttk/ttk-linux-amd64 b/opt/ttk/ttk-linux-amd64 index 12f179e..6ecd45a 100644 Binary files a/opt/ttk/ttk-linux-amd64 and b/opt/ttk/ttk-linux-amd64 differ diff --git a/router/setupRouter.go b/router/setupRouter.go index 61939b7..3005226 100644 --- a/router/setupRouter.go +++ b/router/setupRouter.go @@ -9,6 +9,7 @@ import ( "toutoukan/controllers/goods" "toutoukan/controllers/kills" "toutoukan/controllers/notifications/getnotifications" + "toutoukan/controllers/ping" "toutoukan/controllers/search" "toutoukan/controllers/system" "toutoukan/controllers/user" @@ -68,6 +69,7 @@ func SetupRouter() *gin.Engine { { fileGroup.POST("/upload", file.PicUpload) } + r.GET("/ping", ping.GetPing) return r }