修复数据库反复创建问题,修复用户登录问题,修复redis设置失败问题

This commit is contained in:
junleea 2024-06-07 20:16:08 +08:00
parent db4397ad56
commit 0f609c51c7
5 changed files with 44 additions and 11 deletions

View File

@ -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
}

View File

@ -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"})

View File

@ -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
}

View File

@ -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

View File

@ -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