From 1beeaa9aa9e8b21215864ae25e9e81dfb3fbd8f3 Mon Sep 17 00:00:00 2001 From: ckappgit Date: Sun, 14 Jul 2024 22:34:28 +0800 Subject: [PATCH 1/5] feat:add update user data api --- go.mod | 1 + go.sum | 2 + internal/common/dao/user.go | 10 +++++ internal/router/api/v1/api.go | 4 ++ internal/service/user/chagedata.go | 66 ++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+) create mode 100644 internal/service/user/chagedata.go diff --git a/go.mod b/go.mod index ee9ffe3..a4aa3ad 100644 --- a/go.mod +++ b/go.mod @@ -67,6 +67,7 @@ require ( github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect github.com/jackc/pgx/v5 v5.5.5 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect + github.com/jinzhu/copier v0.4.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect diff --git a/go.sum b/go.sum index 3767623..749cb09 100644 --- a/go.sum +++ b/go.sum @@ -115,6 +115,8 @@ github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jellydator/ttlcache/v2 v2.11.1 h1:AZGME43Eh2Vv3giG6GeqeLeFXxwxn1/qHItqWZl6U64= github.com/jellydator/ttlcache/v2 v2.11.1/go.mod h1:RtE5Snf0/57e+2cLWFYWCCsLas2Hy3c5Z4n14XmSvTI= +github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8= +github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= diff --git a/internal/common/dao/user.go b/internal/common/dao/user.go index 2f922c4..4be467e 100644 --- a/internal/common/dao/user.go +++ b/internal/common/dao/user.go @@ -27,6 +27,16 @@ func SetUserPassword(user *model.User, newpass string) (err error) { return err } +// UpdateUserData 根据map更新用户信息 +func UpdateUserData(user *model.User, maps map[string]interface{}) (err error) { + u := query.User + _, err = u.Where(u.UserID.Eq(user.UserID)).Updates(maps) + if err != nil { + return err + } + return err +} + // GetUserByEmail 根据 email 获取用户 func GetUserByEmail(email string) (user *model.User, err error) { q := query.User diff --git a/internal/router/api/v1/api.go b/internal/router/api/v1/api.go index 95817e6..7ab6ff3 100644 --- a/internal/router/api/v1/api.go +++ b/internal/router/api/v1/api.go @@ -57,6 +57,10 @@ func NewAPI() *gin.Engine { jwt.RequireAuth(cache.Clients[cache.JWTBlacklist], true), // 把 token 拉黑 user_service.ResetPassword) } + // 更新用户信息 + user.POST("profile/update", + jwt.RequireAuth(cache.Clients[cache.JWTBlacklist], false), + user_service.ChangeUser) } return r diff --git a/internal/service/user/chagedata.go b/internal/service/user/chagedata.go new file mode 100644 index 0000000..137fa19 --- /dev/null +++ b/internal/service/user/chagedata.go @@ -0,0 +1,66 @@ +package user + +import ( + "github.com/TensoRaws/NuxBT-Backend/internal/common/dao" + "github.com/TensoRaws/NuxBT-Backend/module/util" + "github.com/gin-gonic/gin" +) + +type ChangeUserRequest struct { + Avatar string `json:"avatar" ` + Background string `json:"background" ` + Email string `json:"email" ` + Private int `json:"private" ` + Signature string `json:"signature" ` + Username string `json:"username"` +} + +// ChangeUser 用户信息更新 (POST /profile/update) +func ChangeUser(c *gin.Context) { + // 参数绑定 + var req ChangeUserRequest + if err := c.ShouldBindJSON(&req); err != nil { + util.AbortWithMsg(c, "invalid request") + return + } + + userID, err := util.GetUserIDFromGinContext(c) + if err != nil { + util.AbortWithMsg(c, "Please login first") + return + } + + user, err := dao.GetUserByID(int32(userID)) + if err != nil { + util.AbortWithMsg(c, "User not found") + return + } + // 迁移数据 + updates := make(map[string]interface{}) + if req.Private == 1 { + updates["private"] = true + } else if req.Private == 2 { + updates["private"] = false + } else if req.Private != 0 { + util.AbortWithMsg(c, "invalid request") + return + } + if req.Username != "" { + updates["username"] = req.Username + } + if req.Email != "" { + updates["email"] = req.Email + } + if req.Avatar != "" { + updates["avatar"] = req.Avatar + } + if req.Signature != "" { + updates["signature"] = req.Signature + } + if req.Background != "" { + updates["background"] = req.Background + } + // 执行更新 + dao.UpdateUserData(user, updates) + util.OKWithMsg(c, "update success") +} From 44761bf1b3c79d860cdcaddd40f349e5061acda8 Mon Sep 17 00:00:00 2001 From: Tohrusky <65994850+Tohrusky@users.noreply.github.com> Date: Mon, 15 Jul 2024 01:13:27 +0800 Subject: [PATCH 2/5] fix: update user info --- internal/common/dao/user.go | 12 ++--- internal/router/api/v1/api.go | 17 +++--- internal/service/user/chagedata.go | 66 ----------------------- internal/service/user/login.go | 2 +- internal/service/user/profile.go | 14 ++--- internal/service/user/profile_update.go | 71 +++++++++++++++++++++++++ internal/service/user/register.go | 16 +++--- internal/service/user/reset.go | 2 +- module/util/username.go | 25 +++++++++ 9 files changed, 126 insertions(+), 99 deletions(-) delete mode 100644 internal/service/user/chagedata.go create mode 100644 internal/service/user/profile_update.go create mode 100644 module/util/username.go diff --git a/internal/common/dao/user.go b/internal/common/dao/user.go index 4be467e..e58755d 100644 --- a/internal/common/dao/user.go +++ b/internal/common/dao/user.go @@ -13,10 +13,10 @@ func CreateUser(user *model.User) (err error) { return err } -// SetUserPassword 修改用户密码 -func SetUserPassword(user *model.User, newpass string) (err error) { +// SetUserPassword 设置用户密码 +func SetUserPassword(user *model.User, newPassword string) (err error) { u := query.User - password, err := bcrypt.GenerateFromPassword([]byte(newpass), bcrypt.DefaultCost) + password, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost) if err != nil { return err } @@ -27,10 +27,10 @@ func SetUserPassword(user *model.User, newpass string) (err error) { return err } -// UpdateUserData 根据map更新用户信息 -func UpdateUserData(user *model.User, maps map[string]interface{}) (err error) { +// UpdateUserDataByUserID 根据 map 更新用户信息,map 中的 key 为字段名 +func UpdateUserDataByUserID(userID int32, maps map[string]interface{}) (err error) { u := query.User - _, err = u.Where(u.UserID.Eq(user.UserID)).Updates(maps) + _, err = u.Where(u.UserID.Eq(userID)).Updates(maps) if err != nil { return err } diff --git a/internal/router/api/v1/api.go b/internal/router/api/v1/api.go index 7ab6ff3..fca2a86 100644 --- a/internal/router/api/v1/api.go +++ b/internal/router/api/v1/api.go @@ -40,6 +40,10 @@ func NewAPI() *gin.Engine { jwt.RequireAuth(cache.Clients[cache.JWTBlacklist], true), // 把 token 拉黑 user_service.Logout, ) + // 修改密码 + user.POST("password/reset", + jwt.RequireAuth(cache.Clients[cache.JWTBlacklist], false), + user_service.ResetPassword) // 用户信息 user.GET("profile/me", jwt.RequireAuth(cache.Clients[cache.JWTBlacklist], false), @@ -51,16 +55,11 @@ func NewAPI() *gin.Engine { middleware_cache.Response(cache.Clients[cache.RespCache], 1*time.Minute), user_service.ProfileOthers, ) - - // 修改密码 - user.POST("password/reset", - jwt.RequireAuth(cache.Clients[cache.JWTBlacklist], true), // 把 token 拉黑 - user_service.ResetPassword) + // 用户信息更新 + user.POST("profile/update", + jwt.RequireAuth(cache.Clients[cache.JWTBlacklist], false), + user_service.ProfileUpdate) } - // 更新用户信息 - user.POST("profile/update", - jwt.RequireAuth(cache.Clients[cache.JWTBlacklist], false), - user_service.ChangeUser) } return r diff --git a/internal/service/user/chagedata.go b/internal/service/user/chagedata.go deleted file mode 100644 index 137fa19..0000000 --- a/internal/service/user/chagedata.go +++ /dev/null @@ -1,66 +0,0 @@ -package user - -import ( - "github.com/TensoRaws/NuxBT-Backend/internal/common/dao" - "github.com/TensoRaws/NuxBT-Backend/module/util" - "github.com/gin-gonic/gin" -) - -type ChangeUserRequest struct { - Avatar string `json:"avatar" ` - Background string `json:"background" ` - Email string `json:"email" ` - Private int `json:"private" ` - Signature string `json:"signature" ` - Username string `json:"username"` -} - -// ChangeUser 用户信息更新 (POST /profile/update) -func ChangeUser(c *gin.Context) { - // 参数绑定 - var req ChangeUserRequest - if err := c.ShouldBindJSON(&req); err != nil { - util.AbortWithMsg(c, "invalid request") - return - } - - userID, err := util.GetUserIDFromGinContext(c) - if err != nil { - util.AbortWithMsg(c, "Please login first") - return - } - - user, err := dao.GetUserByID(int32(userID)) - if err != nil { - util.AbortWithMsg(c, "User not found") - return - } - // 迁移数据 - updates := make(map[string]interface{}) - if req.Private == 1 { - updates["private"] = true - } else if req.Private == 2 { - updates["private"] = false - } else if req.Private != 0 { - util.AbortWithMsg(c, "invalid request") - return - } - if req.Username != "" { - updates["username"] = req.Username - } - if req.Email != "" { - updates["email"] = req.Email - } - if req.Avatar != "" { - updates["avatar"] = req.Avatar - } - if req.Signature != "" { - updates["signature"] = req.Signature - } - if req.Background != "" { - updates["background"] = req.Background - } - // 执行更新 - dao.UpdateUserData(user, updates) - util.OKWithMsg(c, "update success") -} diff --git a/internal/service/user/login.go b/internal/service/user/login.go index d77c523..05bfc87 100644 --- a/internal/service/user/login.go +++ b/internal/service/user/login.go @@ -21,7 +21,7 @@ type LoginResponse struct { func Login(c *gin.Context) { var req LoginRequest if err := c.ShouldBindJSON(&req); err != nil { - util.AbortWithMsg(c, "invalid request") + util.AbortWithMsg(c, "invalid request: "+err.Error()) return } diff --git a/internal/service/user/profile.go b/internal/service/user/profile.go index 977268a..59627c7 100644 --- a/internal/service/user/profile.go +++ b/internal/service/user/profile.go @@ -18,7 +18,7 @@ type ProfileResponse struct { Inviter string `json:"inviter"` LastActive string `json:"last_active"` Private bool `json:"private"` - Roles []string `json:"roles,omitempty"` + Roles []string `json:"roles"` Signature string `json:"signature"` UserID int32 `json:"user_id"` Username string `json:"username"` @@ -71,7 +71,7 @@ func ProfileOthers(c *gin.Context) { // 绑定参数 var req ProfileOthersRequest if err := c.ShouldBindQuery(&req); err != nil { - util.AbortWithMsg(c, "invalid request") + util.AbortWithMsg(c, "invalid request: "+err.Error()) return } // 鉴权 @@ -80,12 +80,6 @@ func ProfileOthers(c *gin.Context) { util.AbortWithMsg(c, "Please login first") return } - // 仅用于鉴权不使用 - _, err = dao.GetUserByID(userID) - if err != nil { - util.AbortWithMsg(c, "User not found") - return - } // 获取信息 user, err := dao.GetUserByID(req.UserId) if err != nil { @@ -109,7 +103,7 @@ func ProfileOthers(c *gin.Context) { Experience: "", Inviter: "", LastActive: "", - Private: user.Private, + Private: true, Roles: nil, Signature: "", UserID: user.UserID, @@ -125,7 +119,7 @@ func ProfileOthers(c *gin.Context) { 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, + Private: false, Roles: roles, Signature: user.Signature, UserID: user.UserID, diff --git a/internal/service/user/profile_update.go b/internal/service/user/profile_update.go new file mode 100644 index 0000000..27ccae4 --- /dev/null +++ b/internal/service/user/profile_update.go @@ -0,0 +1,71 @@ +package user + +import ( + "github.com/TensoRaws/NuxBT-Backend/internal/common/dao" + "github.com/TensoRaws/NuxBT-Backend/module/util" + "github.com/gin-gonic/gin" +) + +type ProfileUpdateRequest struct { + Avatar *string `json:"avatar" binding:"omitempty"` + Background *string `json:"background" binding:"omitempty"` + Email *string `json:"email" binding:"omitempty,email"` + Private *bool `json:"private" binding:"omitempty"` + Signature *string `json:"signature" binding:"omitempty"` + Username *string `json:"username" binding:"omitempty"` +} + +// ProfileUpdate 用户信息更新 (POST /profile/update) +func ProfileUpdate(c *gin.Context) { + // 参数绑定 + var req ProfileUpdateRequest + if err := c.ShouldBindJSON(&req); err != nil { + util.AbortWithMsg(c, "invalid request: "+err.Error()) + return + } + + userID, err := util.GetUserIDFromGinContext(c) + if err != nil { + util.AbortWithMsg(c, "Please login first") + return + } + // 准备更新数据 + updates := make(map[string]interface{}) + + if req.Private != nil { + updates["private"] = req.Private + } + + if req.Username != nil && *req.Username != "" { + err = util.CheckUsername(*req.Username) + if err != nil { + util.AbortWithMsg(c, "invalid username: "+err.Error()) + return + } + updates["username"] = *req.Username + } + + if req.Email != nil { + updates["email"] = *req.Email + } + + if req.Avatar != nil { + updates["avatar"] = *req.Avatar + } + + if req.Signature != nil { + updates["signature"] = *req.Signature + } + + if req.Background != nil { + updates["background"] = *req.Background + } + // 执行更新 + err = dao.UpdateUserDataByUserID(userID, updates) + if err != nil { + util.AbortWithMsg(c, "update failed: "+err.Error()) + return + } + + util.OKWithMsg(c, "update success") +} diff --git a/internal/service/user/register.go b/internal/service/user/register.go index a072bed..bc0d33f 100644 --- a/internal/service/user/register.go +++ b/internal/service/user/register.go @@ -31,7 +31,13 @@ type RegisterDataResponse struct { func Register(c *gin.Context) { var req RegisterRequest if err := c.ShouldBindJSON(&req); err != nil { - util.AbortWithMsg(c, "invalid request") + util.AbortWithMsg(c, "invalid request: "+err.Error()) + return + } + + err := util.CheckUsername(req.Username) + if err != nil { + util.AbortWithMsg(c, "invalid username: "+err.Error()) return } @@ -42,10 +48,8 @@ func Register(c *gin.Context) { return } } else { - // 有邀请码注册,检查邀请码是否有效 - // do something - // 未实现 - // OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO + // TODO: 邀请码功能, 有邀请码注册,检查邀请码是否有效 + log.Logger.Info("invitation code: " + *req.InvitationCode) } password, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost) @@ -62,7 +66,7 @@ func Register(c *gin.Context) { LastActive: time.Now(), }) if err != nil { - util.AbortWithMsg(c, "failed to register: ") + util.AbortWithMsg(c, "failed to register: "+err.Error()) log.Logger.Error("failed to register: " + err.Error()) return } diff --git a/internal/service/user/reset.go b/internal/service/user/reset.go index 4f07d2b..4137a46 100644 --- a/internal/service/user/reset.go +++ b/internal/service/user/reset.go @@ -16,7 +16,7 @@ func ResetPassword(c *gin.Context) { // 绑定参数 var req ResetPasswordRequest if err := c.ShouldBindJSON(&req); err != nil { - util.AbortWithMsg(c, "invalid request") + util.AbortWithMsg(c, "invalid request: "+err.Error()) return } diff --git a/module/util/username.go b/module/util/username.go new file mode 100644 index 0000000..ce1ce67 --- /dev/null +++ b/module/util/username.go @@ -0,0 +1,25 @@ +package util + +import "fmt" + +func CheckUsername(username string) error { + l := len([]rune(username)) + if l > 20 { + return fmt.Errorf("username too long") + } + if l < 2 { + return fmt.Errorf("username too short") + } + if username == "" { + return fmt.Errorf("username cannot be empty") + } + adminWords := []string{"admin", "root", "administrator", "管理员", "超级管理员", "版主", "站长", "moderator"} + for _, word := range adminWords { + if username == word { + return fmt.Errorf("username cannot be %s", word) + } + } + // TODO: more checks,如检查敏感词 + + return nil +} From 97d46e1a37a7064d64e7b775a41a012d9d0da4a0 Mon Sep 17 00:00:00 2001 From: Tohrusky <65994850+Tohrusky@users.noreply.github.com> Date: Mon, 15 Jul 2024 01:44:27 +0800 Subject: [PATCH 3/5] fix: update user info --- internal/service/user/logout.go | 8 ++------ internal/service/user/profile.go | 24 ++++++++++-------------- internal/service/user/profile_update.go | 16 +++++++++------- internal/service/user/reset.go | 7 +------ 4 files changed, 22 insertions(+), 33 deletions(-) diff --git a/internal/service/user/logout.go b/internal/service/user/logout.go index 8256c8a..1e547fc 100644 --- a/internal/service/user/logout.go +++ b/internal/service/user/logout.go @@ -8,13 +8,9 @@ import ( // Logout 用户登出 (POST /logout) func Logout(c *gin.Context) { - user, err := util.GetUserIDFromGinContext(c) - if err != nil { - util.AbortWithMsg(c, "Please login first") - return - } + userID, _ := util.GetUserIDFromGinContext(c) util.OKWithMsg(c, "Logout success") - log.Logger.Info("Logout success: " + util.StructToString(user)) + log.Logger.Info("Logout success, user ID: " + util.StructToString(userID)) } diff --git a/internal/service/user/profile.go b/internal/service/user/profile.go index 59627c7..a53ab7f 100644 --- a/internal/service/user/profile.go +++ b/internal/service/user/profile.go @@ -25,16 +25,12 @@ type ProfileResponse struct { } type ProfileOthersRequest struct { - UserId int32 `form:"user_id" binding:"required"` + UserID int32 `form:"user_id" binding:"required"` } // ProfileMe 获取用户自己的信息 (GET /profile/me) func ProfileMe(c *gin.Context) { - userID, err := util.GetUserIDFromGinContext(c) - if err != nil { - util.AbortWithMsg(c, "Please login first") - return - } + userID, _ := util.GetUserIDFromGinContext(c) user, err := dao.GetUserByID(userID) if err != nil { @@ -74,20 +70,17 @@ func ProfileOthers(c *gin.Context) { util.AbortWithMsg(c, "invalid request: "+err.Error()) return } - // 鉴权 - userID, err := util.GetUserIDFromGinContext(c) - if err != nil { - util.AbortWithMsg(c, "Please login first") - return - } + + userID, _ := util.GetUserIDFromGinContext(c) + // 获取信息 - user, err := dao.GetUserByID(req.UserId) + user, err := dao.GetUserByID(req.UserID) if err != nil { util.AbortWithMsg(c, "User not found") return } - roles, err := dao.GetUserRolesByID(userID) + roles, err := dao.GetUserRolesByID(req.UserID) if err != nil { log.Logger.Info("Failed to get user roles: " + err.Error()) roles = []string{} @@ -126,4 +119,7 @@ func ProfileOthers(c *gin.Context) { Username: user.Username, }) } + + log.Logger.Info("Get user profile success: " + strconv.Itoa(int(req.UserID)) + + ", by user ID: " + strconv.Itoa(int(userID))) } diff --git a/internal/service/user/profile_update.go b/internal/service/user/profile_update.go index 27ccae4..f1d1123 100644 --- a/internal/service/user/profile_update.go +++ b/internal/service/user/profile_update.go @@ -1,7 +1,10 @@ package user import ( + "strconv" + "github.com/TensoRaws/NuxBT-Backend/internal/common/dao" + "github.com/TensoRaws/NuxBT-Backend/module/log" "github.com/TensoRaws/NuxBT-Backend/module/util" "github.com/gin-gonic/gin" ) @@ -24,11 +27,8 @@ func ProfileUpdate(c *gin.Context) { return } - userID, err := util.GetUserIDFromGinContext(c) - if err != nil { - util.AbortWithMsg(c, "Please login first") - return - } + userID, _ := util.GetUserIDFromGinContext(c) + // 准备更新数据 updates := make(map[string]interface{}) @@ -37,7 +37,7 @@ func ProfileUpdate(c *gin.Context) { } if req.Username != nil && *req.Username != "" { - err = util.CheckUsername(*req.Username) + err := util.CheckUsername(*req.Username) if err != nil { util.AbortWithMsg(c, "invalid username: "+err.Error()) return @@ -61,11 +61,13 @@ func ProfileUpdate(c *gin.Context) { updates["background"] = *req.Background } // 执行更新 - err = dao.UpdateUserDataByUserID(userID, updates) + err := dao.UpdateUserDataByUserID(userID, updates) if err != nil { util.AbortWithMsg(c, "update failed: "+err.Error()) return } util.OKWithMsg(c, "update success") + + log.Logger.Info("update user profile success: " + strconv.Itoa(int(userID))) } diff --git a/internal/service/user/reset.go b/internal/service/user/reset.go index 4137a46..842a69f 100644 --- a/internal/service/user/reset.go +++ b/internal/service/user/reset.go @@ -20,12 +20,7 @@ func ResetPassword(c *gin.Context) { return } - // 鉴权 - userID, err := util.GetUserIDFromGinContext(c) - if err != nil { - util.AbortWithMsg(c, "Please login first") - return - } + userID, _ := util.GetUserIDFromGinContext(c) user, err := dao.GetUserByID(userID) if err != nil { From 541aa1ba97ee097e498596258a750784f25ed21b Mon Sep 17 00:00:00 2001 From: Tohrusky <65994850+Tohrusky@users.noreply.github.com> Date: Mon, 15 Jul 2024 01:44:52 +0800 Subject: [PATCH 4/5] fix: update user info --- go.mod | 1 - go.sum | 2 -- 2 files changed, 3 deletions(-) diff --git a/go.mod b/go.mod index a4aa3ad..ee9ffe3 100644 --- a/go.mod +++ b/go.mod @@ -67,7 +67,6 @@ require ( github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect github.com/jackc/pgx/v5 v5.5.5 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect - github.com/jinzhu/copier v0.4.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect diff --git a/go.sum b/go.sum index 749cb09..3767623 100644 --- a/go.sum +++ b/go.sum @@ -115,8 +115,6 @@ github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jellydator/ttlcache/v2 v2.11.1 h1:AZGME43Eh2Vv3giG6GeqeLeFXxwxn1/qHItqWZl6U64= github.com/jellydator/ttlcache/v2 v2.11.1/go.mod h1:RtE5Snf0/57e+2cLWFYWCCsLas2Hy3c5Z4n14XmSvTI= -github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8= -github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= From 4b9257a39c3f1ed8ff0ba61c72928b39bc2b772a Mon Sep 17 00:00:00 2001 From: Tohrusky <65994850+Tohrusky@users.noreply.github.com> Date: Mon, 15 Jul 2024 02:07:48 +0800 Subject: [PATCH 5/5] fix: update user info --- internal/service/user/profile.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/internal/service/user/profile.go b/internal/service/user/profile.go index a53ab7f..914f184 100644 --- a/internal/service/user/profile.go +++ b/internal/service/user/profile.go @@ -13,12 +13,12 @@ type ProfileResponse struct { Avatar string `json:"avatar"` Background string `json:"background"` CreatedAt string `json:"created_at"` - Email string `json:"email"` - Experience string `json:"experience"` - Inviter string `json:"inviter"` + Email *string `json:"email,omitempty"` + Experience *int32 `json:"experience,omitempty"` + Inviter *int32 `json:"inviter,omitempty"` LastActive string `json:"last_active"` Private bool `json:"private"` - Roles []string `json:"roles"` + Roles []string `json:"roles,omitempty"` Signature string `json:"signature"` UserID int32 `json:"user_id"` Username string `json:"username"` @@ -48,9 +48,9 @@ func ProfileMe(c *gin.Context) { 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)), + Email: &user.Email, + Experience: &user.Experience, + Inviter: &user.Inviter, LastActive: user.LastActive.Format("2006-01-02 15:04:05"), Private: user.Private, Roles: roles, @@ -91,14 +91,14 @@ func ProfileOthers(c *gin.Context) { util.OKWithData(c, ProfileResponse{ Avatar: user.Avatar, Background: user.Background, - CreatedAt: "", - Email: "", - Experience: "", - Inviter: "", - LastActive: "", + CreatedAt: user.CreatedAt.Format("2006-01-02 15:04:05"), + Email: nil, + Experience: nil, + Inviter: nil, + LastActive: user.LastActive.Format("2006-01-02 15:04:05"), Private: true, Roles: nil, - Signature: "", + Signature: user.Signature, UserID: user.UserID, Username: user.Username, }) @@ -108,9 +108,9 @@ func ProfileOthers(c *gin.Context) { 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)), + Email: &user.Email, + Experience: &user.Experience, + Inviter: &user.Inviter, LastActive: user.LastActive.Format("2006-01-02 15:04:05"), Private: false, Roles: roles,