diff --git a/internal/app/api/v1/user.go b/internal/app/api/v1/user.go index 5adc0f7..af62662 100644 --- a/internal/app/api/v1/user.go +++ b/internal/app/api/v1/user.go @@ -58,11 +58,12 @@ func GetTwitterInfo(c *gin.Context) { // GetUserInfo Get User Info func GetUserInfo(c *gin.Context) { address := c.Param("address") + LoginAddress := c.GetString("address") if address == "" { FailWithMessage(GetMessage(c, "ParameterError"), c) return } - if list, err := srv.GetUserInfo(address); err != nil { + if list, err := srv.GetUserInfo(address, LoginAddress); err != nil { OkWithData(list, c) } else { OkWithData(list, c) diff --git a/internal/app/api/v2/open_quest.go b/internal/app/api/v2/open_quest.go index 57ca851..a9ab518 100644 --- a/internal/app/api/v2/open_quest.go +++ b/internal/app/api/v2/open_quest.go @@ -43,7 +43,8 @@ func GetUserOpenQuestDetailList(c *gin.Context) { return } address := c.GetString("address") - if list, total, err := srv.GetUserOpenQuestDetailListV2(address, r); err != nil { + LoginAddress := c.GetString("address") + if list, total, err := srv.GetUserOpenQuestDetailListV2(address, LoginAddress, r); err != nil { FailWithMessage("获取失败:"+err.Error(), c) } else { OkWithDetailed(response.PageResult{ diff --git a/internal/app/dao/admin_user.go b/internal/app/dao/admin_user.go new file mode 100644 index 0000000..a5bc16f --- /dev/null +++ b/internal/app/dao/admin_user.go @@ -0,0 +1,16 @@ +package dao + +import ( + "backend-go/internal/app/model" +) + +func (d *Dao) IsAdmin(address string) (is bool, err error) { + var count int64 + if err = d.db.Model(&model.AdminUser{}).Where("address = ?", address).Count(&count).Error; err != nil { + return false, err + } + if count == 0 { + return false, nil + } + return true, nil +} diff --git a/internal/app/dao/tag.go b/internal/app/dao/tag.go new file mode 100644 index 0000000..5ebf32c --- /dev/null +++ b/internal/app/dao/tag.go @@ -0,0 +1,32 @@ +package dao + +import "backend-go/internal/app/model" + +func (d *Dao) GetUserTags(userID uint) (tags []string, err error) { + err = d.db.Model(&model.UsersTag{}).Select("tag.name"). + Joins("join tag on users_tag.tag_id = tag.id"). + Where("users_tag.user_id = ?", userID). + Find(&tags).Error + return +} + +func (d *Dao) GetUserNameTagsByAddress(address string) (nickname, name string, tags []string, err error) { + var user model.Users + err = d.db.Model(&model.Users{}). + Where("address = ?", address). + First(&user).Error + if err != nil { + return + } + if user.Name != nil { + name = *user.Name + } + if user.NickName != nil { + nickname = *user.NickName + } + err = d.db.Model(&model.UsersTag{}).Select("tag.name"). + Joins("join tag on users_tag.tag_id = tag.id"). + Where("users_tag.user_id = ?", user.ID). + Find(&tags).Error + return +} diff --git a/internal/app/model/response/open_quest.go b/internal/app/model/response/open_quest.go index c2fd8b4..74caffd 100644 --- a/internal/app/model/response/open_quest.go +++ b/internal/app/model/response/open_quest.go @@ -26,6 +26,7 @@ type UserOpenQuestJsonElements struct { PassScore int64 `gorm:"column:pass_score" form:"pass_score" json:"pass_score"` // 通过分数 TotalScore int64 `gorm:"column:total_score" form:"total_score" json:"total_score"` // 总分 UserScore int64 `gorm:"column:user_score" form:"user_score" json:"user_score"` // 用户分数 + NickName *string `gorm:"column:nickname;type:varchar(200);default:''" json:"nickname" form:"nickname"` } type UserOpenQuestJsonElementsV2 struct { diff --git a/internal/app/model/tag.go b/internal/app/model/tag.go new file mode 100644 index 0000000..4498cfd --- /dev/null +++ b/internal/app/model/tag.go @@ -0,0 +1,13 @@ +package model + +import "time" + +type Tag struct { + ID uint `gorm:"primarykey" json:"id"` + CreatedAt time.Time `json:"createdAt"` + Name string `gorm:"column:name;unique;type:varchar(100);comment:标签名" json:"name"` +} + +func (Tag) TableName() string { + return "tag" +} diff --git a/internal/app/model/users.go b/internal/app/model/users.go index f214888..2a45469 100644 --- a/internal/app/model/users.go +++ b/internal/app/model/users.go @@ -15,4 +15,5 @@ type Users struct { Socials datatypes.JSON `gorm:"column:socials" json:"socials"` ResourceTime time.Time `gorm:"column:resource_time"` ParticleUserinfo datatypes.JSON `gorm:"column:particle_userinfo" json:"particle_userinfo"` + Name *string `gorm:"column:name;type:varchar(200);comment:用户名称;default:''" json:"name" form:"name"` } diff --git a/internal/app/model/users_tag.go b/internal/app/model/users_tag.go new file mode 100644 index 0000000..333b646 --- /dev/null +++ b/internal/app/model/users_tag.go @@ -0,0 +1,11 @@ +package model + +type UsersTag struct { + ID uint `gorm:"primarykey" json:"id"` // 主键ID + UserID uint `gorm:"column:user_id;comment:用户ID;index:user_id_tag_id,unique" json:"userID"` // 用户ID + TagID uint `gorm:"column:tag_id;comment:标签ID;index:user_id_tag_id,unique" json:"tagID"` // 标签ID +} + +func (UsersTag) TableName() string { + return "users_tag" +} diff --git a/internal/app/service/open_quest_v2.go b/internal/app/service/open_quest_v2.go index 94e8245..b27146e 100644 --- a/internal/app/service/open_quest_v2.go +++ b/internal/app/service/open_quest_v2.go @@ -124,10 +124,15 @@ func (s *Service) GetUserOpenQuestListV2(address string, r request.GetUserOpenQu } // GetUserOpenQuestDetailListV2 获取用户开放题详情 -func (s *Service) GetUserOpenQuestDetailListV2(address string, r request.GetUserOpenQuestDetailListRequest) (list []response.UserOpenQuestJsonElements, total int64, err error) { +func (s *Service) GetUserOpenQuestDetailListV2(address string, loginAddress string, r request.GetUserOpenQuestDetailListRequest) (list []response.UserOpenQuestJsonElements, total int64, err error) { offset := (r.Page - 1) * r.PageSize limit := r.PageSize db := s.dao.DB().Model(&model.UserOpenQuest{}) + // 是否管理员 + isAdmin, err := s.dao.IsAdmin(loginAddress) + if err != nil { + return + } // OpenQuestReviewStatus 1 未审核 2 已审核 countSQL := ` SELECT @@ -205,6 +210,24 @@ func (s *Service) GetUserOpenQuestDetailListV2(address string, r request.GetUser log.Errorv("AnswerCheck error", zap.Error(err)) return } + var showStr string + showStr = fmt.Sprintf("%s...%s", list[i].Address[:6], list[i].Address[len(list[i].Address)-4:]) + if isAdmin { + // 显示标签 + nickname, name, tags, err := s.dao.GetUserNameTagsByAddress(list[i].Address) + if err == nil { + if nickname != "" { + showStr = nickname + } + if name != "" { + showStr += "-" + name + } + for i := 0; i < len(tags); i++ { + showStr += "," + tags[i] + } + } + } + list[i].NickName = &showStr } return } diff --git a/internal/app/service/user.go b/internal/app/service/user.go index 194e279..84a8b8b 100644 --- a/internal/app/service/user.go +++ b/internal/app/service/user.go @@ -20,11 +20,36 @@ import ( "strings" ) -func (s *Service) GetUserInfo(address string) (res interface{}, err error) { +func (s *Service) GetUserInfo(address string, loginAddress string) (res interface{}, err error) { var user model.Users if user, err = s.dao.GetUser(address); err != nil { return } + // 是否管理员 + isAdmin, err := s.dao.IsAdmin(loginAddress) + if err != nil { + return + } + // 管理员显示 + if isAdmin { + var showStr string + showStr = fmt.Sprintf("%s...%s", address[:6], address[len(address)-4:]) + if user.NickName != nil && *user.NickName != "" { + showStr = *user.NickName + } + if user.Name != nil && *user.Name != "" { + showStr += "-" + *user.Name + } + // 显示标签 + tags, err := s.dao.GetUserTags(user.ID) + if err == nil { + for i := 0; i < len(tags); i++ { + showStr += "," + tags[i] + } + } + user.NickName = &showStr + return user, err + } // default nickname if user.NickName == nil || *user.NickName == "" { if len(user.Address) > 10 {