文评列表json添加username字段
This commit is contained in:
@@ -6,8 +6,8 @@ database:
|
|||||||
port: 3306
|
port: 3306
|
||||||
params: "charset=utf8mb4&parseTime=True&loc=Local"
|
params: "charset=utf8mb4&parseTime=True&loc=Local"
|
||||||
redis:
|
redis:
|
||||||
host: "localhost"
|
host: "43.142.81.151"
|
||||||
port: 30079
|
port: 6379
|
||||||
username: "default"
|
username: "default"
|
||||||
password: ""
|
password: ""
|
||||||
jwtsecret: "clka1af83af15vhyt8s652avre"
|
jwtsecret: "clka1af83af15vhyt8s652avre"
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ type ArticleResponse struct {
|
|||||||
EndTime string `json:"end_time"`
|
EndTime string `json:"end_time"`
|
||||||
IsEnded bool `json:"is_ended"`
|
IsEnded bool `json:"is_ended"`
|
||||||
PublisherID string `json:"publisher_id"`
|
PublisherID string `json:"publisher_id"`
|
||||||
|
PublisherName string `json:"publisher_name"`
|
||||||
CreateTime string `json:"create_time"`
|
CreateTime string `json:"create_time"`
|
||||||
Options []ArticleOptionResp `json:"options"`
|
Options []ArticleOptionResp `json:"options"`
|
||||||
UserHasVoted bool `json:"user_has_voted"`
|
UserHasVoted bool `json:"user_has_voted"`
|
||||||
@@ -64,7 +65,7 @@ type UserReq struct {
|
|||||||
func ArticleListget(c *gin.Context) {
|
func ArticleListget(c *gin.Context) {
|
||||||
var req UserReq
|
var req UserReq
|
||||||
|
|
||||||
// 1. 解析请求参数,uid为空也能通过校验
|
// 1. 解析请求参数
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
"error": "参数解析失败",
|
"error": "参数解析失败",
|
||||||
@@ -86,13 +87,45 @@ func ArticleListget(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 根据uid是否存在(包括空字符串)决定是否查询投票记录
|
// 3. 批量查询所有发布者的用户名(核心修改:获取uid对应的username)
|
||||||
userVotesMap := make(map[int64][]int64)
|
// 3.1 收集所有发布者的uid(去重)
|
||||||
// 判断uid是否有效(非空字符串)
|
uniquePublishUids := make(map[string]struct{})
|
||||||
uidValid := req.Uid != ""
|
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 {
|
if uidValid {
|
||||||
// 3.1 仅当uid有效时查询用户投票记录
|
|
||||||
var userAllVotes []UserVote
|
var userAllVotes []UserVote
|
||||||
if err := databaseInit.UserDB.Table("user_votes").
|
if err := databaseInit.UserDB.Table("user_votes").
|
||||||
Where("user_id = ?", req.Uid).
|
Where("user_id = ?", req.Uid).
|
||||||
@@ -102,33 +135,29 @@ func ArticleListget(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 构建投票记录映射
|
|
||||||
for _, vote := range userAllVotes {
|
for _, vote := range userAllVotes {
|
||||||
userVotesMap[vote.VoteArticleID] = append(userVotesMap[vote.VoteArticleID], vote.OptionID)
|
userVotesMap[vote.VoteArticleID] = append(userVotesMap[vote.VoteArticleID], vote.OptionID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 3.2 当uid为空时,不执行任何投票查询,保持映射为空
|
|
||||||
|
|
||||||
// 4. 组装响应数据
|
// 5. 组装响应数据(添加用户名)
|
||||||
var responseList []ArticleResponse
|
var responseList []ArticleResponse
|
||||||
for _, article := range articles {
|
for _, article := range articles {
|
||||||
// 处理投票状态
|
// 5.1 处理投票状态(原有逻辑)
|
||||||
var votedOptionIDs []int64
|
var votedOptionIDs []int64
|
||||||
var userHasVoted bool
|
var userHasVoted bool
|
||||||
if uidValid {
|
if uidValid {
|
||||||
votedOptionIDs, userHasVoted = userVotesMap[article.ArticleID]
|
votedOptionIDs, userHasVoted = userVotesMap[article.ArticleID]
|
||||||
} else {
|
} else {
|
||||||
// uid为空时,默认未投票
|
|
||||||
votedOptionIDs = []int64{}
|
votedOptionIDs = []int64{}
|
||||||
userHasVoted = false
|
userHasVoted = false
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理选项列表
|
// 5.2 处理选项列表(原有逻辑)
|
||||||
var optionList []ArticleOptionResp
|
var optionList []ArticleOptionResp
|
||||||
for _, opt := range article.Options {
|
for _, opt := range article.Options {
|
||||||
isVoted := false
|
isVoted := false
|
||||||
if uidValid {
|
if uidValid {
|
||||||
// 仅在uid有效时检查是否投票
|
|
||||||
for _, votedID := range votedOptionIDs {
|
for _, votedID := range votedOptionIDs {
|
||||||
if opt.ID == votedID {
|
if opt.ID == votedID {
|
||||||
isVoted = true
|
isVoted = true
|
||||||
@@ -136,7 +165,6 @@ func ArticleListget(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// uid为空时isVoted保持false
|
|
||||||
optionList = append(optionList, ArticleOptionResp{
|
optionList = append(optionList, ArticleOptionResp{
|
||||||
ID: opt.ID,
|
ID: opt.ID,
|
||||||
Name: opt.OptionContent,
|
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{
|
articleData := ArticleResponse{
|
||||||
ID: article.ArticleID,
|
ID: article.ArticleID,
|
||||||
Title: article.Title,
|
Title: article.Title,
|
||||||
@@ -154,6 +188,7 @@ func ArticleListget(c *gin.Context) {
|
|||||||
EndTime: article.EndTime.Format("2006-01-02 15:04:05"),
|
EndTime: article.EndTime.Format("2006-01-02 15:04:05"),
|
||||||
IsEnded: article.IsEnded,
|
IsEnded: article.IsEnded,
|
||||||
PublisherID: article.PublishUserID,
|
PublisherID: article.PublishUserID,
|
||||||
|
PublisherName: publisherName, // 填充用户名
|
||||||
CreateTime: article.CreateTime.Format("2006-01-02 15:04:05"),
|
CreateTime: article.CreateTime.Format("2006-01-02 15:04:05"),
|
||||||
Options: optionList,
|
Options: optionList,
|
||||||
UserHasVoted: userHasVoted,
|
UserHasVoted: userHasVoted,
|
||||||
@@ -162,7 +197,7 @@ func ArticleListget(c *gin.Context) {
|
|||||||
responseList = append(responseList, articleData)
|
responseList = append(responseList, articleData)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. 返回响应
|
// 6. 返回响应
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": true,
|
"success": true,
|
||||||
"message": fmt.Sprintf("查询成功,共 %d 篇文章", len(responseList)),
|
"message": fmt.Sprintf("查询成功,共 %d 篇文章", len(responseList)),
|
||||||
|
|||||||
7
controllers/ping/getping.go
Normal file
7
controllers/ping/getping.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package ping
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
func GetPing(c *gin.Context) {
|
||||||
|
c.JSON(200, gin.H{"message": "pong"})
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
@@ -9,6 +9,7 @@ import (
|
|||||||
"toutoukan/controllers/goods"
|
"toutoukan/controllers/goods"
|
||||||
"toutoukan/controllers/kills"
|
"toutoukan/controllers/kills"
|
||||||
"toutoukan/controllers/notifications/getnotifications"
|
"toutoukan/controllers/notifications/getnotifications"
|
||||||
|
"toutoukan/controllers/ping"
|
||||||
"toutoukan/controllers/search"
|
"toutoukan/controllers/search"
|
||||||
"toutoukan/controllers/system"
|
"toutoukan/controllers/system"
|
||||||
"toutoukan/controllers/user"
|
"toutoukan/controllers/user"
|
||||||
@@ -68,6 +69,7 @@ func SetupRouter() *gin.Engine {
|
|||||||
{
|
{
|
||||||
fileGroup.POST("/upload", file.PicUpload)
|
fileGroup.POST("/upload", file.PicUpload)
|
||||||
}
|
}
|
||||||
|
r.GET("/ping", ping.GetPing)
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user