Skip to content

Commit

Permalink
feat:Add an api for obtaining other people's Profile (#9)
Browse files Browse the repository at this point in the history
* feat:Add an api for obtaining other people's Profile

* fix:move user_id to int
  • Loading branch information
ckappgit authored Jul 14, 2024
1 parent fa6ba45 commit 2a1edf7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
6 changes: 6 additions & 0 deletions internal/router/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ func NewAPI() *gin.Engine {
jwt.RequireAuth(cache.Clients[cache.JWTBlacklist], false),
user_service.ProfileMe,
)
// 用户查询他人信息
user.GET("profile/",
jwt.RequireAuth(cache.Clients[cache.JWTBlacklist], false),
user_service.ProfileOthers,
)

// 修改密码
user.POST("password/reset",
jwt.RequireAuth(cache.Clients[cache.JWTBlacklist], true), // 把 token 拉黑
Expand Down
81 changes: 77 additions & 4 deletions internal/service/user/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/gin-gonic/gin"
)

type ProfileMeResponse struct {
type ProfileResponse struct {
Avatar string `json:"avatar"`
Background string `json:"background"`
CreatedAt string `json:"created_at"`
Expand All @@ -20,10 +20,14 @@ type ProfileMeResponse struct {
Private bool `json:"private"`
Roles []string `json:"roles,omitempty"`
Signature string `json:"signature"`
UserID string `json:"user_id"`
UserID int `json:"user_id"`
Username string `json:"username"`
}

type UserProfileRequest struct {
UserId int `form:"user_id" binding:"required"`
}

// ProfileMe 获取用户自己的信息 (GET /profile/me)
func ProfileMe(c *gin.Context) {
userID, err := util.GetUserIDFromGinContext(c)
Expand All @@ -44,7 +48,7 @@ func ProfileMe(c *gin.Context) {
roles = []string{}
}

util.OKWithData(c, ProfileMeResponse{
util.OKWithData(c, ProfileResponse{
Avatar: user.Avatar,
Background: user.Background,
CreatedAt: user.CreatedAt.Format("2006-01-02 15:04:05"),
Expand All @@ -55,9 +59,78 @@ func ProfileMe(c *gin.Context) {
Private: user.Private,
Roles: roles,
Signature: user.Signature,
UserID: strconv.Itoa(int(user.UserID)),
UserID: int(user.UserID),
Username: user.Username,
})

log.Logger.Info("get user profile success: " + util.StructToString(user))
}

// ProfileOthers 用户查询他人信息 (GET /profile)
func ProfileOthers(c *gin.Context) {
// 绑定参数
var req UserProfileRequest
if err := c.ShouldBindQuery(&req); err != nil {
util.AbortWithMsg(c, "invalid request")
return
}
// 鉴权
userID, err := util.GetUserIDFromGinContext(c)
if err != nil {
util.AbortWithMsg(c, "Please login first")
return
}
// 仅用于鉴权不使用
_, err = dao.GetUserByID(int32(userID))
if err != nil {
util.AbortWithMsg(c, "User not found")
return
}
// 获取信息
user, err := dao.GetUserByID(int32(req.UserId))
if err != nil {
util.AbortWithMsg(c, "User not found")
return
}

roles, err := dao.GetUserRolesByID(int32(userID))
if err != nil {
log.Logger.Info("Failed to get user roles: " + err.Error())
roles = []string{}
}
// 判断是否为隐私账号 1==1为谷歌编程规范
if user.Private {
// 只显示最基础信息
util.OKWithData(c, ProfileResponse{
Avatar: user.Avatar,
Background: user.Background,
CreatedAt: "",
Email: "",
Experience: "",
Inviter: "",
LastActive: "",
Private: user.Private,
Roles: []string{},
Signature: "",
UserID: int(user.UserID),
Username: user.Username,
})
} else {
// 显示全部信息

util.OKWithData(c, ProfileResponse{
Avatar: user.Avatar,
Background: user.Background,
CreatedAt: user.CreatedAt.Format("2006-01-02 15:04:05"),
Email: user.Email,
Experience: strconv.Itoa(int(user.Experience)),
Inviter: strconv.Itoa(int(user.Inviter)),
LastActive: user.LastActive.Format("2006-01-02 15:04:05"),
Private: user.Private,
Roles: roles,
Signature: user.Signature,
UserID: int(user.UserID),
Username: user.Username,
})
}
}

0 comments on commit 2a1edf7

Please sign in to comment.