From 0f609c51c7b015463ad3e467bab64a7f65249b06 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Fri, 7 Jun 2024 20:16:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E5=8F=8D=E5=A4=8D=E5=88=9B=E5=BB=BA=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E4=BF=AE=E5=A4=8Dredis=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/db.go | 6 +++--- handler/user.go | 37 +++++++++++++++++++++++++++++++++---- main.go | 2 +- service/userService.go | 2 +- worker/redis.go | 8 ++++++-- 5 files changed, 44 insertions(+), 11 deletions(-) diff --git a/dao/db.go b/dao/db.go index 58e97a0..bc3bfef 100644 --- a/dao/db.go +++ b/dao/db.go @@ -14,9 +14,9 @@ func Init() { if err != nil { panic("failed to connect database") } - db.AutoMigrate(&User{}) // 自动迁移,创建表,如果表已经存在,会自动更新表结构,不会删除表,只会创建不存在的表 - db.AutoMigrate(&Video{}) - db.AutoMigrate(&Device{}) + db.Migrate(&User{}) // 自动迁移,创建表,如果表已经存在,会自动更新表结构,不会删除表,只会创建不存在的表 + db.Migrate(&Video{}) + db.Migrate(&Device{}) DB = db } diff --git a/handler/user.go b/handler/user.go index 4154e98..a9f34e9 100644 --- a/handler/user.go +++ b/handler/user.go @@ -21,6 +21,7 @@ func SetUpUserGroup(router *gin.Engine) { userGroup.POST("/uuid", ScanUUID) userGroup.POST("/gqr", GetQRStatus) userGroup.POST("/sqr", SetQRStatus) + userGroup.POST("/confirm", ConfirmQRLogin) } type RLReq struct { @@ -42,12 +43,39 @@ func ScanUUID(c *gin.Context) { } func SetQRStatus(c *gin.Context) { - c.JSON(200, gin.H{"code": 0, "message": "success", "data": "1"}) + var qrsetReq QRReq + if err := c.ShouldBind(&qrsetReq); err == nil { + res := worker.SetRedis(qrsetReq.UUID, "1") + if res { + c.JSON(200, gin.H{"code": 0, "message": "success", "data": "1"}) + } else { + c.JSON(200, gin.H{"code": 1, "message": "该二维码数据无效", "data": "1"}) + } + } else { + c.JSON(200, gin.H{"code": 1, "message": err, "data": "2"}) + } } // 确认返回token数据 func ConfirmQRLogin(c *gin.Context) { - c.JSON(200, gin.H{"code": 0, "message": "success", "data": "1"}) + var qrsetReq QRReq + if err := c.ShouldBind(&qrsetReq); err == nil { + //user_id, _ := c.Get("id") + user_name, _ := c.Get("username") + if user_name != "" { + key := "user_" + user_name.(string) + token := worker.GetRedis(key) + if worker.SetRedis(qrsetReq.UUID, token) { + c.JSON(200, gin.H{"code": 0, "message": "success", "data": "1"}) + } else { + c.JSON(200, gin.H{"code": 1, "message": "设置Token失败", "data": "3"}) + } + } else { + c.JSON(200, gin.H{"code": 1, "message": "failed", "data": "2"}) + } + } else { + c.JSON(200, gin.H{"code": 1, "message": err, "data": "3"}) + } } func GetQRStatus(c *gin.Context) { @@ -90,7 +118,7 @@ func loginHandler(c *gin.Context) { hasher.Write([]byte(req_data.Password)) // 生成密码的 MD5 散列值 req_data.Password = hex.EncodeToString(hasher.Sum(nil)) // 生成密码的 MD5 散列值 } - user := service.GetUser(req_data.User, req_data.Password) + user := service.GetUser(req_data.User, req_data.Password, req_data.Password) if user.ID != 0 { redis_token := worker.GetRedis("user_" + user.Name) if redis_token == "" { @@ -120,7 +148,8 @@ func loginHandler(c *gin.Context) { data["token"] = tokenString c.JSON(200, gin.H{"code": 0, "message": "success", "data": data}) } else { - c.JSON(200, gin.H{"error": "user not found", "code": 1, "message": "error"}) + //用户名或密码错误 + c.JSON(200, gin.H{"error": "用户名或密码错误", "code": 1, "message": "error"}) } } else { c.JSON(200, gin.H{"error": err.Error(), "code": 1, "message": "error"}) diff --git a/main.go b/main.go index 034b934..9318aea 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,7 @@ 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, "/uuid") { + 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") { c.Next() return } diff --git a/service/userService.go b/service/userService.go index 86d8be6..f77b06c 100644 --- a/service/userService.go +++ b/service/userService.go @@ -9,7 +9,7 @@ func CreateUser(name string, password, email string) uint { return dao.CreateUser(name, password, email) } -func GetUser(name, password string) dao.User { +func GetUser(name, email, password string) dao.User { emailRegex := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$` re := regexp.MustCompile(emailRegex) var user dao.User diff --git a/worker/redis.go b/worker/redis.go index 998d66c..70de3d8 100644 --- a/worker/redis.go +++ b/worker/redis.go @@ -49,13 +49,15 @@ func isContainKey(key string) bool { } // 设置redis -func SetRedis(key string, value string) { +func SetRedis(key string, value string) bool { ctx := context.Background() // 设置键值对, 0 表示不设置过期时间, 如果需要设置过期时间, 可以设置为 time.Second * 10 等 err := redisClient.Set(ctx, key, value, time.Second*100).Err() if err != nil { log.Fatalf("Error setting key: %v", err) + return false } + return true } // 设置hash @@ -92,13 +94,15 @@ func GetHash(key string) RUser { } // 设置redis -func SetRedisWithExpire(key string, value string, expire time.Duration) { // 设置键值对, 0 表示不设置过期时间, 如果需要设置过期时间, 可以设置为 time.Second * 10 等 +func SetRedisWithExpire(key string, value string, expire time.Duration) bool { // 设置键值对, 0 表示不设置过期时间, 如果需要设置过期时间, 可以设置为 time.Second * 10 等 ctx := context.Background() // 设置键值对, 0 表示不设置过期时间, 如果需要设置过期时间, 可以设置为 time.Second * 10 等 err := redisClient.Set(ctx, key, value, expire).Err() if err != nil { log.Fatalf("Error setting key: %v", err) + return false } + return true } // 获取redis