diff --git a/api/user.go b/api/user.go index bebaa41..c8c57e8 100644 --- a/api/user.go +++ b/api/user.go @@ -35,6 +35,7 @@ func Login(c *gin.Context) { //code 1用户名或密码错误 err := DB.Take(&user, "username = ?", request.Username).Error + if err != nil { c.JSON(200, gin.H{ "Error": true, @@ -193,7 +194,7 @@ func GetInfoByUserId(c *gin.Context) { return } - err := DB.Debug().Raw("SELECT username,affiliation,country,team_id FROM user WHERE id = ? LIMIT 1", id).Scan(&info).Error + err := DB.Debug().Raw("SELECT username,affiliation,country,team_id,website FROM user WHERE id = ? LIMIT 1", id).Scan(&info).Error //err := DB.Where("id = ?", id).First(user).Error if err != nil { c.JSON(400, gin.H{"code": 400, "msg": "Get info error!"}) @@ -203,6 +204,44 @@ func GetInfoByUserId(c *gin.Context) { } +// UpdateUserInfo 更新用户信息 +func UpdateUserInfo(c *gin.Context) { + var user User + var request UpdateUserInfoRequest + if err := c.ShouldBindJSON(&request); err != nil { + c.JSON(400, gin.H{"code": 400, "msg": "Request format wrong!"}) + return + } + Link() + DB := db.DBsnctf + session, err := Store.Get(c.Request, "SNCTFSESSID") + if err != nil { + c.JSON(200, gin.H{"code": 400, "msg": "Get SNCTFSESSID error"}) + } + user, ok := session.Values["user"].(User) + if !ok { + c.JSON(200, gin.H{"code": 400, "msg": "No session"}) + } + //获取用户id + userid := user.ID + //获取传入数据 + username := request.Name + affiliation := request.Affiliation + country := request.Country + website := request.Website + email := request.Email + //数据库更新数据 + err = DB.Model(&user).Where("id = ?", userid).Update("username", username).Update("affiliation", affiliation).Update("country", country).Update("website", website).Update("email", email).Error + if err != nil { + c.JSON(200, gin.H{"code": 400, "msg": "Update info error!"}) + return + } + //更新session + session.Values["user"] = user + c.JSON(200, gin.H{"code": 200, "msg": "Update info success!"}) + +} + // GetAllUserInfo 获取所有用户信息 func GetAllUserInfo(c *gin.Context) { var info PublicAllInfoResponse diff --git a/routers/router.go b/routers/router.go index 6ecbc66..89f02b1 100644 --- a/routers/router.go +++ b/routers/router.go @@ -35,6 +35,8 @@ func Initrouter() { { // 获取当前用户信息 personal.GET("/session", api.Session) + // 修改当前用户的信息 + personal.PUT("/userinfo", api.UpdateUserInfo) // 获取题目分类 personal.GET("/category", api.GetCategories) // 获取所有题目信息 diff --git a/snctf.db b/snctf.db index 24fc357..069a119 100644 Binary files a/snctf.db and b/snctf.db differ diff --git a/type/userTypes.go b/type/userTypes.go index c4b2cc3..e1d8fa2 100644 --- a/type/userTypes.go +++ b/type/userTypes.go @@ -94,6 +94,7 @@ type PublicInfoResponse struct { Affiliation string `json:"affiliation"` Country string `json:"country"` TeamID int `json:"team_id"` + Website string `json:"website"` } // PublicAllInfoResponse 定义返回所有用户公开信息结构体 暂时不显示队伍名称 @@ -133,3 +134,12 @@ type StudentsOrOthersInfoResponse struct { IDOrEmail string `json:"id_email"` QQ string `json:"qq"` } + +// UpdateUserInfoResponse 定义更新用户信息的响应 +type UpdateUserInfoRequest struct { + Name string `json:"name"` + Email string `json:"email"` + Country string `json:"country"` + Website string `json:"website"` + Affiliation string `json:"affiliation"` +}