From a13bcd437d1d0d4ea40803a84a151caf265097f4 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Sun, 6 Oct 2024 15:30:38 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/user.go | 28 +++++++++++++++++++++++++--- proto/status.go | 1 + 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/handler/user.go b/handler/user.go index bc53b8d..f80d326 100644 --- a/handler/user.go +++ b/handler/user.go @@ -45,13 +45,35 @@ type SearchReq struct { Keyword string `json:"keyword" form:"keyword"` ID int `json:"id" form:"id"` } +type GetUserInfoReq struct { + ID int `json:"id" form:"id"` +} func GetUserInfo(c *gin.Context) { + var req_data GetUserInfoReq id, _ := c.Get("id") user_id := int(id.(float64)) - user := dao.FindUserByID2(user_id) - user.Password = "" //不返回密码 - c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": user}) + if err := c.ShouldBind(&req_data); err != nil { + var user dao.User + if req_data.ID == user_id { + user = dao.FindUserByID2(user_id) + user.Password = "" //不返回密码 + } else { + //判断当前用户是否有权限查看 + cur_user := dao.FindUserByID2(user_id) + if cur_user.Role == "admin" { + user = dao.FindUserByID2(req_data.ID) + user.Password = "" //不返回密码 + } else { + c.JSON(200, gin.H{"code": proto.PermissionDenied, "message": "无权查看", "data": "2"}) + return + } + } + c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": user}) + } else { + c.JSON(200, gin.H{"code": proto.ParameterError, "message": err, "data": "2"}) + return + } } func GetScanUUID(c *gin.Context) { diff --git a/proto/status.go b/proto/status.go index 6554ee8..ddd36af 100644 --- a/proto/status.go +++ b/proto/status.go @@ -20,6 +20,7 @@ const ( // 用户名密码相关错误码 UsernameOrPasswordError = 6 // 用户名或密码错误 UsernameExists = 7 // 用户名已存在 + PermissionDenied = 8 // 权限不足 // Redis相关错误码 RedisSetError = 8 // 设置redis错误 From 53e04c2fc2aae6758f37c9a823bdd0f4ff75cc82 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Sun, 6 Oct 2024 15:43:30 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/user.go | 2 +- proto/status.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/handler/user.go b/handler/user.go index f80d326..3f55d78 100644 --- a/handler/user.go +++ b/handler/user.go @@ -53,7 +53,7 @@ func GetUserInfo(c *gin.Context) { var req_data GetUserInfoReq id, _ := c.Get("id") user_id := int(id.(float64)) - if err := c.ShouldBind(&req_data); err != nil { + if err := c.ShouldBind(&req_data); err == nil { var user dao.User if req_data.ID == user_id { user = dao.FindUserByID2(user_id) diff --git a/proto/status.go b/proto/status.go index ddd36af..b259edf 100644 --- a/proto/status.go +++ b/proto/status.go @@ -18,9 +18,9 @@ const ( TokenParseError = 19 // Token解析错误 // 用户名密码相关错误码 - UsernameOrPasswordError = 6 // 用户名或密码错误 - UsernameExists = 7 // 用户名已存在 - PermissionDenied = 8 // 权限不足 + UsernameOrPasswordError = 6 // 用户名或密码错误 + UsernameExists = 7 // 用户名已存在 + PermissionDenied = 21 // 权限不足 // Redis相关错误码 RedisSetError = 8 // 设置redis错误 From f9b62f6618e3ae870b83e27d3afabbf530080986 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Sun, 6 Oct 2024 16:35:54 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/user.go | 5 +++++ handler/user.go | 19 +++++++++++++++++++ proto/user_req.go | 13 +++++++++++++ service/userService.go | 13 +++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 proto/user_req.go 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 + } +} From c6c6dbe971487c653e8645d5f1f1c0b1ab509417 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Sun, 6 Oct 2024 16:38:52 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dao/user.go b/dao/user.go index 7eb25d4..dffcb03 100644 --- a/dao/user.go +++ b/dao/user.go @@ -78,5 +78,5 @@ func UpdateUserByID(id int, name, password, email string) { } 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}) + DB.Debug().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}) } From c7be07d22a55caf28cedadcdc2938b986f5fa251 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Sun, 6 Oct 2024 16:41:29 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/userService.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/userService.go b/service/userService.go index 1aa6bae..d090431 100644 --- a/service/userService.go +++ b/service/userService.go @@ -48,7 +48,7 @@ func UpdateUser(user_id int, req proto.UpdateUserInfoReq) (int, error) { dao.UpdateUserByID2(user_id, req) return user_id, nil } else if cur_user.Role == "admin" { - dao.UpdateUserByID2(user_id, req) + dao.UpdateUserByID2(req.ID, req) return req.ID, nil } else { return 0, nil From ec4ec6ab447b2a5476ff42a5cbaea529e448a0ff Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Sun, 6 Oct 2024 16:44:26 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dao/user.go b/dao/user.go index dffcb03..b7482d5 100644 --- a/dao/user.go +++ b/dao/user.go @@ -78,5 +78,5 @@ func UpdateUserByID(id int, name, password, email string) { } func UpdateUserByID2(id int, req proto.UpdateUserInfoReq) { - DB.Debug().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}) + DB.Debug().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, Gender: req.Gender}) } From 2e96977f2678999e5ed61213015b4e7767633d22 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Mon, 7 Oct 2024 15:22:58 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9url=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 8 +++++--- proto/conf.go | 1 + worker/redis.go | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 52466a9..48c4966 100644 --- a/main.go +++ b/main.go @@ -86,9 +86,11 @@ func JWTAuthMiddleware() gin.HandlerFunc { } //如果请求为login或register,则不需要验证token - if strings.Contains(c.Request.URL.Path, "/login") || strings.Contains(c.Request.URL.Path, "/register") || strings.Contains(c.Request.URL.Path, "/uuid") || strings.Contains(c.Request.URL.Path, "/gqr") || strings.Contains(c.Request.URL.Path, "/cid/callback") { - c.Next() - return + for k, _ := range proto.Url_map { + if strings.Contains(c.Request.URL.Path, k) { + c.Next() + return + } } if tokenString == "" { //c.AbortWithStatus(200) diff --git a/proto/conf.go b/proto/conf.go index 427fa4b..15b5053 100644 --- a/proto/conf.go +++ b/proto/conf.go @@ -9,6 +9,7 @@ import ( var Config ConfigStruct var SigningKey = []byte{} +var Url_map = map[string]bool{"/login": true, "/register": true, "/uuid": true, "/gqr": true, "/cid/callback": true} const ( MYSQL_USER = "video_t2" diff --git a/worker/redis.go b/worker/redis.go index 709790c..b716737 100644 --- a/worker/redis.go +++ b/worker/redis.go @@ -346,3 +346,29 @@ func GetRedisBitmap(key string, offset int64) int { } return int(val) } + +// 发布订阅者模式-发布消息 +func Publish(channel string, message string, expire time.Duration) { + ctx := context.Background() + err := redisClient.Publish(ctx, channel, message).Err() + if err != nil { + fmt.Println("Error publishing message: %v", err) + } + err = redisClient.Expire(ctx, channel, expire).Err() + if err != nil { + fmt.Println("Error setting key: %v", err) + } +} + +// 发布订阅者模式-订阅消息 +func Subscribe(channel string) []string { + ctx := context.Background() + pubsub := redisClient.Subscribe(ctx, channel) + ch := pubsub.Channel() + defer pubsub.Close() + var messages []string + for msg := range ch { + messages = append(messages, msg.Payload) + } + return messages +}