From 4bbcbb3531ebfa08ce5dc15211866b1dc3e45ec3 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Tue, 11 Jun 2024 18:38:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=AB=E7=A0=81=E7=99=BB=E5=BD=95uuid?= =?UTF-8?q?=EF=BC=8Credis=20hash=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/user.go | 25 ++++++++++++++++++------ worker/redis.go | 51 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/handler/user.go b/handler/user.go index 91b374c..edd6818 100644 --- a/handler/user.go +++ b/handler/user.go @@ -33,21 +33,24 @@ type RLReq struct { } type QRReq struct { - UUID string `json:"uuid" form:"uuid"` + UUID string `json:"uuid" form:"uuid"` + Address string `json:"address" form:"address"` + IP string `json:"ip" form:"ip"` } func ScanUUID(c *gin.Context) { id := uuid.New() - worker.SetRedisWithExpire(id.String(), "0", time.Minute*3) + worker.SetHash(id.String(), map[string]interface{}{"status": "0", "address": "", "ip": ""}) c.JSON(200, gin.H{"code": 0, "message": "success", "data": id.String()}) } func SetQRStatus(c *gin.Context) { var qrsetReq QRReq if err := c.ShouldBind(&qrsetReq); err == nil { - res := worker.SetRedis(qrsetReq.UUID, "1") + res := worker.SetHashWithField(qrsetReq.UUID, "status", "1") if res { - c.JSON(200, gin.H{"code": 0, "message": "success", "data": "1"}) + data := worker.GetHashAll(qrsetReq.UUID) + c.JSON(200, gin.H{"code": 0, "message": "success", "data": data}) } else { c.JSON(200, gin.H{"code": 1, "message": "该二维码数据无效", "data": "1"}) } @@ -81,7 +84,13 @@ func ConfirmQRLogin(c *gin.Context) { func GetQRStatus(c *gin.Context) { var qrReq QRReq if err := c.ShouldBind(&qrReq); err == nil { - str := worker.GetRedis(qrReq.UUID) + end := worker.SetHashWithField(qrReq.UUID, "address", qrReq.Address) + end_ := worker.SetHashWithField(qrReq.UUID, "ip", qrReq.IP) + if !end || !end_ { + c.JSON(200, gin.H{"code": 1, "message": "failed", "data": "2"}) + return + } + str := worker.GetHash(qrReq.UUID, "status") switch str { case "0": c.JSON(200, gin.H{"code": 0, "message": "success", "data": "0"}) //空值 @@ -137,7 +146,11 @@ func loginHandler(c *gin.Context) { worker.SetRedisWithExpire("user_"+user.Name, tokenString, time.Hour*10) // 将用户信息存入 worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟 - worker.SetHash(tokenString, int(user.ID), user.Name, user.Email) // 将用户信息存入 + data := make(map[string]interface{}) + data["id"] = user.ID + data["username"] = user.Name + data["email"] = user.Email + worker.SetHash(tokenString, data) // 将用户信息存入 } else { tokenString = redis_token } diff --git a/worker/redis.go b/worker/redis.go index 5b8acdc..10c1d8e 100644 --- a/worker/redis.go +++ b/worker/redis.go @@ -61,7 +61,7 @@ func SetRedis(key string, value string) bool { } // 设置hash -func SetHash(key string, id int, name, email string) bool { +func SetHashWithTime(key string, id int, name, email string, duration time.Duration) bool { //捕获错误,如果错误返回 ctx := context.Background() // 创建一个上下文 @@ -85,17 +85,50 @@ func SetHash(key string, id int, name, email string) bool { return true } -// 获取hash -func GetHash(key string) RUser { +// 设置redis hash,设置过期时间 +func SetHash(key string, data map[string]interface{}) bool { ctx := context.Background() - val, err := redisClient.HGetAll(ctx, key).Result() // 从 Redis 读取哈希表, 如果键不存在则返回空字符串, 如果出现错误则返回错误 + err := redisClient.HMSet(ctx, key, data).Err() if err != nil { - fmt.Println("Error getting key: %v", err) + fmt.Println("%v :Error setting hash: %v", key, err) + return false } - id, _ := strconv.Atoi(val["id"]) - name, _ := val["name"] - email, _ := val["email"] - return RUser{ID: id, Name: name, Email: email} + err = redisClient.Expire(ctx, key, time.Minute*3).Err() + if err != nil { + fmt.Println("%v :Error setting expire: %v", key, err) + return false + } + return true +} + +func SetHashWithField(key string, field string, value string) bool { + ctx := context.Background() + err := redisClient.HSet(ctx, key, field, value).Err() + if err != nil { + fmt.Println("Error setting key: %v", err) + return false + } + return true +} + +func GetHash(key string, field string) string { + ctx := context.Background() + val, err := redisClient.HGet(ctx, key, field).Result() + if err != nil { + fmt.Println("Error getting hash: %v", err) + return "" + } + return val +} + +func GetHashAll(key string) map[string]string { + ctx := context.Background() + val, err := redisClient.HGetAll(ctx, key).Result() + if err != nil { + fmt.Println("Error getting hash: %v", err) + return nil + } + return val } // 设置redis