diff --git a/dao/user.go b/dao/user.go index d7aae5f..7eb25d4 100644 --- a/dao/user.go +++ b/dao/user.go @@ -17,6 +17,7 @@ type User struct { Redis bool `gorm:"column:redis"` Run bool `gorm:"column:run"` Upload bool `gorm:"column:upload"` + Avatar string `gorm:"column:avatar"` CreateTime string `gorm:"column:create_time"` UpdateTime string `gorm:"column:update_time"` } @@ -75,3 +76,7 @@ func FindUserByEmail(email string) User { func UpdateUserByID(id int, name, password, email string) { DB.Model(&User{}).Where("id = ?", id).Updates(User{Name: name, Password: password, Email: email}) } + +func UpdateUserByID2(id int, req proto.UpdateUserInfoReq) { + DB.Model(&User{}).Where("id = ?", id).Updates(User{Name: req.Username, Age: req.Age, Role: req.Role, Run: req.Run, Redis: req.Redis, Upload: req.Upload, Avatar: req.Avatar}) +} diff --git a/handler/user.go b/handler/user.go index 3f55d78..11023d0 100644 --- a/handler/user.go +++ b/handler/user.go @@ -25,6 +25,7 @@ func SetUpUserGroup(router *gin.Engine) { userGroup.POST("/confirm", ConfirmQRLogin) userGroup.POST("/search", SearchHandler) userGroup.POST("/info", GetUserInfo) + userGroup.POST("/update", UpdateUserInfo) } type RLReq struct { @@ -76,6 +77,24 @@ func GetUserInfo(c *gin.Context) { } } +func UpdateUserInfo(c *gin.Context) { + var req_data proto.UpdateUserInfoReq + id, _ := c.Get("id") + user_id := int(id.(float64)) + if err := c.ShouldBind(&req_data); err == nil { + rid, err2 := service.UpdateUser(user_id, req_data) + if err2 != nil { + c.JSON(200, gin.H{"code": proto.OperationFailed, "message": "failed", "data": "2"}) + return + } + c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": rid}) + } else { + c.JSON(200, gin.H{"code": proto.ParameterError, "message": err, "data": "2"}) + return + } + +} + func GetScanUUID(c *gin.Context) { var ReqData QRReq if err := c.ShouldBind(&ReqData); err != nil { diff --git a/proto/user_req.go b/proto/user_req.go new file mode 100644 index 0000000..be15ea6 --- /dev/null +++ b/proto/user_req.go @@ -0,0 +1,13 @@ +package proto + +type UpdateUserInfoReq struct { + ID int `json:"id" form:"id"` //用户id + Username string `json:"username" form:"username"` //用户名 + Age int `json:"age" form:"age"` //年龄 + Role string `json:"role" form:"role"` //角色 + Gender string `json:"gender" form:"gender"` //性别 + Redis bool `json:"redis" form:"redis"` //是否刷新redis + Upload bool `json:"upload" form:"upload"` //是否上传头像 + Run bool `json:"run" form:"run"` //是否运行 + Avatar string `json:"avatar" form:"avatar"` //头像 +} diff --git a/service/userService.go b/service/userService.go index 3ce3274..1aa6bae 100644 --- a/service/userService.go +++ b/service/userService.go @@ -41,3 +41,16 @@ func GetUserByID(id int) []proto.User { func GetUserByNameLike(name string) []proto.User { return dao.FindUserByNameLike(name) } + +func UpdateUser(user_id int, req proto.UpdateUserInfoReq) (int, error) { + cur_user := dao.FindUserByID2(user_id) + if user_id == req.ID { + dao.UpdateUserByID2(user_id, req) + return user_id, nil + } else if cur_user.Role == "admin" { + dao.UpdateUserByID2(user_id, req) + return req.ID, nil + } else { + return 0, nil + } +}